Compare commits

...
69 Commits
Author SHA1 Message Date
Mervin Praison 657fd93743 Release v3.9.2 2026-01-16 04:37:53 +00:00
github-actions[bot] f0a2936b7f docs: auto-update api.md [skip ci] 2026-01-15 20:08:49 +00:00
Mervin Praison 58778492ca Release v3.9.1 2026-01-15 20:08:35 +00:00
Mervin Praison 08c444f3a8 Release v3.9.0 2026-01-15 16:05:47 +00:00
Mervin Praison 34c1b39e95 Release v3.8.22 2026-01-15 13:01:10 +00:00
Mervin Praison 5e4c58a9ac fix(ci): correct CLI commands - session list, queue list (not ls) 2026-01-15 11:06:32 +00:00
Mervin Praison 19a30ff3d9 fix(ci): fix Handoff test - use correct parameters 2026-01-15 11:00:32 +00:00
Mervin Praison 50f12c7682 fix(ci): correct PlanningConfig params (reasoning, auto_approve) 2026-01-15 10:57:50 +00:00
Mervin Praison 5a70b8abb9 fix(ci): correct ExecutionConfig params (max_iter, max_retry_limit) 2026-01-15 10:55:33 +00:00
Mervin Praison b7d4d219b9 fix(ci): correct MemoryConfig test to use backend param 2026-01-15 10:52:56 +00:00
Mervin Praison ff2d83ffdf Release v3.8.21 2026-01-15 10:50:27 +00:00
Mervin Praison 2d58caa741 fix(ci): correct import paths for Windows tests
- MemoryConfig from praisonaiagents.config (not memory)
- FileMemory from praisonaiagents.memory
- OutputConfig, ExecutionConfig, PlanningConfig from config
2026-01-15 10:50:25 +00:00
Mervin Praison d31cf379e5 feat(ci): expand Windows tests with Memory, Tools, Config, Handoff, async
Added tests for:
- Memory: FileMemory, MemoryConfig
- Tool decorator (@tool)
- Config classes: OutputConfig, ExecutionConfig, PlanningConfig
- Handoff between agents
- Async features (arun, astart availability)
- Multi-agent real API test
2026-01-15 10:47:11 +00:00
Mervin Praison 719ef04e7c fix(memory): make fcntl import conditional for Windows compatibility
fcntl is Unix-only; on Windows, skip file locking.
This fixes ModuleNotFoundError: No module named 'fcntl' on Windows.

- Add _HAS_FCNTL flag based on sys.platform
- Update _read_json and _write_json to conditionally use fcntl.flock
- File locking is acceptable to skip for single-process usage
2026-01-15 10:38:45 +00:00
Mervin Praison 4fb25a99d5 fix(ci): fix Windows workflow YAML syntax and secrets condition 2026-01-15 10:35:18 +00:00
Mervin Praison 0fdacff2cf feat(ci): add Windows CI test workflow
Tests key PraisonAI functionality on Windows:
- Core imports: Agent, Agents, Workflow, Task, WorkflowStep
- Object instantiation
- CLI commands: --help, session ls, queue ls
- Real API test with OPENAI_API_KEY

Timeout: 5 minutes
Runs on: windows-latest
Triggers: push/PR to main/develop + manual dispatch
2026-01-15 10:33:56 +00:00
Mervin Praison 6a1fa19c6e Release v3.8.20 2026-01-15 09:45:45 +00:00
Mervin Praison a4725a027a Release v3.8.19 2026-01-15 07:29:27 +00:00
Mervin Praison dc9a8f3443 Release v3.8.18 2026-01-15 04:41:16 +00:00
Mervin Praison 9421e28c8a Release v3.8.17 2026-01-13 18:29:06 +00:00
Mervin Praison c3d60c82ca feat: add debug output with token/cost metrics
- Added llm_end callback to openai_client.py and llm.py with:
  - tokens_in, tokens_out from usage
  - cost from litellm.completion_cost()
  - latency_ms timing
- Updated trace.py and status.py to display metrics
- Added show_metrics param to enable debug mode metrics display
- Agent.py passes metrics flag to enable_status_output

Debug output now shows:
  [17:18:22] Calling LLM (gpt-4o-mini)...
  [17:18:24]   │ 📊 gpt-4o-mini: 104→14 tokens [1.5s]
  [17:18:24] ▸ get_weather(city='Tokyo')
  [17:18:24] Calling LLM (gpt-4o-mini)...
  [17:18:25]   │ 📊 gpt-4o-mini: 133→8 tokens [0.7s]
  Response: The weather in Tokyo is sunny.
2026-01-13 17:20:06 +00:00
Mervin Praison 8c509b439f Release v3.8.16 2026-01-13 16:55:40 +00:00
Mervin Praison 3fbf1a353e fix: consistent status output for all LLM paths (OpenAI + LiteLLM)
- Fixed _execute_callback_and_display to always trigger callback
  (removed incorrect 'if not _using_custom_llm' guard)
- Added tool_call callback to llm.py for LiteLLM path
- Removed duplicate llm_start callback from llm.py method entry
- Reset AI call counter between agent runs
- Both sync LLM paths now show identical output

Verified output for both paths:
  ▸ AI → thinking...
    │ → get_weather(city="Tokyo") → sunny
  ▸ AI → responding...
  Final Output: ...
2026-01-13 16:45:08 +00:00
Mervin Praison e0f3a49e41 improve: status output UX - cleaner, user-friendly format
- Changed 'LLM' to 'AI' for user-friendliness
- Added context: 'thinking' (1st call) / 'responding' (2nd call)
- Tool calls now show inline with result: │ → tool(args) → result
- Fixed openai_client to pass structured params to callbacks

Output now looks like:
  ▸ AI → thinking...
    │ → get_weather(city="Tokyo") → sunny
  ▸ AI → responding...
  Final Output: ...
2026-01-13 16:36:51 +00:00
Mervin Praison 987b43877f fix: show all LLM calls in status output (including tool follow-ups)
- Added llm_start callback in openai_client.py while loop
- Each LLM API call now triggers callback for status display
- Removed duplicate callback from agent._chat_completion

Status output now correctly shows:
  ▸ Calling LLM (gpt-4o-mini)...       # 1st: decides to call tool
  │ → get_weather(city="Tokyo")
  ▸ Calling LLM (gpt-4o-mini)...       # 2nd: generates response
  Final Output: ...
2026-01-13 16:18:53 +00:00
Mervin Praison ebb383208e feat: add LLM call display to status/trace output
- Added llm_start callback in agent._chat_completion()
- Registered on_llm_start callback in enable_status_output()
- StatusOutput.llm_start() displays '▸ Calling LLM (model)...'
- Works for both LiteLLM and OpenAI client paths

Status output now shows:
  ▸ Calling LLM (openai/gpt-4o-mini)...
  │ → get_weather(city="Tokyo")
  Final Output: ...
2026-01-13 16:10:46 +00:00
Mervin Praison c9e4f0738a refactor: rename output modules for user-friendliness
- Rename actions.py → status.py (handles status preset, no timestamps)
- Rename old status.py → trace.py (handles trace preset, with timestamps)
- ActionsSink → StatusOutput
- StatusSink → TraceOutput
- enable_actions_mode → enable_status_output
- enable_status_mode → enable_trace_output

This makes internal code match user-facing preset names:
  output='status' → StatusOutput (▸ tool → result)
  output='trace' → TraceOutput ([HH:MM:SS] ▸ tool [0.2s])
2026-01-13 15:59:42 +00:00
Mervin Praison db0a24555d fix: status preset correctly hides timestamps, add LLM call methods
- Added show_timestamps param to ActionsSink (default True)
- Agent passes show_timestamps=False for status preset (simple_output=True)
- Added llm_start/llm_end methods to ActionsSink for future LLM tracking
- status: │ → tool() (no timestamps)
- trace: [HH:MM:SS] ▸ tool [0.2s] ✓ (with timestamps)
2026-01-13 15:51:51 +00:00
Mervin Praison e3fe35e2b6 fix: status preset now hides timestamps correctly
- Added show_timestamps param to ActionsSink
- Agent passes show_timestamps=False when simple_output=True
- status: │ → tool() (no timestamps)
- trace: [HH:MM:SS] ▸ tool [0.2s] ✓ (with timestamps)
2026-01-13 15:46:57 +00:00
Mervin Praison 74a7322b83 refactor: rename output presets for clarity
- Rename 'text' → 'status' (no timestamps, simple progress)
- Rename old 'status' → 'trace' (with timestamps, full trace)
- Change 'debug' = trace + metrics (no boxes, developer-focused)
- Keep 'verbose' = rich panels (interactive use)
- Add 'text' as backward compat alias → status

New hierarchy (most intuitive):
  silent → status → trace → debug → verbose → stream → json
2026-01-13 15:10:05 +00:00
Mervin Praison 7ceb7f8cb9 Release v3.8.14 2026-01-13 14:46:59 +00:00
Mervin Praison 84e5b6c693 fix: Agents panels now render Markdown (bold, headers, lists)
Changed Text() to Markdown() in agents.start() verbose output so
**bold**, # headers, - lists render properly in multi-agent panels.
2026-01-13 14:44:17 +00:00
Mervin Praison 5f00ddbc7c Release v3.8.13 2026-01-13 14:43:54 +00:00
Mervin Praison db551c5d65 feat: add Markdown rendering for status output mode
- Added use_markdown=True parameter to StatusSink
- response() now renders content with rich.markdown.Markdown
- Lazy import for performance, graceful fallback
2026-01-13 14:39:53 +00:00
Mervin Praison aed6027256 Release v3.8.12 2026-01-13 14:33:31 +00:00
Mervin Praison a9d709645a fix: respect explicit output config, don't override with TTY auto-verbose
- Added _has_explicit_output_config flag to track when user sets output=
- Modified agent.start() to skip TTY auto-verbose if user explicitly configured output
- Fixes issue where output='status' was overridden by TTY auto-verbose showing panels

Before: output='status' in TTY showed panels (wrong)
After: output='status' in TTY shows clean inline status (correct)
2026-01-13 14:30:56 +00:00
Mervin Praison a430e1b873 Release v3.8.11 2026-01-13 14:21:02 +00:00
Mervin Praison 8e0a40db56 Release v3.8.10 2026-01-13 13:55:31 +00:00
Mervin Praison 8d1e4637d4 feat: add status output mode for clean inline progress
- Add new 'status' preset showing [HH:MM:SS] ▸ tool → result ✓
- Create output/status.py with StatusSink class
- Add status_trace field to OutputConfig
- Update presets.py with full preset hierarchy
- Consolidate presets: text, status, verbose, debug, stream, json
- Add backward compatible aliases (plain→silent, minimal→silent, etc.)

Fixes confusing verbose output by providing clean alternative
2026-01-13 13:52:05 +00:00
Mervin Praison e11d62dc89 Release v3.8.9 2026-01-13 13:14:04 +00:00
Mervin Praison 13a9a3e07d Release v3.8.8 2026-01-13 13:06:26 +00:00
Mervin Praison b95f6b0665 Release v3.8.7 2026-01-13 12:55:43 +00:00
github-actions[bot] e8c4410014 docs: auto-update api.md [skip ci] 2026-01-13 09:30:27 +00:00
Mervin Praison 6c2d852ed1 Release v3.8.6 2026-01-13 09:30:11 +00:00
Mervin Praison 6f95ae8d9d Release v3.8.5 2026-01-13 07:04:53 +00:00
Mervin Praison 8e2dd09b3b Release v3.8.4 2026-01-13 06:17:09 +00:00
github-actions[bot] 8ecfad5c5a docs: auto-update api.md [skip ci] 2026-01-13 06:01:35 +00:00
Mervin Praison 35a88a0913 Release v3.8.3 2026-01-13 06:01:20 +00:00
Mervin Praison 6df3e32640 Release v3.8.2 2026-01-13 04:16:08 +00:00
github-actions[bot] fb36e1c50c docs: auto-update api.md [skip ci] 2026-01-12 17:00:39 +00:00
Mervin Praison ac5454cb00 Release v3.8.1 2026-01-12 17:00:20 +00:00
github-actions[bot] bbe95795d1 docs: auto-update api.md [skip ci] 2026-01-12 12:49:30 +00:00
Mervin Praison bdaf5340f9 Release v3.8.0 2026-01-12 12:49:16 +00:00
Mervin Praison 5579614001 Release v3.7.9 2026-01-12 09:10:46 +00:00
Mervin Praison 2c4dd0e2d4 Release v3.7.8 2026-01-12 07:39:39 +00:00
Mervin Praison fd23d62841 Release v3.7.7 2026-01-11 21:30:04 +00:00
Mervin Praison 5f3bcb78ac Release v3.7.6 2026-01-11 08:23:08 +00:00
Mervin Praison 3c40c2278e Release v3.7.5 2026-01-11 07:34:59 +00:00
Mervin Praison 1921b03aab chore: remove empty trace and comparison test files 2026-01-10 21:09:11 +00:00
Mervin Praison f33bf57662 Release v3.7.4 2026-01-10 21:06:16 +00:00
github-actions[bot] df21c419ea docs: auto-update api.md [skip ci] 2026-01-10 19:45:30 +00:00
Mervin Praison fe997d525c ci: add workflow to auto-update api.md on main branch push 2026-01-10 19:45:04 +00:00
Mervin Praison ca26988774 Release v3.7.3 2026-01-10 19:43:43 +00:00
Mervin Praison 3600594316 Before AI example generation 2026-01-10 16:54:16 +00:00
Mervin Praison 097cd42890 Before testing 2026-01-10 16:04:21 +00:00
Mervin Praison 595add2991 Before testing AI generation 2026-01-10 15:55:26 +00:00
Mervin Praison 03a3aa420b Release v3.7.2 2026-01-10 15:31:05 +00:00
Mervin Praison 9533abd8a4 Release v3.7.1 2026-01-10 14:29:43 +00:00
Mervin Praison 39c9e50875 Release v3.7.0 2026-01-10 13:09:09 +00:00
453 changed files with 47669 additions and 7703 deletions

No files matched your search

+204
View File
@@ -0,0 +1,204 @@
name: Windows Tests
on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]
workflow_dispatch:
jobs:
test-windows:
runs-on: windows-latest
timeout-minutes: 5
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_MODEL_NAME: gpt-4o-mini
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install praisonaiagents
run: |
python -m pip install --upgrade pip
cd src/praisonai-agents
pip install .
shell: bash
- name: Install praisonai wrapper
run: |
cd src/praisonai
pip install .[dev]
shell: bash
# ==================== CORE IMPORTS ====================
- name: Test core imports
run: |
python -c "from praisonaiagents import Agent, Agents, Workflow; print('Core imports OK')"
python -c "from praisonaiagents import tool, Tools; print('Tool imports OK')"
python -c "from praisonaiagents import Task, WorkflowStep; print('Task/Step imports OK')"
# ==================== AGENT FEATURES ====================
- name: Test Agent instantiation
run: |
python -c "
from praisonaiagents import Agent
a = Agent(name='Test', instructions='You are helpful')
print('Agent created:', a.name)
a2 = Agent(name='ToolAgent', instructions='Use tools', tools=[])
print('Agent with tools:', a2.name)
"
- name: Test Agents orchestration
run: |
python -c "
from praisonaiagents import Agent, Agents, Task
a1 = Agent(name='Agent1', instructions='First agent')
a2 = Agent(name='Agent2', instructions='Second agent')
t = Task(description='Do something', agent=a1)
agents = Agents(agents=[a1, a2], tasks=[t])
print('Agents:', len(agents.agents), 'Tasks:', len(agents.tasks))
"
# ==================== WORKFLOW FEATURES ====================
- name: Test Workflow and WorkflowStep
run: |
python -c "
from praisonaiagents import Workflow, WorkflowStep
from praisonaiagents.workflows import WorkflowOutputConfig
w = Workflow(name='test')
s1 = WorkflowStep(name='step1')
s2 = WorkflowStep(name='step2', context=['step1'])
print('Workflow:', w.name)
print('Steps:', s1.name, s2.name)
cfg = WorkflowOutputConfig(verbose=True, stream=False)
w2 = Workflow(name='configured', output=cfg)
print('Configured workflow verbose:', w2.verbose)
"
# ==================== MEMORY FEATURES ====================
- name: Test Memory classes
run: |
python -c "
from praisonaiagents.memory import FileMemory
from praisonaiagents.config import MemoryConfig
cfg = MemoryConfig(backend='file', user_id='test_user')
print('MemoryConfig backend:', cfg.backend)
mem = FileMemory(user_id='test_user', base_path='.praison/test_memory')
print('FileMemory user:', mem.user_id)
"
# ==================== TOOL DECORATOR ====================
- name: Test tool decorator
run: |
python -c "
from praisonaiagents import tool
@tool
def my_tool(query: str) -> str:
'''A test tool'''
return f'Echo: {query}'
result = my_tool('hello')
print('Tool result:', result)
"
# ==================== CONFIG CLASSES ====================
- name: Test config classes
run: |
python -c "
from praisonaiagents.config import OutputConfig, ExecutionConfig, PlanningConfig
out_cfg = OutputConfig(verbose=True, stream=True)
print('OutputConfig verbose:', out_cfg.verbose)
exec_cfg = ExecutionConfig(max_iter=50, max_retry_limit=5)
print('ExecutionConfig max_iter:', exec_cfg.max_iter)
plan_cfg = PlanningConfig(reasoning=True, auto_approve=False)
print('PlanningConfig reasoning:', plan_cfg.reasoning)
"
# ==================== HANDOFF ====================
- name: Test Handoff
run: |
python -c "
from praisonaiagents import Handoff, handoff, Agent
a1 = Agent(name='Agent1', instructions='First')
a2 = Agent(name='Agent2', instructions='Second')
# Using Handoff class directly
h = Handoff(agent=a2)
print('Handoff tool name:', h.tool_name)
# Using handoff function
h2 = handoff(a1, tool_name_override='custom_handoff')
print('Custom handoff name:', h2.tool_name)
"
# ==================== ASYNC FEATURES ====================
- name: Test async imports
run: |
python -c "
import asyncio
from praisonaiagents import Agent
async def test_async():
agent = Agent(name='AsyncAgent', instructions='Test')
assert hasattr(agent, 'arun'), 'Missing arun'
assert hasattr(agent, 'astart'), 'Missing astart'
print('Async methods available')
asyncio.run(test_async())
"
# ==================== CLI COMMANDS ====================
- name: Test CLI help
run: praisonai --help
- name: Test CLI session list
run: praisonai session list
continue-on-error: true
- name: Test CLI queue list
run: praisonai queue list
continue-on-error: true
# ==================== REAL API TESTS ====================
- name: Real API test - Agent run
if: env.OPENAI_API_KEY != ''
run: |
python -c "
from praisonaiagents import Agent
agent = Agent(name='TestAgent', instructions='Reply with exactly: WINDOWS_OK', llm='gpt-4o-mini')
result = agent.run('Say WINDOWS_OK')
print('Result:', result)
print('Real API test passed')
"
- name: Real API test - Multi-agent
if: env.OPENAI_API_KEY != ''
run: |
python -c "
from praisonaiagents import Agent, Agents, Task
researcher = Agent(name='Researcher', instructions='You research topics', llm='gpt-4o-mini')
writer = Agent(name='Writer', instructions='You write content', llm='gpt-4o-mini')
task = Task(description='Say hello world', agent=researcher)
agents = Agents(agents=[researcher, writer], tasks=[task])
result = agents.start()
print('Multi-agent result type:', type(result))
print('Multi-agent test passed')
"
+57
View File
@@ -0,0 +1,57 @@
name: Update API Reference
on:
push:
branches: [ "main" ]
paths:
- 'src/praisonai-agents/praisonaiagents/**/*.py'
- 'src/praisonai/praisonai/**/*.py'
- 'src/praisonai-ts/src/**/*.ts'
- '.github/workflows/update-api-md.yml'
permissions:
contents: write
jobs:
update-api-md:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: main
persist-credentials: true
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Generate api.md
run: |
cd $GITHUB_WORKSPACE
python3 src/praisonai/scripts/generate_api_md.py
- name: Check for changes
id: check_changes
run: |
if git diff --quiet api.md; then
echo "changes=false" >> $GITHUB_OUTPUT
echo "No changes to api.md"
else
echo "changes=true" >> $GITHUB_OUTPUT
echo "api.md has been updated"
git diff api.md | head -50
fi
- name: Commit and push changes
if: steps.check_changes.outputs.changes == 'true'
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "docs: auto-update api.md [skip ci]"
file_pattern: 'api.md'
commit_user_name: "github-actions[bot]"
commit_user_email: "41898282+github-actions[bot]@users.noreply.github.com"
commit_author: "github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"
+2 -1
View File
@@ -83,4 +83,5 @@ issue_details.json
# MCP Registry tokens
.mcpregistry_*
*.log
*.log
.internal
+1 -1
View File
@@ -23,7 +23,7 @@
</div>
PraisonAI is a production-ready Multi-AI Agents framework with self-reflection, designed to create AI Agents to automate and solve problems ranging from simple tasks to complex challenges. By integrating PraisonAI Agents, AG2 (Formerly AutoGen), and CrewAI into a low-code solution, it streamlines the building and management of multi-agent LLM systems, emphasising simplicity, customisation, and effective human-agent collaboration.
PraisonAI is a production-ready Multi-AI Agents framework with self-reflection, designed to create AI Agents to automate and solve problems ranging from simple tasks to complex challenges. It provides a low-code solution to streamline the building and management of multi-agent LLM systems, emphasising simplicity, customisation, and effective human-agent collaboration.
<div align="center">
<a href="https://docs.praison.ai">
+441
View File
@@ -0,0 +1,441 @@
# PraisonAI API Reference
This file is auto-generated. Do not edit manually.
Regenerate with: `praisonai docs api-md --write`
# Agents
Types:
```python
from praisonaiagents import Agent, Agents
```
Methods:
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">achat</a>(prompt: str, temperature = 1.0, tools = None, output_json = None, output_pydantic = None, reasoning_steps = False, task_name = None, task_description = None, task_id = None)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">aexecute</a>(task, context = None)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">agent_id</a>()</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">analyze_prompt</a>(prompt: str) -> set</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">arun</a>(prompt: str, **kwargs)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">astart</a>(prompt: str, **kwargs)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">auto_memory</a>()</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">auto_memory</a>(value)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">background</a>()</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">background</a>(value)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">chat</a>(prompt, temperature = 1.0, tools = None, output_json = None, output_pydantic = None, reasoning_steps = False, stream = None, task_name = None, task_description = None, task_id = None, config = None, force_retrieval = False, skip_retrieval = False)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">chat_with_context</a>(message: str, context: 'ContextPack', **kwargs) -> str</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">checkpoints</a>()</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">checkpoints</a>(value)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">clean_json_output</a>(output: str) -> str</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">clear_history</a>()</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">console</a>()</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">context_manager</a>()</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">context_manager</a>(value)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">display_generating</a>(content: str, start_time: float)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">execute</a>(task, context = None)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">execute_tool</a>(function_name, arguments)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">execute_tool_async</a>(function_name: str, arguments: Dict[str, Any]) -> Any</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">from_template</a>(uri: str, config: Optional[Dict[str, Any]] = None, offline: bool = False, **kwargs) -> 'Agent'</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">generate_task</a>() -> 'Task'</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">get_available_tools</a>() -> List[Any]</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">get_memory_context</a>(query: Optional[str] = None) -> str</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">get_recommended_stage</a>(prompt: str) -> str</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">get_rules_context</a>(file_path: Optional[str] = None, include_manual: Optional[List[str]] = None) -> str</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">get_skills_prompt</a>() -> str</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">handoff_to</a>(target_agent: 'Agent', prompt: str, context: Optional[Dict[str, Any]] = None, config: Optional['HandoffConfig'] = None) -> 'HandoffResult'</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">handoff_to_async</a>(target_agent: 'Agent', prompt: str, context: Optional[Dict[str, Any]] = None, config: Optional['HandoffConfig'] = None) -> 'HandoffResult'</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">iter_stream</a>(prompt: str, **kwargs)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">launch</a>(path: str = '/', port: int = 8000, host: str = '0.0.0.0', debug: bool = False, protocol: str = 'http')</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">llm_model</a>()</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">output_style</a>()</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">output_style</a>(value)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">policy</a>()</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">policy</a>(value)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">query</a>(question: str, **kwargs) -> 'RAGResult'</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">rag</a>()</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">rag_query</a>(question: str, **kwargs) -> 'RAGResult'</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">retrieval_config</a>()</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">retrieve</a>(query: str, **kwargs) -> 'ContextPack'</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">rules_manager</a>()</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">run</a>(prompt: str, **kwargs)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">run_autonomous</a>(prompt: str, max_iterations: Optional[int] = None, timeout_seconds: Optional[float] = None)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">session_id</a>() -> Optional[str]</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">skill_manager</a>()</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">start</a>(prompt: str = None, **kwargs)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">store_memory</a>(content: str, memory_type: str = 'short_term', **kwargs)</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">switch_model</a>(new_model: str) -> None</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">thinking_budget</a>()</code>
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">thinking_budget</a>(value)</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">add_task</a>(task)</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">aexecute_task</a>(task_id)</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">append_to_state</a>(key: str, value: Any, max_length: Optional[int] = None) -> List[Any]</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">arun_all_tasks</a>()</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">arun_task</a>(task_id)</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">astart</a>(content = None, return_dict = False, **kwargs)</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">clean_json_output</a>(output: str) -> str</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">clear_state</a>() -> None</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">context_manager</a>()</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">current_plan</a>()</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">default_completion_checker</a>(task, agent_output)</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">delete_state</a>(key: str) -> bool</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">display_token_usage</a>()</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">execute_task</a>(task_id)</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">get_agent_details</a>(agent_name)</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">get_all_state</a>() -> Dict[str, Any]</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">get_all_tasks_status</a>()</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">get_detailed_token_report</a>() -> Dict[str, Any]</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">get_plan_markdown</a>() -> str</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">get_state</a>(key: str, default: Any = None) -> Any</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">get_task_details</a>(task_id)</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">get_task_result</a>(task_id)</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">get_task_status</a>(task_id)</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">get_todo_markdown</a>() -> str</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">get_token_usage_summary</a>() -> Dict[str, Any]</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">has_state</a>(key: str) -> bool</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">increment_state</a>(key: str, amount: float = 1, default: float = 0) -> float</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">launch</a>(path: str = '/agents', port: int = 8000, host: str = '0.0.0.0', debug: bool = False, protocol: str = 'http')</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">restore_session_state</a>(session_id: str) -> bool</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">run</a>(content = None, return_dict = False, **kwargs)</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">run_all_tasks</a>()</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">run_task</a>(task_id)</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">save_output_to_file</a>(task, task_output)</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">save_session_state</a>(session_id: str, include_memory: bool = True) -> None</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">set_state</a>(key: str, value: Any) -> None</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">start</a>(content = None, return_dict = False, output = None, **kwargs)</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">todo_list</a>()</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">update_plan_step_status</a>(step_id: str, status: str) -> bool</code>
* <code title="class Agents">Agents.<a href="./src/praisonai-agents/praisonaiagents/agents/agents.py">update_state</a>(updates: Dict) -> None</code>
# Tools
Types:
```python
from praisonaiagents import Tools, tool
```
Methods:
* <code title="function">praisonaiagents.<a href="./src/praisonai-agents/praisonaiagents/tools/decorator.py">tool</a>(func: Optional[Callable] = None) -> Union[FunctionTool, Callable[[Callable], FunctionTool]]</code>
# Other
Types:
```python
from praisonaiagents import Task
```
Methods:
* <code title="class Task">Task.<a href="./src/praisonai-agents/praisonaiagents/task/task.py">execute_callback</a>(task_output: TaskOutput) -> None</code>
* <code title="class Task">Task.<a href="./src/praisonai-agents/praisonaiagents/task/task.py">execute_callback_sync</a>(task_output: TaskOutput) -> None</code>
* <code title="class Task">Task.<a href="./src/praisonai-agents/praisonaiagents/task/task.py">initialize_memory</a>()</code>
* <code title="class Task">Task.<a href="./src/praisonai-agents/praisonaiagents/task/task.py">store_in_memory</a>(content: str, agent_name: str = None, task_id: str = None)</code>
# Wrapper (praisonai)
Types:
```python
from praisonai import CloudProvider, Deploy, DeployConfig, DeployType, PraisonAI, __version__
```
# CLI
Methods:
* <code title="cli">praisonai <a href="./src/praisonai/praisonai/cli/main.py">--help</a></code>
* <code title="cli">praisonai acp acp-main <a href="./src/praisonai/praisonai/cli/commands/acp.py">--help</a></code>
* <code title="cli">praisonai agents create <a href="./src/praisonai/praisonai/cli/commands/agents.py">--help</a></code>
* <code title="cli">praisonai agents info <a href="./src/praisonai/praisonai/cli/commands/agents.py">--help</a></code>
* <code title="cli">praisonai agents list <a href="./src/praisonai/praisonai/cli/commands/agents.py">--help</a></code>
* <code title="cli">praisonai audit agent-centric <a href="./src/praisonai/praisonai/cli/commands/audit.py">--help</a></code>
* <code title="cli">praisonai benchmark agent <a href="./src/praisonai/praisonai/cli/commands/benchmark.py">--help</a></code>
* <code title="cli">praisonai benchmark benchmark-callback <a href="./src/praisonai/praisonai/cli/commands/benchmark.py">--help</a></code>
* <code title="cli">praisonai benchmark cli <a href="./src/praisonai/praisonai/cli/commands/benchmark.py">--help</a></code>
* <code title="cli">praisonai benchmark compare <a href="./src/praisonai/praisonai/cli/commands/benchmark.py">--help</a></code>
* <code title="cli">praisonai benchmark litellm <a href="./src/praisonai/praisonai/cli/commands/benchmark.py">--help</a></code>
* <code title="cli">praisonai benchmark profile <a href="./src/praisonai/praisonai/cli/commands/benchmark.py">--help</a></code>
* <code title="cli">praisonai benchmark sdk <a href="./src/praisonai/praisonai/cli/commands/benchmark.py">--help</a></code>
* <code title="cli">praisonai benchmark workflow <a href="./src/praisonai/praisonai/cli/commands/benchmark.py">--help</a></code>
* <code title="cli">praisonai call call-main <a href="./src/praisonai/praisonai/cli/commands/call.py">--help</a></code>
* <code title="cli">praisonai chat <a href="./src/praisonai/praisonai/cli/main.py">--help</a></code>
* <code title="cli">praisonai chat chat-main <a href="./src/praisonai/praisonai/cli/commands/chat.py">--help</a></code>
* <code title="cli">praisonai code <a href="./src/praisonai/praisonai/cli/main.py">--help</a></code>
* <code title="cli">praisonai code code-main <a href="./src/praisonai/praisonai/cli/commands/code.py">--help</a></code>
* <code title="cli">praisonai commit commit-main <a href="./src/praisonai/praisonai/cli/commands/commit.py">--help</a></code>
* <code title="cli">praisonai completion bash <a href="./src/praisonai/praisonai/cli/commands/completion.py">--help</a></code>
* <code title="cli">praisonai completion completion-callback <a href="./src/praisonai/praisonai/cli/commands/completion.py">--help</a></code>
* <code title="cli">praisonai completion fish <a href="./src/praisonai/praisonai/cli/commands/completion.py">--help</a></code>
* <code title="cli">praisonai completion zsh <a href="./src/praisonai/praisonai/cli/commands/completion.py">--help</a></code>
* <code title="cli">praisonai config get <a href="./src/praisonai/praisonai/cli/commands/config.py">--help</a></code>
* <code title="cli">praisonai config list <a href="./src/praisonai/praisonai/cli/commands/config.py">--help</a></code>
* <code title="cli">praisonai config path <a href="./src/praisonai/praisonai/cli/commands/config.py">--help</a></code>
* <code title="cli">praisonai config reset <a href="./src/praisonai/praisonai/cli/commands/config.py">--help</a></code>
* <code title="cli">praisonai config set <a href="./src/praisonai/praisonai/cli/commands/config.py">--help</a></code>
* <code title="cli">praisonai context add <a href="./src/praisonai/praisonai/cli/commands/context.py">--help</a></code>
* <code title="cli">praisonai context clear <a href="./src/praisonai/praisonai/cli/commands/context.py">--help</a></code>
* <code title="cli">praisonai context compact <a href="./src/praisonai/praisonai/cli/commands/context.py">--help</a></code>
* <code title="cli">praisonai context export <a href="./src/praisonai/praisonai/cli/commands/context.py">--help</a></code>
* <code title="cli">praisonai context grep <a href="./src/praisonai/praisonai/cli/commands/context.py">--help</a></code>
* <code title="cli">praisonai context list <a href="./src/praisonai/praisonai/cli/commands/context.py">--help</a></code>
* <code title="cli">praisonai context show <a href="./src/praisonai/praisonai/cli/commands/context.py">--help</a></code>
* <code title="cli">praisonai context stats <a href="./src/praisonai/praisonai/cli/commands/context.py">--help</a></code>
* <code title="cli">praisonai context tail <a href="./src/praisonai/praisonai/cli/commands/context.py">--help</a></code>
* <code title="cli">praisonai debug acp <a href="./src/praisonai/praisonai/cli/commands/debug.py">--help</a></code>
* <code title="cli">praisonai debug debug-callback <a href="./src/praisonai/praisonai/cli/commands/debug.py">--help</a></code>
* <code title="cli">praisonai debug interactive <a href="./src/praisonai/praisonai/cli/commands/debug.py">--help</a></code>
* <code title="cli">praisonai debug lsp <a href="./src/praisonai/praisonai/cli/commands/debug.py">--help</a></code>
* <code title="cli">praisonai debug trace <a href="./src/praisonai/praisonai/cli/commands/debug.py">--help</a></code>
* <code title="cli">praisonai deploy aws <a href="./src/praisonai/praisonai/cli/commands/deploy.py">--help</a></code>
* <code title="cli">praisonai deploy azure <a href="./src/praisonai/praisonai/cli/commands/deploy.py">--help</a></code>
* <code title="cli">praisonai deploy docker <a href="./src/praisonai/praisonai/cli/commands/deploy.py">--help</a></code>
* <code title="cli">praisonai deploy gcp <a href="./src/praisonai/praisonai/cli/commands/deploy.py">--help</a></code>
* <code title="cli">praisonai diag diag-callback <a href="./src/praisonai/praisonai/cli/commands/diag.py">--help</a></code>
* <code title="cli">praisonai diag export <a href="./src/praisonai/praisonai/cli/commands/diag.py">--help</a></code>
* <code title="cli">praisonai docs api-md <a href="./src/praisonai/praisonai/cli/commands/docs.py">--help</a></code>
* <code title="cli">praisonai docs generate <a href="./src/praisonai/praisonai/cli/commands/docs.py">--help</a></code>
* <code title="cli">praisonai docs list <a href="./src/praisonai/praisonai/cli/commands/docs.py">--help</a></code>
* <code title="cli">praisonai docs report <a href="./src/praisonai/praisonai/cli/commands/docs.py">--help</a></code>
* <code title="cli">praisonai docs run <a href="./src/praisonai/praisonai/cli/commands/docs.py">--help</a></code>
* <code title="cli">praisonai docs run-all <a href="./src/praisonai/praisonai/cli/commands/docs.py">--help</a></code>
* <code title="cli">praisonai docs serve <a href="./src/praisonai/praisonai/cli/commands/docs.py">--help</a></code>
* <code title="cli">praisonai docs stats <a href="./src/praisonai/praisonai/cli/commands/docs.py">--help</a></code>
* <code title="cli">praisonai doctor cleanup <a href="./src/praisonai/praisonai/cli/commands/doctor.py">--help</a></code>
* <code title="cli">praisonai doctor config <a href="./src/praisonai/praisonai/cli/commands/doctor.py">--help</a></code>
* <code title="cli">praisonai doctor db <a href="./src/praisonai/praisonai/cli/commands/doctor.py">--help</a></code>
* <code title="cli">praisonai doctor doctor-callback <a href="./src/praisonai/praisonai/cli/commands/doctor.py">--help</a></code>
* <code title="cli">praisonai doctor env <a href="./src/praisonai/praisonai/cli/commands/doctor.py">--help</a></code>
* <code title="cli">praisonai doctor mcp <a href="./src/praisonai/praisonai/cli/commands/doctor.py">--help</a></code>
* <code title="cli">praisonai doctor network <a href="./src/praisonai/praisonai/cli/commands/doctor.py">--help</a></code>
* <code title="cli">praisonai doctor performance <a href="./src/praisonai/praisonai/cli/commands/doctor.py">--help</a></code>
* <code title="cli">praisonai doctor selftest <a href="./src/praisonai/praisonai/cli/commands/doctor.py">--help</a></code>
* <code title="cli">praisonai doctor tools <a href="./src/praisonai/praisonai/cli/commands/doctor.py">--help</a></code>
* <code title="cli">praisonai doctor troubleshoot <a href="./src/praisonai/praisonai/cli/commands/doctor.py">--help</a></code>
* <code title="cli">praisonai endpoints list <a href="./src/praisonai/praisonai/cli/commands/endpoints.py">--help</a></code>
* <code title="cli">praisonai endpoints test <a href="./src/praisonai/praisonai/cli/commands/endpoints.py">--help</a></code>
* <code title="cli">praisonai environment check <a href="./src/praisonai/praisonai/cli/commands/environment.py">--help</a></code>
* <code title="cli">praisonai environment doctor <a href="./src/praisonai/praisonai/cli/commands/environment.py">--help</a></code>
* <code title="cli">praisonai environment view <a href="./src/praisonai/praisonai/cli/commands/environment.py">--help</a></code>
* <code title="cli">praisonai eval accuracy <a href="./src/praisonai/praisonai/cli/commands/eval.py">--help</a></code>
* <code title="cli">praisonai eval performance <a href="./src/praisonai/praisonai/cli/commands/eval.py">--help</a></code>
* <code title="cli">praisonai examples info <a href="./src/praisonai/praisonai/cli/commands/examples.py">--help</a></code>
* <code title="cli">praisonai examples list <a href="./src/praisonai/praisonai/cli/commands/examples.py">--help</a></code>
* <code title="cli">praisonai examples report <a href="./src/praisonai/praisonai/cli/commands/examples.py">--help</a></code>
* <code title="cli">praisonai examples run <a href="./src/praisonai/praisonai/cli/commands/examples.py">--help</a></code>
* <code title="cli">praisonai examples run-all <a href="./src/praisonai/praisonai/cli/commands/examples.py">--help</a></code>
* <code title="cli">praisonai examples stats <a href="./src/praisonai/praisonai/cli/commands/examples.py">--help</a></code>
* <code title="cli">praisonai hooks add <a href="./src/praisonai/praisonai/cli/commands/hooks.py">--help</a></code>
* <code title="cli">praisonai hooks list <a href="./src/praisonai/praisonai/cli/commands/hooks.py">--help</a></code>
* <code title="cli">praisonai hooks remove <a href="./src/praisonai/praisonai/cli/commands/hooks.py">--help</a></code>
* <code title="cli">praisonai knowledge add <a href="./src/praisonai/praisonai/cli/commands/knowledge.py">--help</a></code>
* <code title="cli">praisonai knowledge index <a href="./src/praisonai/praisonai/cli/commands/knowledge.py">--help</a></code>
* <code title="cli">praisonai knowledge list <a href="./src/praisonai/praisonai/cli/commands/knowledge.py">--help</a></code>
* <code title="cli">praisonai knowledge search <a href="./src/praisonai/praisonai/cli/commands/knowledge.py">--help</a></code>
* <code title="cli">praisonai lsp logs <a href="./src/praisonai/praisonai/cli/commands/lsp.py">--help</a></code>
* <code title="cli">praisonai lsp lsp-callback <a href="./src/praisonai/praisonai/cli/commands/lsp.py">--help</a></code>
* <code title="cli">praisonai lsp start <a href="./src/praisonai/praisonai/cli/commands/lsp.py">--help</a></code>
* <code title="cli">praisonai lsp status <a href="./src/praisonai/praisonai/cli/commands/lsp.py">--help</a></code>
* <code title="cli">praisonai lsp stop <a href="./src/praisonai/praisonai/cli/commands/lsp.py">--help</a></code>
* <code title="cli">praisonai mcp add <a href="./src/praisonai/praisonai/cli/commands/mcp.py">--help</a></code>
* <code title="cli">praisonai mcp describe <a href="./src/praisonai/praisonai/cli/commands/mcp.py">--help</a></code>
* <code title="cli">praisonai mcp list <a href="./src/praisonai/praisonai/cli/commands/mcp.py">--help</a></code>
* <code title="cli">praisonai mcp mcp-callback <a href="./src/praisonai/praisonai/cli/commands/mcp.py">--help</a></code>
* <code title="cli">praisonai mcp remove <a href="./src/praisonai/praisonai/cli/commands/mcp.py">--help</a></code>
* <code title="cli">praisonai mcp run <a href="./src/praisonai/praisonai/cli/commands/mcp.py">--help</a></code>
* <code title="cli">praisonai mcp status <a href="./src/praisonai/praisonai/cli/commands/mcp.py">--help</a></code>
* <code title="cli">praisonai mcp sync <a href="./src/praisonai/praisonai/cli/commands/mcp.py">--help</a></code>
* <code title="cli">praisonai mcp test <a href="./src/praisonai/praisonai/cli/commands/mcp.py">--help</a></code>
* <code title="cli">praisonai mcp tools <a href="./src/praisonai/praisonai/cli/commands/mcp.py">--help</a></code>
* <code title="cli">praisonai memory add <a href="./src/praisonai/praisonai/cli/commands/memory.py">--help</a></code>
* <code title="cli">praisonai memory clear <a href="./src/praisonai/praisonai/cli/commands/memory.py">--help</a></code>
* <code title="cli">praisonai memory search <a href="./src/praisonai/praisonai/cli/commands/memory.py">--help</a></code>
* <code title="cli">praisonai memory show <a href="./src/praisonai/praisonai/cli/commands/memory.py">--help</a></code>
* <code title="cli">praisonai memory status <a href="./src/praisonai/praisonai/cli/commands/memory.py">--help</a></code>
* <code title="cli">praisonai package install <a href="./src/praisonai/praisonai/cli/commands/package.py">--help</a></code>
* <code title="cli">praisonai package list <a href="./src/praisonai/praisonai/cli/commands/package.py">--help</a></code>
* <code title="cli">praisonai package uninstall <a href="./src/praisonai/praisonai/cli/commands/package.py">--help</a></code>
* <code title="cli">praisonai profile imports <a href="./src/praisonai/praisonai/cli/commands/profile.py">--help</a></code>
* <code title="cli">praisonai profile optimize <a href="./src/praisonai/praisonai/cli/commands/profile.py">--help</a></code>
* <code title="cli">praisonai profile profile-callback <a href="./src/praisonai/praisonai/cli/commands/profile.py">--help</a></code>
* <code title="cli">praisonai profile query <a href="./src/praisonai/praisonai/cli/commands/profile.py">--help</a></code>
* <code title="cli">praisonai profile snapshot <a href="./src/praisonai/praisonai/cli/commands/profile.py">--help</a></code>
* <code title="cli">praisonai profile startup <a href="./src/praisonai/praisonai/cli/commands/profile.py">--help</a></code>
* <code title="cli">praisonai profile suite <a href="./src/praisonai/praisonai/cli/commands/profile.py">--help</a></code>
* <code title="cli">praisonai rag chat <a href="./src/praisonai/praisonai/cli/commands/rag.py">--help</a></code>
* <code title="cli">praisonai rag eval <a href="./src/praisonai/praisonai/cli/commands/rag.py">--help</a></code>
* <code title="cli">praisonai rag index <a href="./src/praisonai/praisonai/cli/commands/rag.py">--help</a></code>
* <code title="cli">praisonai rag query <a href="./src/praisonai/praisonai/cli/commands/rag.py">--help</a></code>
* <code title="cli">praisonai rag serve <a href="./src/praisonai/praisonai/cli/commands/rag.py">--help</a></code>
* <code title="cli">praisonai realtime realtime-main <a href="./src/praisonai/praisonai/cli/commands/realtime.py">--help</a></code>
* <code title="cli">praisonai recipe install <a href="./src/praisonai/praisonai/cli/commands/recipe.py">--help</a></code>
* <code title="cli">praisonai recipe list <a href="./src/praisonai/praisonai/cli/commands/recipe.py">--help</a></code>
* <code title="cli">praisonai recipe run <a href="./src/praisonai/praisonai/cli/commands/recipe.py">--help</a></code>
* <code title="cli">praisonai registry list <a href="./src/praisonai/praisonai/cli/commands/registry.py">--help</a></code>
* <code title="cli">praisonai registry serve <a href="./src/praisonai/praisonai/cli/commands/registry.py">--help</a></code>
* <code title="cli">praisonai research research-main <a href="./src/praisonai/praisonai/cli/commands/research.py">--help</a></code>
* <code title="cli">praisonai retrieval index <a href="./src/praisonai/praisonai/cli/commands/retrieval.py">--help</a></code>
* <code title="cli">praisonai retrieval query <a href="./src/praisonai/praisonai/cli/commands/retrieval.py">--help</a></code>
* <code title="cli">praisonai retrieval search <a href="./src/praisonai/praisonai/cli/commands/retrieval.py">--help</a></code>
* <code title="cli">praisonai rules add <a href="./src/praisonai/praisonai/cli/commands/rules.py">--help</a></code>
* <code title="cli">praisonai rules clear <a href="./src/praisonai/praisonai/cli/commands/rules.py">--help</a></code>
* <code title="cli">praisonai rules list <a href="./src/praisonai/praisonai/cli/commands/rules.py">--help</a></code>
* <code title="cli">praisonai run <a href="./src/praisonai/praisonai/cli/main.py">--help</a></code>
* <code title="cli">praisonai run run-main <a href="./src/praisonai/praisonai/cli/commands/run.py">--help</a></code>
* <code title="cli">praisonai schedule delete <a href="./src/praisonai/praisonai/cli/commands/schedule.py">--help</a></code>
* <code title="cli">praisonai schedule describe <a href="./src/praisonai/praisonai/cli/commands/schedule.py">--help</a></code>
* <code title="cli">praisonai schedule list <a href="./src/praisonai/praisonai/cli/commands/schedule.py">--help</a></code>
* <code title="cli">praisonai schedule logs <a href="./src/praisonai/praisonai/cli/commands/schedule.py">--help</a></code>
* <code title="cli">praisonai schedule restart <a href="./src/praisonai/praisonai/cli/commands/schedule.py">--help</a></code>
* <code title="cli">praisonai schedule schedule-callback <a href="./src/praisonai/praisonai/cli/commands/schedule.py">--help</a></code>
* <code title="cli">praisonai schedule start <a href="./src/praisonai/praisonai/cli/commands/schedule.py">--help</a></code>
* <code title="cli">praisonai schedule stats <a href="./src/praisonai/praisonai/cli/commands/schedule.py">--help</a></code>
* <code title="cli">praisonai schedule stop <a href="./src/praisonai/praisonai/cli/commands/schedule.py">--help</a></code>
* <code title="cli">praisonai serve serve-callback <a href="./src/praisonai/praisonai/cli/commands/serve.py">--help</a></code>
* <code title="cli">praisonai serve start <a href="./src/praisonai/praisonai/cli/commands/serve.py">--help</a></code>
* <code title="cli">praisonai serve status <a href="./src/praisonai/praisonai/cli/commands/serve.py">--help</a></code>
* <code title="cli">praisonai serve stop <a href="./src/praisonai/praisonai/cli/commands/serve.py">--help</a></code>
* <code title="cli">praisonai session delete <a href="./src/praisonai/praisonai/cli/commands/session.py">--help</a></code>
* <code title="cli">praisonai session export <a href="./src/praisonai/praisonai/cli/commands/session.py">--help</a></code>
* <code title="cli">praisonai session import <a href="./src/praisonai/praisonai/cli/commands/session.py">--help</a></code>
* <code title="cli">praisonai session list <a href="./src/praisonai/praisonai/cli/commands/session.py">--help</a></code>
* <code title="cli">praisonai session resume <a href="./src/praisonai/praisonai/cli/commands/session.py">--help</a></code>
* <code title="cli">praisonai session show <a href="./src/praisonai/praisonai/cli/commands/session.py">--help</a></code>
* <code title="cli">praisonai skills create <a href="./src/praisonai/praisonai/cli/commands/skills.py">--help</a></code>
* <code title="cli">praisonai skills info <a href="./src/praisonai/praisonai/cli/commands/skills.py">--help</a></code>
* <code title="cli">praisonai skills install <a href="./src/praisonai/praisonai/cli/commands/skills.py">--help</a></code>
* <code title="cli">praisonai skills list <a href="./src/praisonai/praisonai/cli/commands/skills.py">--help</a></code>
* <code title="cli">praisonai skills search <a href="./src/praisonai/praisonai/cli/commands/skills.py">--help</a></code>
* <code title="cli">praisonai skills validate <a href="./src/praisonai/praisonai/cli/commands/skills.py">--help</a></code>
* <code title="cli">praisonai standardise check <a href="./src/praisonai/praisonai/cli/commands/standardise.py">--help</a></code>
* <code title="cli">praisonai standardise fix <a href="./src/praisonai/praisonai/cli/commands/standardise.py">--help</a></code>
* <code title="cli">praisonai standardise init <a href="./src/praisonai/praisonai/cli/commands/standardise.py">--help</a></code>
* <code title="cli">praisonai standardise report <a href="./src/praisonai/praisonai/cli/commands/standardise.py">--help</a></code>
* <code title="cli">praisonai templates create <a href="./src/praisonai/praisonai/cli/commands/templates.py">--help</a></code>
* <code title="cli">praisonai templates list <a href="./src/praisonai/praisonai/cli/commands/templates.py">--help</a></code>
* <code title="cli">praisonai test info <a href="./src/praisonai/praisonai/cli/commands/test.py">--help</a></code>
* <code title="cli">praisonai test interactive <a href="./src/praisonai/praisonai/cli/commands/test.py">--help</a></code>
* <code title="cli">praisonai test run <a href="./src/praisonai/praisonai/cli/commands/test.py">--help</a></code>
* <code title="cli">praisonai todo add <a href="./src/praisonai/praisonai/cli/commands/todo.py">--help</a></code>
* <code title="cli">praisonai todo done <a href="./src/praisonai/praisonai/cli/commands/todo.py">--help</a></code>
* <code title="cli">praisonai todo list <a href="./src/praisonai/praisonai/cli/commands/todo.py">--help</a></code>
* <code title="cli">praisonai tools info <a href="./src/praisonai/praisonai/cli/commands/tools.py">--help</a></code>
* <code title="cli">praisonai tools list <a href="./src/praisonai/praisonai/cli/commands/tools.py">--help</a></code>
* <code title="cli">praisonai tools test <a href="./src/praisonai/praisonai/cli/commands/tools.py">--help</a></code>
* <code title="cli">praisonai traces disable <a href="./src/praisonai/praisonai/cli/commands/traces.py">--help</a></code>
* <code title="cli">praisonai traces enable <a href="./src/praisonai/praisonai/cli/commands/traces.py">--help</a></code>
* <code title="cli">praisonai traces list <a href="./src/praisonai/praisonai/cli/commands/traces.py">--help</a></code>
* <code title="cli">praisonai traces status <a href="./src/praisonai/praisonai/cli/commands/traces.py">--help</a></code>
* <code title="cli">praisonai train train-main <a href="./src/praisonai/praisonai/cli/commands/train.py">--help</a></code>
* <code title="cli">praisonai ui chat <a href="./src/praisonai/praisonai/cli/commands/ui.py">--help</a></code>
* <code title="cli">praisonai ui code <a href="./src/praisonai/praisonai/cli/commands/ui.py">--help</a></code>
* <code title="cli">praisonai ui gradio <a href="./src/praisonai/praisonai/cli/commands/ui.py">--help</a></code>
* <code title="cli">praisonai ui realtime <a href="./src/praisonai/praisonai/cli/commands/ui.py">--help</a></code>
* <code title="cli">praisonai ui ui-main <a href="./src/praisonai/praisonai/cli/commands/ui.py">--help</a></code>
* <code title="cli">praisonai version check <a href="./src/praisonai/praisonai/cli/commands/version.py">--help</a></code>
* <code title="cli">praisonai version show <a href="./src/praisonai/praisonai/cli/commands/version.py">--help</a></code>
* <code title="cli">praisonai version version-callback <a href="./src/praisonai/praisonai/cli/commands/version.py">--help</a></code>
* <code title="cli">praisonai workflow create <a href="./src/praisonai/praisonai/cli/commands/workflow.py">--help</a></code>
* <code title="cli">praisonai workflow list <a href="./src/praisonai/praisonai/cli/commands/workflow.py">--help</a></code>
* <code title="cli">praisonai workflow run <a href="./src/praisonai/praisonai/cli/commands/workflow.py">--help</a></code>
# TypeScript
Types/Exports:
```ts
export { Agent, Agents, PraisonAIAgents, Router } from "./agent";
export type { PraisonAIAgentsConfig, SimpleAgentConfig, SimpleRouteConfig, SimpleRouterConfig } from "./agent";
export { ContextAgent, createContextAgent } from "./agent/context";
export { Handoff, handoff, handoffFilters } from "./agent/handoff";
export { ImageAgent, createImageAgent } from "./agent/image";
export { PromptExpanderAgent, createPromptExpanderAgent } from "./agent/prompt-expander";
export { QueryRewriterAgent, createQueryRewriterAgent } from "./agent/query-rewriter";
export { DeepResearchAgent, createDeepResearchAgent } from "./agent/research";
export { RouterAgent, createRouter, routeConditions } from "./agent/router";
export { // Agent loop
createAgentLoop, // DevTools
enableDevTools, // MCP
createMCP, // Middleware (renamed to avoid conflicts)
createCachingMiddleware, // Models
createModel, // Multimodal
createImagePart, // Next.js
createRouteHandler, // OAuth for MCP
type OAuthClientProvider, // Server adapters
createHttpHandler, // Speech & Transcription
generateSpeech, // Telemetry (AI SDK v6 parity)
configureTelemetry, // Tool Approval (AI SDK v6 parity)
ApprovalManager, // Tools
defineTool, // UI Message (AI SDK v6 parity)
convertToModelMessages, AIAgentStep, AIEmbedManyResult, AIEmbedOptions, AIEmbedResult, AIFilePart, AIGenerateImageOptions, AIGenerateImageResult, AIGenerateObjectOptions, AIGenerateObjectResult, AIGenerateTextOptions, AIGenerateTextResult, AIImagePart, AIMiddleware, AIMiddlewareConfig, AIModelMessage, AISpan, AISpanKind, AISpanOptions, AISpanStatus, AIStreamObjectOptions, AIStreamObjectResult, AIStreamTextOptions, AIStreamTextResult, AITelemetryEvent, AITelemetrySettings, AITextPart, AIToolDefinition, AITracer, AgentLoop, DANGEROUS_PATTERNS, MODEL_ALIASES, SPEECH_MODELS, TRANSCRIPTION_MODELS, ToolApprovalDeniedError, ToolApprovalTimeoutError, aiEmbed, aiEmbedMany, aiGenerateImage, aiGenerateObject, aiGenerateText, aiStreamObject, aiStreamText, applyMiddleware, autoEnableDevTools, base64ToUint8Array, clearAICache, clearEvents, closeAllMCPClients, closeMCPClient, convertToUIMessages, createAILoggingMiddleware, createAISpan, createApprovalResponse, createDangerousPatternChecker, createDevToolsMiddleware, createExpressHandler, createFastifyHandler, createFilePart, createHonoHandler, createMultimodalMessage, createNestHandler, createPagesHandler, createPdfPart, createSystemMessage, createTelemetryMiddleware, createTelemetrySettings, createTextMessage, createTextPart, createToolSet, disableAITelemetry, disableDevTools, enableAITelemetry, functionToTool, getAICacheStats, getApprovalManager, getDevToolsState, getDevToolsUrl, getEvents, getMCPClient, getModel, getTelemetrySettings, getToolsNeedingApproval, getTracer, hasModelAlias, hasPendingApprovals, initOpenTelemetry, isDangerous, isDataUrl, isDevToolsEnabled, isTelemetryEnabled, isUrl, listModelAliases, mcpToolsToAITools, parseModel, pipeUIMessageStreamToResponse, recordEvent, resolveModelAlias, safeValidateUIMessages, setApprovalManager, stopAfterSteps, stopWhen, stopWhenNoToolCalls, toMessageContent, toUIMessageStreamResponse, transcribe, uint8ArrayToBase64, validateUIMessages, withApproval, withSpan, wrapModel } from "./ai";
export { AutoAgents, createAutoAgents } from "./auto";
export { BaseCache, FileCache, MemoryCache, createFileCache, createMemoryCache } from "./cache";
export { CLI_SPEC_VERSION, executeCommand, parseArgs } from "./cli";
export { // Autonomy Mode
AutonomyManager, // Background Jobs
JobQueue, // Checkpoints
CheckpointManager, // Cost Tracker
CostTracker, // External Agents
BaseExternalAgent, // Fast Context
FastContext, // Flow Display
FlowDisplay, // Git Integration
GitManager, // Interactive TUI
InteractiveTUI, // N8N Integration
N8NIntegration, // Repo Map
RepoMap, // Sandbox Executor
SandboxExecutor, // Scheduler
Scheduler, // Slash Commands
SlashCommandHandler, AiderAgent, ClaudeCodeAgent, CodexCliAgent, CommandValidator, CostTokenUsage, DEFAULT_BLOCKED_COMMANDS, DEFAULT_BLOCKED_PATHS, DEFAULT_IGNORE_PATTERNS, DiffViewer, FileCheckpointStorage, FileJobStorage, GeminiCliAgent, GenericExternalAgent, HistoryManager, MODEL_PRICING, MODE_POLICIES, MemoryCheckpointStorage, MemoryJobStorage, StatusDisplay, cliApprovalPrompt, createAutonomyManager, createCheckpointManager, createCostTracker, createDiffViewer, createExternalAgent, createFastContext, createFileCheckpointStorage, createFileJobStorage, createFlowDisplay, createGitManager, createHistoryManager, createInteractiveTUI, createJobQueue, createN8NIntegration, createRepoMap, createSandboxExecutor, createScheduler, createSlashCommandHandler, createStatusDisplay, cronExpressions, estimateTokens, executeSlashCommand, externalAgentAsTool, formatCost, getExternalAgentRegistry, getQuickContext, getRepoTree, isSlashCommand, parseSlashCommand, registerCommand, renderWorkflow, sandboxExec, triggerN8NWebhook } from "./cli/features";
export { createDbAdapter, db, getDefaultDbAdapter, setDefaultDbAdapter } from "./db";
export type { DbAdapter, DbConfig, DbMessage, DbRun, DbTrace } from "./db";
export { MemoryPostgresAdapter, NeonPostgresAdapter, PostgresSessionStorage, createMemoryPostgres, createNeonPostgres, createPostgresSessionStorage } from "./db/postgres";
export { MemoryRedisAdapter, UpstashRedisAdapter, createMemoryRedis, createUpstashRedis } from "./db/redis";
export { SQLiteAdapter, createSQLiteAdapter } from "./db/sqlite";
export { EvalSuite, accuracyEval, performanceEval, reliabilityEval } from "./eval";
export { AgentEventBus, AgentEvents, EventEmitterPubSub, PubSub, createEventBus, createPubSub } from "./events";
export { LLMGuardrail, createLLMGuardrail } from "./guardrails/llm-guardrail";
export { // Computer Use
createComputerUse, ComputerUseClient, createCLIApprovalPrompt, createComputerUseAgent } from "./integrations/computer-use";
export { BaseObservabilityProvider, ConsoleObservabilityProvider, LangfuseObservabilityProvider, MemoryObservabilityProvider, ObservabilityTraceContext, createConsoleObservability, createLangfuseObservability, createMemoryObservability } from "./integrations/observability";
export { // Natural Language Postgres
createNLPostgres, NLPostgresClient, NLPostgresConfig, createPostgresTool } from "./integrations/postgres";
export { // Slack
createSlackBot, SlackBot, parseSlackMessage, verifySlackSignature } from "./integrations/slack";
export { BaseVectorStore, ChromaVectorStore, MemoryVectorStore, PineconeVectorStore, QdrantVectorStore, VectorQueryResult, WeaviateVectorStore, createChromaStore, createMemoryVectorStore, createPineconeStore, createQdrantStore, createWeaviateStore } from "./integrations/vector";
export { BaseVoiceProvider, ElevenLabsVoiceProvider, OpenAIVoiceProvider, createElevenLabsVoice, createOpenAIVoice } from "./integrations/voice";
export { GraphRAG, GraphStore, createGraphRAG } from "./knowledge/graph-rag";
export { BaseReranker, CohereReranker, CrossEncoderReranker, LLMReranker, createCohereReranker, createCrossEncoderReranker, createLLMReranker } from "./knowledge/reranker";
export { // Provider classes
OpenAIProvider, // Provider factory and utilities
createProvider, // Provider registry (extensibility API)
ProviderRegistry, // Types
type LLMProvider, AnthropicProvider, BaseProvider, GoogleProvider, ProviderMessage, ProviderToolDefinition, createProviderRegistry, getAvailableProviders, getDefaultProvider, getDefaultRegistry, hasProvider, isProviderAvailable, listProviders, parseModelString, registerBuiltinProviders, registerProvider, unregisterProvider } from "./llm/providers";
export { ADAPTERS, AISDK_PROVIDERS, COMMUNITY_PROVIDERS, PROVIDER_ALIASES } from "./llm/providers/ai-sdk/types";
export { AutoMemory, AutoMemoryKnowledgeBase, AutoMemoryVectorStore, DEFAULT_POLICIES, createAutoMemory, createLLMSummarizer } from "./memory/auto-memory";
export { FileMemory, createFileMemory } from "./memory/file-memory";
export { Memory, createMemory } from "./memory/memory";
export type { MemoryConfig, MemoryEntry } from "./memory/memory";
export { // Adapters
NoopObservabilityAdapter, // Constants
OBSERVABILITY_TOOLS, // Global adapter management
setObservabilityAdapter, // Types
type SpanKind, ConsoleObservabilityAdapter, MemoryObservabilityAdapter, clearAdapterCache, createConsoleAdapter, createMemoryAdapter, createObservabilityAdapter, getObservabilityAdapter, getObservabilityToolInfo, hasObservabilityToolEnvVar, listObservabilityTools, noopAdapter, resetObservabilityAdapter, trace } from "./observability";
export { Plan, PlanStep, PlanStorage, PlanningAgent, TaskAgent, TodoItem, TodoList, createPlan, createPlanStorage, createPlanningAgent, createTaskAgent, createTodoList } from "./planning";
export { SkillManager, createSkillManager, parseSkillFile } from "./skills";
export { AgentTelemetry, TelemetryCollector, cleanupTelemetry, createAgentTelemetry, disableTelemetry, enableTelemetry, getTelemetry } from "./telemetry";
export { BaseTool, FunctionTool, ToolRegistry, ToolResult, ToolValidationError, createTool, getRegistry, getTool, registerTool, tool, validateTool } from "./tools";
export { airweaveSearch, bedrockBrowserClick, bedrockBrowserFill, bedrockBrowserNavigate, bedrockCodeInterpreter, codeExecution, codeMode, createCustomTool, exaSearch, firecrawlCrawl, firecrawlScrape, parallelSearch, perplexitySearch, registerCustomTool, registerLocalTool, registerNpmTool, superagentGuard, superagentRedact, superagentVerify, tavilyCrawl, tavilyExtract, tavilySearch, valyuBioSearch, valyuCompanyResearch, valyuEconomicsSearch, valyuFinanceSearch, valyuPaperSearch, valyuPatentSearch, valyuSecSearch, valyuWebSearch } from "./tools/builtins";
export { MissingDependencyError, MissingEnvVarError, ToolsRegistry, composeMiddleware, createLoggingMiddleware, createRateLimitMiddleware, createRedactionMiddleware, createRetryMiddleware, createTimeoutMiddleware, createToolsRegistry, createTracingMiddleware, createValidationMiddleware, getToolsRegistry, resetToolsRegistry } from "./tools/registry";
export type { InstallHints, PraisonTool, RedactionHooks, RegisteredTool, ToolCapabilities, ToolExecutionContext, ToolExecutionResult, ToolFactory, ToolHooks, ToolInstallStatus, ToolLimits, ToolLogger, ToolMetadata, ToolMiddleware, ToolParameterProperty, ToolParameterSchema } from "./tools/registry";
export { registerBuiltinTools, tools } from "./tools/tools";
export { Workflow, loop, parallel, repeat, route } from "./workflows";
export type { StepResult, WorkflowContext, WorkflowStep } from "./workflows";
export { createWorkflowFromYAML, loadWorkflowFromFile, parseYAMLWorkflow, validateWorkflowDefinition } from "./workflows/yaml-parser";
```
# Optional Plugins
External tools are available via `praisonai-tools` package:
```bash
pip install praisonai-tools
```
See [PraisonAI-tools](https://github.com/MervinPraison/PraisonAI-tools) for available tools.
+1 -1
View File
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
# Install Python packages (using latest versions)
RUN pip install --no-cache-dir \
praisonai_tools \
"praisonai>=3.6.2" \
"praisonai>=3.9.2" \
"praisonai[chat]" \
"embedchain[github,youtube]"
+1 -1
View File
@@ -20,7 +20,7 @@ RUN mkdir -p /root/.praison
# Install Python packages (using latest versions)
RUN pip install --no-cache-dir \
praisonai_tools \
"praisonai>=3.6.2" \
"praisonai>=3.9.2" \
"praisonai[ui]" \
"praisonai[chat]" \
"praisonai[realtime]" \
+1 -1
View File
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
# Install Python packages (using latest versions)
RUN pip install --no-cache-dir \
praisonai_tools \
"praisonai>=3.6.2" \
"praisonai>=3.9.2" \
"praisonai[ui]" \
"praisonai[crewai]"
+96
View File
@@ -0,0 +1,96 @@
# Consolidated Params Examples
Examples demonstrating the **Agent-Centric API** with consolidated parameters.
## Precedence Ladder
```
Instance > Config > Array > Dict > String > Bool > Default
```
## Quick Reference
| Feature | Presets | Example |
|---------|---------|---------|
| `memory` | file, sqlite, redis, postgres, mem0, mongodb | `memory="sqlite"` |
| `output` | minimal, normal, verbose, debug, silent | `output="verbose"` |
| `execution` | fast, balanced, thorough, unlimited | `execution="balanced"` |
| `planning` | reasoning, read_only, auto | `planning="reasoning"` |
| `reflection` | minimal, standard, thorough | `reflection="standard"` |
| `guardrails` | strict, permissive, safety | `guardrails="strict"` |
| `web` | duckduckgo, tavily, google, bing, serper | `web="duckduckgo"` |
| `context` | sliding_window, summarize, truncate | `context="sliding_window"` |
| `autonomy` | suggest, auto_edit, full_auto | `autonomy="suggest"` |
| `caching` | enabled, disabled, prompt | `caching="prompt"` |
## Examples
### Basic Examples (Single Feature)
| File | Feature | Description |
|------|---------|-------------|
| `basic_agent.py` | Agent | Minimal agent with memory |
| `basic_memory.py` | memory | SQLite memory preset |
| `basic_output.py` | output | Verbose output preset |
| `basic_execution.py` | execution | Execution control |
| `basic_guardrails.py` | guardrails | Callable validator |
| `basic_reflection.py` | reflection | Self-reflection preset |
| `basic_web.py` | web | Web search preset |
| `basic_planning.py` | planning | Planning preset |
| `basic_autonomy.py` | autonomy | Autonomy preset |
| `basic_caching.py` | caching | Caching preset |
| `basic_knowledge.py` | knowledge | RAG with sources |
| `basic_context.py` | context | Context management |
| `basic_hooks.py` | hooks | Lifecycle callbacks |
### Multi-Agent Examples
| File | Description |
|------|-------------|
| `basic_agents.py` | Multi-agent with memory and planning |
### Workflow Examples
| File | Description |
|------|-------------|
| `basic_workflow.py` | Simple workflow with output preset |
| `basic_workflow_agentlike.py` | Workflow with agent-like params |
| `basic_step_override.py` | Step-level param overrides |
| `advanced_workflow_full_features.py` | All consolidated params |
### Advanced Examples
| File | Description |
|------|-------------|
| `advanced_output_execution.py` | Combined output + execution |
## Usage Forms
Each consolidated param supports multiple input forms:
```python
# Bool - enable with defaults
memory=True
# String preset
memory="sqlite"
# Dict config
memory={"backend": "sqlite", "user_id": "user123"}
# Config instance
memory=MemoryConfig(backend="sqlite", user_id="user123")
# Array with overrides (for some params)
guardrails=["strict", {"max_retries": 10}]
```
## Running Examples
```bash
# Set API key
export OPENAI_API_KEY=your_key_here
# Run any example
python basic_agent.py
```
@@ -0,0 +1,18 @@
"""
Basic Autonomy Example - Agent-Centric API
Demonstrates autonomy with consolidated params.
Presets: suggest, auto_edit, full_auto
"""
from praisonaiagents import Agent
# Basic: Enable autonomy with preset
agent = Agent(
instructions="You are an autonomous assistant.",
autonomy="suggest", # Presets: suggest, auto_edit, full_auto
)
if __name__ == "__main__":
response = agent.start("Help me organize my project files.")
print(response)
@@ -0,0 +1,23 @@
"""
Basic Caching Example - Agent-Centric API
Demonstrates caching with consolidated params.
Presets: enabled, disabled, prompt
"""
from praisonaiagents import Agent
# Basic: Enable caching with preset
agent = Agent(
instructions="You are a helpful assistant with caching enabled.",
caching="prompt", # Presets: enabled, disabled, prompt
)
if __name__ == "__main__":
# First call - will cache
response = agent.start("What is the capital of France?")
print(response)
# Second call - may use cache
response = agent.start("What is the capital of France?")
print(response)
@@ -0,0 +1,18 @@
"""
Basic Context Example - Agent-Centric API
Demonstrates context management with consolidated params.
Presets: sliding_window, summarize, truncate
"""
from praisonaiagents import Agent
# Basic: Enable context management with preset
agent = Agent(
instructions="You are a helpful assistant with context management.",
context="sliding_window", # Presets: sliding_window, summarize, truncate
)
if __name__ == "__main__":
response = agent.start("Tell me a long story about a dragon.")
print(response)
@@ -0,0 +1,28 @@
"""
Basic Hooks Example - Agent-Centric API
Demonstrates lifecycle hooks with consolidated params.
Valid keys: on_step, on_tool_call, middleware
"""
from praisonaiagents import Agent
# Define hook callbacks
def on_step_callback(step_info):
print(f"Step: {step_info}")
def on_tool_call_callback(tool_info):
print(f"Tool called: {tool_info}")
# Basic: Enable hooks with dict
agent = Agent(
instructions="You are a helpful assistant.",
hooks={
"on_step": on_step_callback,
"on_tool_call": on_tool_call_callback,
},
)
if __name__ == "__main__":
response = agent.start("What is 2 + 2?")
print(response)
@@ -0,0 +1,18 @@
"""
Basic Knowledge (RAG) Example - Agent-Centric API
Demonstrates knowledge with consolidated params.
Supports: bool, list of sources, dict config, KnowledgeConfig
"""
from praisonaiagents import Agent
# Basic: Enable knowledge with list of sources
agent = Agent(
instructions="You are a helpful assistant with document knowledge.",
knowledge=["docs/"], # List of file/folder paths
)
if __name__ == "__main__":
response = agent.start("What does the documentation say about installation?")
print(response)
@@ -0,0 +1,18 @@
"""
Basic Planning Example - Agent-Centric API
Demonstrates planning with consolidated params.
Presets: reasoning, read_only, auto
"""
from praisonaiagents import Agent
# Basic: Enable planning with preset
agent = Agent(
instructions="You are a strategic planner.",
planning="reasoning", # Presets: reasoning, read_only, auto
)
if __name__ == "__main__":
response = agent.start("Plan a 3-day trip to Tokyo.")
print(response)
@@ -0,0 +1,18 @@
"""
Basic Reflection Example - Agent-Centric API
Demonstrates reflection (self-reflect) with consolidated params.
Presets: minimal, standard, thorough
"""
from praisonaiagents import Agent
# Basic: Enable reflection with preset
agent = Agent(
instructions="You are a helpful assistant that reflects on your answers.",
reflection="standard", # Presets: minimal, standard, thorough
)
if __name__ == "__main__":
response = agent.start("Explain quantum computing in simple terms.")
print(response)
+18
View File
@@ -0,0 +1,18 @@
"""
Basic Web Search Example - Agent-Centric API
Demonstrates web search with consolidated params.
Presets: duckduckgo, tavily, google, bing, serper, search_only, fetch_only
"""
from praisonaiagents import Agent
# Basic: Enable web search with preset
agent = Agent(
instructions="You are a research assistant with web access.",
web="duckduckgo", # Presets: duckduckgo, tavily, google, bing, serper
)
if __name__ == "__main__":
response = agent.start("What are the latest AI developments in 2024?")
print(response)
@@ -22,7 +22,7 @@ agents: # Canonical: use 'agents' instead of 'roles'
verbose: true
allow_delegation: false
llm:
model: "gpt-5-nano"
model: "gpt-4o-mini"
temperature: 0.3
tools:
- "InternetSearchTool"
@@ -76,7 +76,7 @@ agents: # Canonical: use 'agents' instead of 'roles'
verbose: true
allow_delegation: false
llm:
model: "gpt-5-nano"
model: "gpt-4o-mini"
temperature: 0.3
tools:
- "InternetSearchTool"
@@ -130,7 +130,7 @@ agents: # Canonical: use 'agents' instead of 'roles'
verbose: true
allow_delegation: false
llm:
model: "gpt-5-nano"
model: "gpt-4o-mini"
temperature: 0.3
tools:
- "InternetSearchTool"
@@ -185,7 +185,7 @@ agents: # Canonical: use 'agents' instead of 'roles'
verbose: true
allow_delegation: false
llm:
model: "gpt-5-nano"
model: "gpt-4o-mini"
temperature: 0.4
tools:
- "InternetSearchTool"
@@ -239,7 +239,7 @@ agents: # Canonical: use 'agents' instead of 'roles'
verbose: true
allow_delegation: false
llm:
model: "gpt-5-nano"
model: "gpt-4o-mini"
temperature: 0.3
tools:
- "InternetSearchTool"
@@ -299,7 +299,7 @@ agents: # Canonical: use 'agents' instead of 'roles'
verbose: true
allow_delegation: false
llm:
model: "gpt-5-nano"
model: "gpt-4o-mini"
temperature: 0.3
tasks:
report_synthesis:
+148
View File
@@ -0,0 +1,148 @@
"""
Multi-Server MCP Example.
This example demonstrates how to use multiple MCP servers with PraisonAI Agents,
including mixing MCP tools with regular Python function tools.
Requirements:
- Node.js and npx installed (or uvx for Python-based servers)
- pip install praisonaiagents[mcp]
- OPENAI_API_KEY environment variable set
Usage:
python multi_server.py
"""
from praisonaiagents import Agent, MCP
import os
def calculate(expression: str) -> str:
"""
A simple calculator tool that evaluates mathematical expressions.
Args:
expression: A mathematical expression to evaluate (e.g., "2 + 2")
Returns:
The result of the calculation
"""
try:
# Safe evaluation of mathematical expressions
allowed_chars = set("0123456789+-*/.() ")
if not all(c in allowed_chars for c in expression):
return "Error: Invalid characters in expression"
result = eval(expression)
return f"Result: {result}"
except Exception as e:
return f"Error: {str(e)}"
def main():
# Check for API key
if not os.environ.get("OPENAI_API_KEY"):
print("Please set OPENAI_API_KEY environment variable")
return
print("=" * 60)
print("Multi-Server MCP Example")
print("=" * 60)
# Create MCP instance with Time server
print("\nInitializing MCP servers...")
try:
time_mcp = MCP("uvx mcp-server-time", timeout=30)
print("✓ Time MCP server initialized")
except Exception as e:
print(f"✗ Time MCP server failed: {e}")
return
# Create an agent with multiple tool sources
agent = Agent(
name="MultiToolAgent",
instructions="""You are a helpful assistant with access to multiple tools:
1. Time tools - for getting current time in different timezones
2. Calculator - for mathematical calculations
Use the appropriate tool based on the user's request.""",
llm="openai/gpt-4o-mini",
tools=[time_mcp, calculate] # Mix MCP and regular tools
)
# Show available tools
print("\nAvailable tools:")
formatted_tools = agent._format_tools_for_completion(agent.tools)
for tool in formatted_tools:
print(f" - {tool['function']['name']}: {tool['function']['description'][:50]}...")
# Example queries
queries = [
"What time is it in Tokyo?",
"Calculate 15 * 7 + 23",
"What's the current time in New York and London?"
]
print("\n" + "=" * 60)
print("Running queries...")
print("=" * 60)
for i, query in enumerate(queries, 1):
print(f"\n--- Query {i} ---")
print(f"Question: {query}\n")
try:
response = agent.chat(query)
print(f"Response:\n{response}")
except Exception as e:
print(f"Error: {e}")
print("-" * 40)
# Clean up
time_mcp.shutdown()
print("\nDone!")
def demo_multiple_mcp_servers():
"""
Demonstrate using multiple MCP servers simultaneously.
This function shows how to create agents with multiple MCP servers,
each providing different capabilities.
"""
print("\n" + "=" * 60)
print("Multiple MCP Servers Demo")
print("=" * 60)
# Initialize multiple MCP servers
servers = {}
try:
servers['time'] = MCP("uvx mcp-server-time", timeout=30)
print("✓ Time server initialized")
except Exception as e:
print(f"✗ Time server failed: {e}")
# Create agent with all available servers
available_tools = list(servers.values())
if not available_tools:
print("No MCP servers available")
return
agent = Agent(
name="MultiServerAgent",
instructions="You have access to multiple MCP servers. Use the appropriate tools.",
llm="openai/gpt-4o-mini",
tools=available_tools
)
print(f"\nAgent created with {len(available_tools)} MCP server(s)")
# Clean up
for server in servers.values():
server.shutdown()
if __name__ == "__main__":
main()
+70
View File
@@ -0,0 +1,70 @@
"""
Sequential Thinking MCP Example.
This example demonstrates how to use the Sequential Thinking MCP server
with PraisonAI Agents to break down complex problems step by step.
Requirements:
- Node.js and npx installed
- pip install praisonaiagents[mcp]
- OPENAI_API_KEY environment variable set
Usage:
python sequential_thinking.py
"""
from praisonaiagents import Agent, MCP
import os
def main():
# Check for API key
if not os.environ.get("OPENAI_API_KEY"):
print("Please set OPENAI_API_KEY environment variable")
return
# Create MCP instance with Sequential Thinking server
# The server provides tools for step-by-step problem analysis
sequential_mcp = MCP(
"npx -y @modelcontextprotocol/server-sequential-thinking",
timeout=60
)
# Create an agent with the Sequential Thinking tools
agent = Agent(
name="ProblemSolver",
instructions="""You are an expert problem solver who breaks down complex problems
into manageable steps. When given a problem:
1. Use the sequential thinking tool to analyze it step by step
2. Provide clear, actionable steps
3. Explain your reasoning at each step""",
llm="openai/gpt-4o-mini",
tools=sequential_mcp
)
# Example problems to solve
problems = [
"Break down the process of making a cup of tea",
"How would you plan a weekend trip to a new city?",
"What steps would you take to learn a new programming language?"
]
print("=" * 60)
print("Sequential Thinking MCP Example")
print("=" * 60)
for i, problem in enumerate(problems, 1):
print(f"\n--- Problem {i} ---")
print(f"Question: {problem}\n")
response = agent.chat(problem)
print(f"Response:\n{response}")
print("-" * 40)
# Clean up
sequential_mcp.shutdown()
print("\nDone!")
if __name__ == "__main__":
main()
@@ -46,7 +46,7 @@ agent = RouterAgent(
name="Smart Assistant",
role="Adaptive AI Assistant",
goal="Complete tasks using the most appropriate model",
models=["gpt-5-nano", "gpt-5-nano", "claude-3-5-sonnet-20241022"],
models=["gpt-4o-mini", "gpt-4o-mini", "claude-3-5-sonnet-20241022"],
routing_strategy="auto" # Automatic model selection
)
@@ -90,7 +90,7 @@ analyzer = RouterAgent(
goal="Analyze data efficiently",
models={
"gemini/gemini-1.5-flash": {},
"gpt-5-nano": {},
"gpt-4o-mini": {},
"claude-3-haiku-20240307": {}
},
model_router=router,
@@ -207,7 +207,7 @@ for i, agent in enumerate(auto.agents):
name=agent.name,
role=agent.role,
goal=agent.goal,
models=["gpt-5-nano", "gpt-5-nano", "claude-3-5-sonnet"],
models=["gpt-4o-mini", "gpt-4o-mini", "claude-3-5-sonnet"],
routing_strategy="auto"
)
+1 -1
View File
@@ -97,7 +97,7 @@ researcher = Agent(
name="Researcher",
role="Information Researcher",
goal="Store information with semantic understanding",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# Create task
+1 -1
View File
@@ -17,7 +17,7 @@ def my_function():
agent = Agent(
instructions="You are a helpful assistant",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
with track_api_call("agent_request"):
+1 -1
View File
@@ -13,7 +13,7 @@ agent = Agent(
role="Assistant",
goal="Help users",
backstory="You are helpful.",
llm="gpt-5-nano",
llm="gpt-4o-mini",
metrics=True # 🎯 That's it!
)
@@ -177,7 +177,7 @@ Set your API keys:
```bash
export OPENAI_API_KEY="your-api-key"
export OPENAI_MODEL_NAME="gpt-5-nano"
export OPENAI_MODEL_NAME="gpt-4o-mini"
```
## 📝 Customization
@@ -38,7 +38,7 @@ def main():
# Create agent
agent = Agent(
instructions="You are a helpful assistant that explains scientific concepts simply",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# Track the API call performance
@@ -13,8 +13,8 @@ def main():
role="Research Specialist",
goal="Find information about a topic",
backstory="You are an expert researcher who finds comprehensive information.",
verbose=True,
llm="gpt-5-nano", # Use a cost-effective model for examples
output="verbose",
llm="gpt-4o-mini", # Use a cost-effective model for examples
metrics=True # 🎯 SIMPLIFIED: Just add this parameter!
)
@@ -29,7 +29,7 @@ def main():
agents = Agents(
agents=[research_agent],
tasks=[research_task],
verbose=True
output="verbose"
)
print("🚀 Running agent task...")
@@ -54,7 +54,7 @@ def create_agents():
goal="Gather comprehensive information on renewable energy",
backstory="Expert researcher with deep knowledge of energy sector",
instructions="You are a thorough researcher. Provide detailed, fact-based information.",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
analyst = Agent(
@@ -63,7 +63,7 @@ def create_agents():
goal="Analyze trends and patterns in renewable energy data",
backstory="Experienced data analyst specializing in energy markets",
instructions="You are an analytical thinker. Focus on trends, patterns, and insights.",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
writer = Agent(
@@ -72,7 +72,7 @@ def create_agents():
goal="Create clear, compelling reports",
backstory="Professional technical writer with expertise in energy sector",
instructions="You are a skilled writer. Create clear, well-structured reports.",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
return researcher, analyst, writer
@@ -104,8 +104,7 @@ def main():
workflow = Agents(
agents=[researcher, analyst, writer],
tasks=[research_task, analysis_task, report_task],
process="sequential",
verbose=True
process="sequential"
)
result = workflow.start()
@@ -11,7 +11,7 @@ Shows how to monitor agent performance when using external tools.
"""
from praisonaiagents import Agent
from praisonaiagents.tools import duckduckgo, wikipedia
from praisonaiagents.tools import duckduckgo, wikipedia_tools
from praisonaiagents.telemetry import (
monitor_function, track_api_call, get_api_stats,
get_slowest_functions, performance_monitor
@@ -48,8 +48,8 @@ def main():
instructions="""You are a research assistant that helps find information.
Use the available tools to search for information and provide comprehensive answers.
Be thorough in your research and cite your sources.""",
llm="gpt-5-nano",
tools=[duckduckgo, wikipedia] # Add search tools
llm="gpt-4o-mini",
tools=[duckduckgo, wikipedia_tools] # Add search tools
)
# Test queries to demonstrate tool performance monitoring
@@ -97,7 +97,9 @@ def main():
# Slowest functions analysis
print("\n🐌 Slowest Functions:")
slowest = get_slowest_functions()
for func_name, avg_time in slowest:
for item in slowest[:5]:
func_name = item.get('function', 'unknown')
avg_time = item.get('average_time', 0)
print(f" {func_name}: {avg_time:.3f}s average")
# Real-time performance data
@@ -54,7 +54,7 @@ async def run_async_agent(query: str):
# Create agent
agent = Agent(
instructions="You are an efficient AI assistant that processes requests quickly",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# Track the agent execution
@@ -60,7 +60,7 @@ def run_robust_agent(query: str, with_error_simulation: bool = True):
instructions="""You are a resilient AI assistant.
Even when facing challenges, you provide helpful responses.
If you encounter errors, acknowledge them and offer alternatives.""",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
try:
@@ -65,7 +65,7 @@ def run_stateful_agent(query: str, session_id: str, conversation_turn: int):
instructions="""You are a memory-enabled AI assistant. You can remember
previous conversations and build on past interactions. Use your memory
to provide personalized and contextual responses.""",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# Pre-processing: retrieve relevant memories
@@ -165,7 +165,9 @@ def main():
# Slowest operations
print("\n🐌 Slowest Memory Operations:")
slowest = get_slowest_functions()
for func_name, avg_time in slowest[:5]: # Top 5 slowest
for item in slowest[:5]: # Top 5 slowest
func_name = item.get('function', 'unknown')
avg_time = item.get('average_time', 0)
print(f" {func_name}: {avg_time:.3f}s average")
# Memory operation breakdown
@@ -77,7 +77,7 @@ def run_hierarchical_workflow():
backstory="Experienced project manager with expertise in team coordination",
instructions="""You are a project manager. Break down complex projects into tasks,
assign them to appropriate team members, and coordinate the overall effort.""",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# Create worker agents
@@ -87,7 +87,7 @@ def run_hierarchical_workflow():
goal="Conduct thorough research on assigned topics",
backstory="Expert researcher with deep analytical skills",
instructions="You are a research specialist. Provide detailed, factual information.",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
analysis_worker = Agent(
@@ -96,7 +96,7 @@ def run_hierarchical_workflow():
goal="Analyze data and identify patterns and insights",
backstory="Experienced data analyst with strong analytical thinking",
instructions="You are a data analyst. Focus on patterns, trends, and insights.",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
writing_worker = Agent(
@@ -105,7 +105,7 @@ def run_hierarchical_workflow():
goal="Create clear, well-structured documentation and reports",
backstory="Professional writer specializing in technical content",
instructions="You are a technical writer. Create clear, comprehensive reports.",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# Project tasks
@@ -180,7 +180,7 @@ def run_agent_with_custom_tools():
instructions="""You are a technical assistant with access to various tools.
Use the appropriate tools to help with data processing, API calls, database queries,
and file operations. Always explain what tools you're using and why.""",
llm="gpt-5-nano",
llm="gpt-4o-mini",
tools=[process_data, call_external_api, query_database, file_operation]
)
@@ -263,7 +263,7 @@ def main():
# Slowest operations
print("\n🐌 Slowest Tool Operations:")
slowest = get_slowest_functions()
tool_slowest = [(name, time) for name, time in slowest if 'tool' in name.lower()]
tool_slowest = [(item.get('function', 'unknown'), item.get('average_time', 0)) for item in slowest if 'tool' in item.get('function', '').lower()]
for tool_name, avg_time in tool_slowest[:3]:
print(f" {tool_name}: {avg_time:.3f}s")
@@ -60,7 +60,7 @@ def get_real_time_agent_response(query: str, response_id: int):
# Create a lightweight agent for streaming
agent = Agent(
instructions="You are a real-time assistant. Provide quick, helpful responses.",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
with track_api_call(f"streaming_response_{response_id}"):
@@ -42,19 +42,19 @@ def collect_comprehensive_data():
agents = {
'quick_agent': Agent(
instructions="You are a quick-response agent. Provide concise answers.",
llm="gpt-5-nano"
llm="gpt-4o-mini"
),
'analytical_agent': Agent(
instructions="You are an analytical agent. Provide detailed analysis.",
llm="gpt-5-nano"
llm="gpt-4o-mini"
),
'research_agent': Agent(
instructions="You are a research agent. Provide comprehensive information.",
llm="gpt-5-nano"
llm="gpt-4o-mini"
),
'creative_agent': Agent(
instructions="You are a creative agent. Provide imaginative responses.",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
}
@@ -98,19 +98,19 @@ def simulate_complex_workflow():
planner = Agent(
name="planner",
instructions="You are a project planner. Break down tasks and plan execution.",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
executor = Agent(
name="executor",
instructions="You are a task executor. Complete assigned tasks efficiently.",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
reviewer = Agent(
name="reviewer",
instructions="You are a quality reviewer. Review and improve work.",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# Create workflow tasks
@@ -302,13 +302,17 @@ def display_dashboard(dashboard_data, benchmark_results):
perf_data = dashboard_data['performance_data']
print(" Fastest Functions:")
fastest_functions = sorted(perf_data['slowest_functions'], key=lambda x: x[1])[:3]
for name, time in fastest_functions:
print(f" {name}: {time:.3f}s")
fastest_functions = sorted(perf_data['slowest_functions'], key=lambda x: x.get('average_time', float('inf')))[:3]
for item in fastest_functions:
name = item.get('function', 'unknown')
time_val = item.get('average_time', 0)
print(f" {name}: {time_val:.3f}s")
print(" Slowest Functions:")
for name, time in perf_data['slowest_functions'][:3]:
print(f" {name}: {time:.3f}s")
for item in perf_data['slowest_functions'][:3]:
name = item.get('function', 'unknown')
time_val = item.get('average_time', 0)
print(f" {name}: {time_val:.3f}s")
# Benchmark Results
print(f"\n⚡ BENCHMARK RESULTS")
-279
View File
@@ -1,279 +0,0 @@
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Project Overview
PraisonAI Agents is a hierarchical AI agent framework for completing complex tasks with self-reflection capabilities. It supports multi-agent collaboration, tool integration, and various execution patterns (sequential, hierarchical, parallel).
## Development Commands
### Installation and Setup
```bash
# Install core package
pip install -e .
# Install with specific features
pip install -e .[all] # All features
pip install -e .[memory] # Memory capabilities
pip install -e .[knowledge] # Document processing
pip install -e .[mcp] # MCP server support
pip install -e .[llm] # Extended LLM support
pip install -e .[api] # API server capabilities
```
### Testing
```bash
# Run individual test examples (no formal test runner configured)
python tests/basic-agents.py
python tests/async_example.py
python tests/knowledge-agents.py
# Test specific features
python tests/mcp-agents.py # MCP integration
python tests/memory_example.py # Memory functionality
python tests/tools_example.py # Tool system
```
### Running Examples
```bash
# Basic agent usage
python tests/single-agent.py
# Multi-agent workflows
python tests/multi-agents-api.py
# Async operations
python tests/async_example_full.py
# MCP server examples
python tests/mcp-sse-direct-server.py # Start MCP server
python tests/mcp-sse-direct-client.py # Connect to server
```
## Core Architecture
### Agent System (`praisonaiagents/agent/`)
- **Agent**: Core agent class with LLM integration, tool calling, and self-reflection
- **ImageAgent**: Specialized multimodal agent for image processing
- Self-reflection with configurable min/max iterations (default: 1-3)
- Delegation support for hierarchical agent structures
### Multi-Agent Orchestration (`praisonaiagents/agents/`)
- **PraisonAIAgents**: Main orchestrator for managing multiple agents and tasks
- **AutoAgents**: Automatic agent creation and management
- Process types: `sequential`, `hierarchical`, `parallel`
- Context passing between agents and task dependency management
### Task System (`praisonaiagents/task/`)
- **Task**: Core task definition with context, callbacks, and output specifications
- Supports file output, JSON/Pydantic structured output, async execution
- Conditional logic with `condition` parameter for task flow control
- Context passing via `context` parameter for task dependencies
- **Guardrails**: Built-in validation and safety mechanisms for task outputs
- Function-based guardrails for custom validation logic
- LLM-based guardrails using natural language descriptions
- Automatic retry with configurable `max_retries` parameter
- Compatible with CrewAI guardrail patterns
### LLM Integration (`praisonaiagents/llm/`)
- Unified wrapper for multiple LLM providers via LiteLLM
- Supports OpenAI, Anthropic, Gemini, DeepSeek, local models (Ollama)
- Context length management and tool calling capabilities
- Set via `llm` parameter on agents or global `OPENAI_API_KEY`/`ANTHROPIC_API_KEY`
### Tool System (`praisonaiagents/tools/`)
Two implementation patterns:
1. **Function-based**: Simple tools using `@tool` decorator
2. **Class-based**: Complex tools inheriting from `BaseTool`
Built-in tools include: DuckDuckGo search, file operations, calculator, Wikipedia, arXiv, data analysis tools, shell execution.
### Memory & Knowledge Systems
- **Memory** (`praisonaiagents/memory/`): Multi-layered memory with RAG support
- Types: short-term, long-term, entity, user memory
- Providers: ChromaDB, Mem0, custom implementations
- **Knowledge** (`praisonaiagents/knowledge/`): Document processing with chunking
- Chunking strategies via `chonkie` library
- Embedding and retrieval capabilities
### MCP (Model Context Protocol) Integration
- **MCP Server**: Server-side tool protocol for distributed execution
- **SSE Support**: Server-sent events for real-time communication
- Tool discovery and dynamic registration
## Development Patterns
### Agent Creation
```python
agent = Agent(
name="Agent Name",
role="Agent Role",
goal="Agent Goal",
backstory="Agent Background",
llm="gpt-5-nano", # or other LLM
self_reflect=True, # Enable self-reflection
min_reflect=1, # Minimum reflection iterations
max_reflect=3, # Maximum reflection iterations
tools=[tool1, tool2], # Optional tools
guardrail=validate_function, # Agent-level guardrail (function or string)
max_guardrail_retries=3 # Retry limit for guardrail failures
)
```
### Task Definition
```python
task = Task(
name="task_name",
description="Task description",
expected_output="Expected output format",
agent=agent,
context=[previous_task], # Task dependencies
output_pydantic=ResponseModel, # Structured output
condition="condition_function" # Conditional execution
)
```
### Guardrails Usage
#### Task-Level Guardrails
```python
from typing import Tuple, Any
from praisonaiagents import GuardrailResult
# Function-based guardrail (Option 1: Using GuardrailResult)
def validate_output(task_output: TaskOutput) -> GuardrailResult:
"""Custom validation function returning GuardrailResult."""
if "error" in task_output.raw.lower():
return GuardrailResult(
success=False,
result=None,
error="Output contains errors"
)
if len(task_output.raw) < 10:
return GuardrailResult(
success=False,
result=None,
error="Output is too short"
)
return GuardrailResult(
success=True,
result=task_output,
error=""
)
# Function-based guardrail (Option 2: Using Tuple[bool, Any])
def validate_output_tuple(task_output: TaskOutput) -> Tuple[bool, Any]:
"""Custom validation function returning tuple."""
if "error" in task_output.raw.lower():
return False, "Output contains errors"
if len(task_output.raw) < 10:
return False, "Output is too short"
return True, task_output
task = Task(
description="Write a professional email",
expected_output="A well-formatted email",
agent=agent,
guardrail=validate_output, # Function-based guardrail
max_retries=3 # Retry up to 3 times if guardrail fails
)
# LLM-based guardrail
task = Task(
description="Generate marketing copy",
expected_output="Professional marketing content",
agent=agent,
guardrail="Ensure the content is professional, engaging, and free of errors", # String description
max_retries=2
)
```
#### Agent-Level Guardrails
```python
# Agent guardrails apply to ALL outputs from that agent
# Option 1: Using GuardrailResult
def validate_professional_tone(task_output: TaskOutput) -> GuardrailResult:
"""Ensure professional tone in all agent responses."""
content = task_output.raw.lower()
casual_words = ['yo', 'dude', 'awesome', 'cool']
for word in casual_words:
if word in content:
return GuardrailResult(
success=False,
result=None,
error=f"Unprofessional language detected: {word}"
)
return GuardrailResult(
success=True,
result=task_output,
error=""
)
# Option 2: Using Tuple[bool, Any]
def validate_professional_tone_tuple(task_output: TaskOutput) -> Tuple[bool, Any]:
"""Ensure professional tone in all agent responses."""
content = task_output.raw.lower()
casual_words = ['yo', 'dude', 'awesome', 'cool']
for word in casual_words:
if word in content:
return False, f"Unprofessional language detected: {word}"
return True, task_output
# Agent with function-based guardrail
agent = Agent(
name="BusinessWriter",
instructions="You are a professional business writer",
guardrail=validate_professional_tone, # Function guardrail
max_guardrail_retries=3
)
# Agent with LLM-based guardrail
agent = Agent(
name="ContentWriter",
instructions="You are a content writer",
guardrail="Ensure all responses are professional, accurate, and appropriate for business use", # String guardrail
max_guardrail_retries=2
)
```
### Multi-Agent Workflow
```python
workflow = PraisonAIAgents(
agents=[agent1, agent2],
tasks=[task1, task2],
process="sequential", # or "hierarchical", "parallel"
verbose=True,
manager_agent=manager_agent # For hierarchical process
)
result = workflow.start()
```
### Async Support
All major components support async execution:
```python
result = await workflow.astart()
result = await agent.aexecute(task)
```
## Key Dependencies
- **Core**: `pydantic`, `rich`, `openai`, `mcp`
- **Memory**: `chromadb`, `mem0ai`
- **Knowledge**: `markitdown`, `chonkie`
- **LLM**: `litellm` for unified provider access
- **API**: `fastapi`, `uvicorn` for server capabilities
## Error Handling
- Global error logging via `error_logs` list
- Callback system for real-time error reporting
- Context length exception handling with automatic retry
- Graceful degradation for optional dependencies
## Testing Strategy
The project uses example-driven testing with 100+ test files in `tests/` directory. Each test file demonstrates specific usage patterns and serves as both test and documentation. Run individual examples to test functionality rather than using a formal test runner.
Use conda activate praisonai-agents to activate the environment.
+63
View File
@@ -0,0 +1,63 @@
"""
Streaming Example with Profiling
Key Points:
1. output="stream" preset enables streaming by default
2. stream=True in start() is redundant when using output="stream" (but doesn't hurt)
3. Profiler.enable() MUST be called before profiling works
4. Use Profiler.streaming() context manager for TTFT metrics
"""
from praisonai.profiler import Profiler, profile, profile_imports
# IMPORTANT: Enable profiler BEFORE any profiling operations
Profiler.enable()
# Profile imports
with profile_imports():
from praisonaiagents import Agent
@profile
def main():
agent = Agent(
instructions="You are a helpful assistant",
output="stream" # This already enables streaming
)
print("\nStreaming response:")
print("-" * 40)
# Use Profiler.streaming() to track TTFT (Time To First Token)
with Profiler.streaming("llm_stream") as tracker:
first_chunk = True
for chunk in agent.start("Write a short paragraph about the history of computing"):
if first_chunk:
tracker.first_token() # Mark TTFT
first_chunk = False
tracker.chunk() # Count chunks
print(chunk, end="", flush=True)
print("\n" + "-" * 40)
# Run with profiling blocks
with Profiler.block("full_workflow"):
with Profiler.block("setup"):
pass
with Profiler.block("execution"):
result = main()
with Profiler.block("cleanup"):
pass
# Generate report
Profiler.report()
# Show streaming-specific stats
streaming_records = Profiler.get_streaming_records()
if streaming_records:
print("\nStreaming Metrics:")
for record in streaming_records:
print(f" {record.name}: TTFT={record.ttft_ms:.2f}ms, Total={record.total_ms:.2f}ms, Chunks={record.chunk_count}")
stats = Profiler.get_statistics()
print(f"\nMedian execution time: {stats['p50']:.2f}ms")
@@ -1,98 +1,75 @@
#!/usr/bin/env python3
"""
Agent-level guardrails example.
This example demonstrates how to use guardrails at the Agent level,
This example demonstrates how to use guardrails at the Agent level
which will apply to all tasks executed by that agent.
"""
from typing import Tuple, Any
from praisonaiagents import Agent, TaskOutput
def validate_content_length(task_output: TaskOutput) -> Tuple[bool, Any]:
"""
Validate that task output content meets minimum length requirement.
Args:
task_output: The task output to validate
Returns:
Tuple of (success, result_or_error_message)
"""
if len(task_output.raw) < 50:
return False, "Content too short - must be at least 50 characters"
return True, task_output
def validate_professional_tone(task_output: TaskOutput) -> Tuple[bool, Any]:
"""
Validate that the content has a professional tone.
Args:
task_output: The task output to validate
Returns:
Tuple of (success, result_or_error_message)
"""
content = task_output.raw.lower()
unprofessional_words = ['yo', 'dude', 'awesome', 'cool', 'lol']
for word in unprofessional_words:
if word in content:
return False, f"Content contains unprofessional word: '{word}'"
return True, task_output
def main():
"""Demonstrate Agent-level guardrails with function-based and LLM-based validation."""
print("=== Agent Guardrail Examples ===\n")
# Example 1: Function-based guardrail
print("1. Function-based guardrail (content length validation):")
agent1 = Agent(
name="ContentWriter",
instructions="You are a professional content writer who creates detailed responses",
guardrail=validate_content_length,
max_guardrail_retries=2
name="ContentWriter"
instructions="You are a professional content writer who creates detailed responses"
guardrails=validate_content_length
)
try:
result1 = agent1.start("Write a brief welcome message")
print(f"Result: {result1}\n")
except Exception as e:
print(f"Error: {e}\n")
# Example 2: LLM-based guardrail (string description)
print("2. LLM-based guardrail (professional tone validation):")
agent2 = Agent(
name="BusinessWriter",
instructions="You are a business communication expert",
guardrail="Ensure the content is professional, formal, and suitable for business communication. No casual language or slang.",
max_guardrail_retries=3
instructions="You are a business communication expert"
guardrails="Ensure the content is professional, formal, and suitable for business communication. No casual language or slang."
)
try:
result2 = agent2.start("Write a welcome message for new employees")
print(f"Result: {result2}\n")
except Exception as e:
print(f"Error: {e}\n")
# Example 3: Multiple agents with different guardrails
print("3. Professional tone function-based guardrail:")
agent3 = Agent(
name="ProfessionalWriter",
instructions="Write professional business content",
guardrail=validate_professional_tone,
max_guardrail_retries=2
name="ProfessionalWriter"
instructions="Write professional business content"
guardrails=validate_professional_tone
)
try:
result3 = agent3.start("Write a casual greeting message")
print(f"Result: {result3}\n")
except Exception as e:
print(f"Error: {e}\n")
print("=== Agent Guardrails Demonstration Complete ===")
if __name__ == "__main__":
main()
@@ -21,7 +21,7 @@ article_agent = Agent(
goal="Fetch the user's favorite morning article",
tools=[fetch_favorite_article],
llm="gemini/gemini-2.5-flash-lite-preview-06-17",
verbose=True
output="verbose"
)
news_agent = Agent(
@@ -30,7 +30,7 @@ news_agent = Agent(
goal="Search for trending news in Kenya",
tools=[search_trending_kenya],
llm="gemini/gemini-2.5-flash-lite-preview-06-17",
verbose=True
output="verbose"
)
twitter_agent = Agent(
@@ -39,7 +39,7 @@ twitter_agent = Agent(
goal="Fetch the latest Twitter feed",
tools=[fetch_twitter_feed],
llm="gemini/gemini-2.5-flash-lite-preview-06-17",
verbose=True
output="verbose"
)
aggregator = Agent(
@@ -47,7 +47,7 @@ aggregator = Agent(
role="Result Aggregator",
goal="Aggregate and summarize all results",
llm="gemini/gemini-2.5-flash-lite-preview-06-17",
verbose=True
output="verbose"
)
# Tasks for each agent
@@ -92,7 +92,7 @@ async def main():
agents=[article_agent, news_agent, twitter_agent, aggregator],
tasks=[article_task, news_task, twitter_task, aggregate_task],
process="workflow",
verbose=True
output="verbose"
)
results = await workflow.astart(dict_output=True)
+2 -2
View File
@@ -5,8 +5,8 @@ async def main():
agent = Agent(
instructions="You are a helpful assistant",
llm="gemini/gemini-1.5-flash-8b",
self_reflect=True,
verbose=True
reflection=True,
output="verbose"
)
# Use achat instead of start/chat for async operation
@@ -10,7 +10,7 @@ import os
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
llm_config = {
"model": "gpt-5-nano", # Changed from Gemini to gpt-5-nano for better tool support
"model": "gpt-4o-mini", # Changed from Gemini to gpt-5-nano for better tool support
"temperature": 0.7,
"response_format": {
"type": "text"
@@ -79,7 +79,7 @@ async def run_parallel_searches():
backstory="Expert in web search and information retrieval. You have access to the tavily_search tool to search the web.",
tools=[tavily_search],
llm=llm_config,
verbose=True
output="verbose"
)
# Create summary agent
+1 -1
View File
@@ -52,7 +52,7 @@ agents = AutoAgents(
tools=[evaluate, get_stock_price],
process="sequential",
llm="gemini/gemini-2.5-flash-lite-preview-06-17",
self_reflect=False,
reflection=False,
verbose=False,
max_agents=3 # Maximum number of agents to create
)
+3 -2
View File
@@ -5,8 +5,9 @@ def get_weather(city: str) -> str:
agent = Agent(
instructions="You are a helpful assistant",
llm="gpt-5-nano",
tools=[get_weather]
llm="gpt-4o-mini",
tools=[get_weather],
output="status"
)
agent.start("What is the weather in Tokyo?")
+2 -13
View File
@@ -1,14 +1,3 @@
#!/usr/bin/env python3
from praisonaiagents import Agent
print("Creating agent...")
agent = Agent(
instructions="You are a helpful assistant"
)
print("Starting agent...")
result = agent.start("Why is the sky blue?")
print("Agent completed successfully!")
print(f"Result: {result}")
agent = Agent(instructions="You are a helpful assistant")
result = agent.start("Why is the sky blue?")
@@ -1,6 +1,6 @@
# PraisonAI Agents - Real Execution Benchmark
**Generated:** 2025-12-18 14:43:52
**Generated:** 2026-01-13 06:07:28
**Model:** gpt-4o-mini
**Iterations:** 3
**Prompt:** "What is 2+2? Reply with just the number."
@@ -9,18 +9,18 @@
| Framework | Method | Avg Time | Relative |
|-----------|--------|----------|----------|
| **PraisonAI** | `agent.start()` | **0.45s** | **1.00x (fastest)** |
| PraisonAI (LiteLLM) | `agent.start()` | 0.55s | 1.22x |
| CrewAI | `crew.kickoff()` | 0.58s | 1.28x |
| Agno | `agent.run()` | 0.92s | 2.05x |
| **PraisonAI (LiteLLM)** | `agent.start()` | **0.55s** | **1.00x (fastest)** |
| PraisonAI | `agent.start()` | 0.64s | 1.15x |
| CrewAI | `crew.kickoff()` | 0.95s | 1.71x |
| Agno | `agent.run()` | 1.05s | 1.90x |
## Package Versions
| Package | Version |
|---------|--------|
| PraisonAI | 0.1.5 |
| Agno | 2.3.14 |
| CrewAI | 1.6.1 |
| PraisonAI | 0.11.7 |
| Agno | 2.3.25 |
| CrewAI | 1.8.0 |
## How to Reproduce
@@ -1,6 +1,6 @@
# PraisonAI Agents - Real-World Execution Benchmark
**Generated:** 2025-12-18 14:33:49
**Generated:** 2026-01-13 14:42:59
**Model:** gpt-4o-mini
**Iterations:** 3 per test
@@ -8,60 +8,60 @@
| Test | PraisonAI (avg) | Agno (avg) | Difference |
|------|-----------------|------------|------------|
| Single Agent | 2.04s | 3.18s | PraisonAI 1.56x faster |
| 2 Agents | 2.49s | 20.49s | PraisonAI 8.21x faster |
| 3 Agents | 6.52s | 13.97s | PraisonAI 2.14x faster |
| Single Agent | 2.46s | 2.92s | PraisonAI 1.19x faster |
| 2 Agents | 3.21s | 19.39s | PraisonAI 6.05x faster |
| 3 Agents | 8.26s | 15.01s | PraisonAI 1.82x faster |
## Detailed Results
### praisonai_single
- Average: 2.04s
- Min: 1.95s
- Max: 2.19s
- Std Dev: 0.13s
- Times: ['2.19s', '1.95s', '2.00s']
- Average: 2.46s
- Min: 1.93s
- Max: 2.90s
- Std Dev: 0.49s
- Times: ['1.93s', '2.55s', '2.90s']
### agno_single
- Average: 3.18s
- Min: 3.00s
- Max: 3.51s
- Std Dev: 0.28s
- Times: ['3.51s', '3.00s', '3.04s']
- Average: 2.92s
- Min: 2.85s
- Max: 2.97s
- Std Dev: 0.06s
- Times: ['2.97s', '2.85s', '2.94s']
### praisonai_two_agents
- Average: 2.49s
- Min: 2.22s
- Max: 2.65s
- Std Dev: 0.24s
- Times: ['2.65s', '2.22s', '2.62s']
- Average: 3.21s
- Min: 2.80s
- Max: 3.67s
- Std Dev: 0.44s
- Times: ['3.67s', '2.80s', '3.15s']
### agno_two_agents
- Average: 20.49s
- Min: 9.96s
- Max: 35.05s
- Std Dev: 13.02s
- Times: ['16.46s', '9.96s', '35.05s']
- Average: 19.39s
- Min: 10.62s
- Max: 26.06s
- Std Dev: 7.93s
- Times: ['26.06s', '10.62s', '21.50s']
### praisonai_three_agents
- Average: 6.52s
- Min: 5.62s
- Max: 7.39s
- Std Dev: 0.88s
- Times: ['5.62s', '6.54s', '7.39s']
- Average: 8.26s
- Min: 6.44s
- Max: 11.88s
- Std Dev: 3.13s
- Times: ['6.47s', '6.44s', '11.88s']
### agno_three_agents
- Average: 13.97s
- Min: 11.69s
- Max: 15.32s
- Std Dev: 1.99s
- Times: ['14.91s', '11.69s', '15.32s']
- Average: 15.01s
- Min: 14.46s
- Max: 15.90s
- Std Dev: 0.78s
- Times: ['15.90s', '14.46s', '14.68s']
## Package Versions
| Package | Version |
|---------|--------|
| PraisonAI | 0.1.5 |
| Agno | 2.3.14 |
| PraisonAI | 0.11.22 |
| Agno | 2.3.25 |
## How to Reproduce
@@ -52,12 +52,11 @@ def benchmark_praisonai(model, iterations, prompt):
agent = Agent(
name="Calculator",
instructions="You are a helpful assistant. Be very brief.",
llm=model,
verbose=False
llm=model
)
start = time.perf_counter()
response = agent.start(prompt)
response = agent.start(prompt, output="silent")
elapsed = time.perf_counter() - start
times.append(elapsed)
@@ -80,12 +79,11 @@ def benchmark_praisonai_litellm(model, iterations, prompt):
agent = Agent(
name="Calculator",
instructions="You are a helpful assistant. Be very brief.",
llm=f"openai/{model}",
verbose=False
llm=f"openai/{model}"
)
start = time.perf_counter()
response = agent.start(prompt)
response = agent.start(prompt, output="silent")
elapsed = time.perf_counter() - start
times.append(elapsed)
@@ -103,12 +103,11 @@ def benchmark_praisonai_single(iterations: int = 3) -> ExecutionResult:
name="Assistant",
instructions="You are a helpful assistant. Be very brief.",
llm="gpt-4o-mini",
tools=[get_weather],
verbose=False
tools=[get_weather]
)
start = time.perf_counter()
response = agent.start("What's the weather in NYC? Reply in one sentence.")
response = agent.start("What's the weather in NYC? Reply in one sentence.", output="silent")
elapsed = time.perf_counter() - start
result.times.append(elapsed)
@@ -132,8 +131,7 @@ def benchmark_praisonai_two_agents(iterations: int = 3) -> ExecutionResult:
goal="Gather weather information",
instructions="Get weather data and pass to reporter. Be brief.",
llm="gpt-4o-mini",
tools=[get_weather],
verbose=False
tools=[get_weather]
)
reporter = Agent(
@@ -141,18 +139,16 @@ def benchmark_praisonai_two_agents(iterations: int = 3) -> ExecutionResult:
role="News Reporter",
goal="Create weather report",
instructions="Create a one-sentence weather summary.",
llm="gpt-4o-mini",
verbose=False
llm="gpt-4o-mini"
)
agents = Agents(
agents=[researcher, reporter],
process="sequential",
verbose=False
process="sequential"
)
start = time.perf_counter()
response = agents.start("Get NYC weather and create a brief report.")
response = agents.start("Get NYC weather and create a brief report.", output="silent")
elapsed = time.perf_counter() - start
result.times.append(elapsed)
@@ -176,8 +172,7 @@ def benchmark_praisonai_three_agents(iterations: int = 3) -> ExecutionResult:
goal="Get weather data",
instructions="Fetch weather information. Be brief.",
llm="gpt-4o-mini",
tools=[get_weather],
verbose=False
tools=[get_weather]
)
time_agent = Agent(
@@ -186,8 +181,7 @@ def benchmark_praisonai_three_agents(iterations: int = 3) -> ExecutionResult:
goal="Get time information",
instructions="Fetch timezone information. Be brief.",
llm="gpt-4o-mini",
tools=[get_time],
verbose=False
tools=[get_time]
)
summarizer = Agent(
@@ -195,18 +189,16 @@ def benchmark_praisonai_three_agents(iterations: int = 3) -> ExecutionResult:
role="Summary Writer",
goal="Combine information",
instructions="Create a one-sentence summary combining weather and time.",
llm="gpt-4o-mini",
verbose=False
llm="gpt-4o-mini"
)
agents = Agents(
agents=[weather_agent, time_agent, summarizer],
process="sequential",
verbose=False
process="sequential"
)
start = time.perf_counter()
response = agents.start("Get NYC weather and EST time, then summarize.")
response = agents.start("Get NYC weather and EST time, then summarize.", output="silent")
elapsed = time.perf_counter() - start
result.times.append(elapsed)
+1 -1
View File
@@ -1,5 +1,5 @@
from praisonaiagents import ContextAgent
agent = ContextAgent(llm="gpt-5-nano", auto_analyze=False)
agent = ContextAgent(llm="gpt-4o-mini", auto_analyze=False)
agent.start("https://github.com/MervinPraison/PraisonAI/ Need to add Authentication")
+1 -1
View File
@@ -14,7 +14,7 @@ def test_agent_direct():
agent = Agent(
instructions="""You are a helpful assistant that can break down complex problems.
Use the available tools when relevant to perform step-by-step analysis.""",
llm="gpt-5-nano",
llm="gpt-4o-mini",
tools=MCP("npx -y @modelcontextprotocol/server-sequential-thinking")
)
@@ -14,8 +14,8 @@ llm_config = {
agent = Agent(
instructions="You are a helpful assistant. Be concise and clear.",
llm=llm_config,
verbose=True,
self_reflect=True,
output="verbose",
reflection=True,
max_reflect=2,
min_reflect=1
)
@@ -7,8 +7,7 @@ def validate_content(data):
agent = Agent(
instructions="You are a writer",
guardrail=validate_content,
max_guardrail_retries=1
guardrails=validate_content
)
agent.start("Write a welcome message with 5 words")
@@ -12,7 +12,7 @@ agent = Agent(
task = Task(
description="Write a welcome message",
guardrail=validate_content,
guardrails=validate_content,
agent=agent
)
+2 -2
View File
@@ -62,7 +62,7 @@ def main():
description="Write a professional email to a client about project completion",
expected_output="A well-formatted professional email",
agent=agent,
guardrail=email_validator, # Function-based guardrail
guardrails=email_validator, # Function-based guardrail
max_retries=2
)
@@ -110,7 +110,7 @@ Project Team""",
description="Write a marketing email for a new product launch",
expected_output="Engaging marketing content",
agent=agent,
guardrail="Ensure the content is professional, engaging, includes a clear call-to-action, and is free of errors",
guardrails="Ensure the content is professional, engaging, includes a clear call-to-action, and is free of errors",
max_retries=3
)
+1 -1
View File
@@ -9,7 +9,7 @@ agent = Agent(
goal="Search for information from multiple sources",
backstory="I am an AI assistant that can search YouTube and Wikipedia.",
tools=[YouTubeSearchTool, WikipediaAPIWrapper],
self_reflect=False
reflection=False
)
# Create tasks to demonstrate both tools
+2 -2
View File
@@ -32,9 +32,9 @@ agent = Agent(
instructions="You are a helpful Assistant specialized in scientific explanations. "
"Provide clear, accurate, and engaging responses.",
llm=llm_config, # Pass the detailed configuration
verbose=True, # Enable detailed output
output="verbose", # Enable detailed output
markdown=True, # Format responses in markdown
self_reflect=True, # Enable self-reflection
reflection=True, # Enable self-reflection
max_reflect=3, # Maximum reflection iterations
min_reflect=1 # Minimum reflection iterations
)
+1 -1
View File
@@ -3,7 +3,7 @@ from praisonaiagents import Agent, MCP
agent = Agent(
instructions="""You are a helpful assistant that can check stock prices and perform other tasks.
Use the available tools when relevant to answer user questions.""",
llm="gpt-5-nano",
llm="gpt-4o-mini",
tools = MCP("/Users/praison/miniconda3/envs/mcp/bin/python /Users/praison/stockprice/app.py")
)
@@ -71,7 +71,7 @@ def main():
tools=[mongodb_tools],
memory=True,
verbose=True,
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# 2. Knowledge Curator Agent - manages knowledge base
@@ -85,7 +85,7 @@ def main():
knowledge_config=mongodb_knowledge_config,
memory=True,
verbose=True,
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# 3. Business Analyst Agent - analyzes data and provides insights
@@ -99,7 +99,7 @@ def main():
tools=[mongodb_tools],
memory=True,
verbose=True,
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# 4. Customer Service Agent - handles customer interactions
@@ -114,7 +114,7 @@ def main():
tools=[mongodb_tools],
memory=True,
verbose=True,
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# Create comprehensive business tasks
@@ -55,11 +55,9 @@ def main():
retrieve information from a comprehensive MongoDB knowledge base. You excel
at finding relevant information, synthesizing knowledge from multiple sources,
and providing accurate, context-aware responses.""",
knowledge_config=mongodb_knowledge_config,
knowledge=[os.path.join(os.path.dirname(__file__), "llms.md")],
memory=True,
verbose=True,
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# Create a research assistant agent
@@ -72,8 +70,7 @@ def main():
knowledge base. You ensure information is accurate, well-structured,
and properly categorized.""",
memory=True,
verbose=True,
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# Create tasks for knowledge management
@@ -128,7 +125,7 @@ def main():
agents=[research_agent, knowledge_agent],
tasks=knowledge_tasks,
memory=True,
verbose=True
output="verbose"
)
# Execute the knowledge management pipeline
@@ -59,7 +59,7 @@ def main():
knowledge=[os.path.join(os.path.dirname(__file__), "llms.md")],
memory=True,
verbose=True,
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# Create a research assistant agent
@@ -73,7 +73,7 @@ def main():
and properly categorized.""",
memory=True,
verbose=True,
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# Create tasks for knowledge management
+2 -2
View File
@@ -3,7 +3,7 @@ from praisonaiagents.memory import Memory
from praisonaiagents.agent import ContextAgent
import pymongo
context_agent = ContextAgent(llm="gpt-5-nano", auto_analyze=False)
context_agent = ContextAgent(llm="gpt-4o-mini", auto_analyze=False)
context_output = context_agent.start("https://github.com/MervinPraison/PraisonAI/ Need to add Authentication")
@@ -25,7 +25,7 @@ implementation_agent = Agent(
goal="Implement authentication features based on project requirements",
backstory="Expert software implementer specializing in authentication systems, security features, and seamless integration with existing codebases",
memory=True,
llm="gpt-5-nano",
llm="gpt-4o-mini",
)
implementation_task = Task(
@@ -50,7 +50,7 @@ def main():
retrieve relevant information from your persistent memory store.""",
memory=True,
verbose=True,
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# Create research tasks
@@ -38,7 +38,7 @@ def main():
for data management and retrieval.""",
tools=[mongodb_tools],
verbose=True,
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# Create a Data Analysis Agent
@@ -51,7 +51,7 @@ def main():
identify patterns and trends in the stored information.""",
tools=[mongodb_tools],
verbose=True,
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
# Create tasks for MongoDB operations
+7
View File
@@ -0,0 +1,7 @@
from praisonaiagents import Agent, Agents
research_agent = Agent(instructions="Research about AI")
summarise_agent = Agent(instructions="Summarise research agent's findings")
agents = Agents(agents=[research_agent, summarise_agent])
response = agents.start()
+1 -1
View File
@@ -2,6 +2,6 @@ from praisonaiagents import Agent
agent = Agent(
instructions="You are a helpful assistant",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
agent.start("Why sky is Blue?")
+1 -1
View File
@@ -2,7 +2,7 @@ from praisonaiagents import Agent
agent = Agent(
instructions="You are a helpful assistant",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
agent.start("Why sky is Blue?")
agent.start("What was my previous question?")
+1 -1
View File
@@ -3,7 +3,7 @@ import asyncio
agent = Agent(
instructions="You are a helpful assistant",
llm="gpt-5-nano"
llm="gpt-4o-mini"
)
agent.run("Why sea is Blue?")
+1 -1
View File
@@ -22,7 +22,7 @@ def multiply(a: int, b: int) -> int:
agent = Agent(
instructions="You are a helpful assistant. You can use the tools provided to you to help the user.",
llm="gpt-5-nano",
llm="gpt-4o-mini",
tools=[get_stock_price, multiply]
)
+1 -2
View File
@@ -14,8 +14,7 @@ task2 = Task(description="Write a short article about meditation benefits", agen
agents = Agents(
agents=[researcher, writer],
tasks=[task1, task2],
planning=True,
auto_approve_plan=True # Auto-approve the generated plan
planning=True
)
agents.start()
+1 -1
View File
@@ -22,7 +22,7 @@ def multiply(a: int, b: int) -> int:
agent = Agent(
instructions="You are a helpful assistant. You can use the tools provided to you to help the user.",
llm="gpt-5-nano",
llm="gpt-4o-mini",
tools=[get_stock_price, multiply]
)
+196 -166
View File
@@ -5,6 +5,29 @@ This module uses lazy loading to minimize import time and memory usage.
Heavy dependencies like litellm are only loaded when actually needed.
"""
# =============================================================================
# NAMESPACE PACKAGE GUARD
# =============================================================================
# Detect if we're loaded as a namespace package (which indicates stale artifacts
# in site-packages). This happens when there's a praisonaiagents/ directory in
# site-packages without an __init__.py file.
#
# Root cause: Partial uninstall or version mismatch leaves directories that
# Python's PathFinder treats as namespace packages, shadowing the real package.
# =============================================================================
if __file__ is None:
import warnings as _ns_warnings
_ns_warnings.warn(
"praisonaiagents is loaded as a namespace package, which indicates "
"stale artifacts in site-packages. This will cause import errors. "
"Fix: Remove the stale directory with:\n"
" rm -rf $(python -c \"import site; print(site.getsitepackages()[0])\")/praisonaiagents/\n"
"Then reinstall: pip install praisonaiagents",
ImportWarning,
stacklevel=1
)
del _ns_warnings
# Apply warning patch BEFORE any imports to intercept warnings at the source
from . import _warning_patch # noqa: F401
@@ -25,6 +48,14 @@ from .tools.registry import get_registry, register_tool, get_tool, ToolRegistry
from .db import db
from .obs import obs
# Sub-packages for organized imports (pa.config, pa.tools, etc.)
# These enable: import praisonaiagents as pa; pa.config.MemoryConfig
from . import config
from . import tools
from . import memory
from . import workflows
# Note: knowledge and mcp are lazy-loaded via __getattr__ due to heavy deps
# Workflows - lightweight module
from .workflows import (
Workflow, WorkflowStep, WorkflowContext, StepResult,
@@ -68,7 +99,18 @@ from .main import (
)
# Module-level caches for lazy-loaded classes
_lazy_cache = {}
# Using threading.local() ensures thread-safe access to cached values
import threading
_lazy_cache_local = threading.local()
def _get_lazy_cache():
"""Get thread-local lazy cache dict. Thread-safe for concurrent access."""
if not hasattr(_lazy_cache_local, 'cache'):
_lazy_cache_local.cache = {}
return _lazy_cache_local.cache
# Backward-compatible alias (module-level access still works, now thread-safe)
_lazy_cache = {} # Kept for compatibility, but prefer _get_lazy_cache()
# Flags for optional modules
_mcp_available = None
@@ -442,6 +484,51 @@ def __getattr__(name):
_lazy_cache[name] = ContextManager
return ContextManager
# =========================================================================
# Sub-package fallback: delegate to sub-packages for additional symbols
# This enables: from praisonaiagents import duckduckgo, FileMemory, etc.
# =========================================================================
# Try tools sub-package (110+ tool functions)
try:
from . import tools as _tools_module
if hasattr(_tools_module, name):
result = getattr(_tools_module, name)
_lazy_cache[name] = result
return result
except (ImportError, AttributeError):
pass
# Try memory sub-package (FileMemory, RulesManager, etc.)
try:
from . import memory as _memory_module
if hasattr(_memory_module, name):
result = getattr(_memory_module, name)
_lazy_cache[name] = result
return result
except (ImportError, AttributeError):
pass
# Try config sub-package (already handled above, but catch any stragglers)
try:
from . import config as _config_module
if hasattr(_config_module, name):
result = getattr(_config_module, name)
_lazy_cache[name] = result
return result
except (ImportError, AttributeError):
pass
# Try workflows sub-package (Pipeline, Route, etc. - already imported, catch aliases)
try:
from . import workflows as _workflows_module
if hasattr(_workflows_module, name):
result = getattr(_workflows_module, name)
_lazy_cache[name] = result
return result
except (ImportError, AttributeError):
pass
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
@@ -476,174 +563,117 @@ def _init_telemetry():
_init_telemetry()
def warmup(include_litellm: bool = False, include_openai: bool = True) -> dict:
"""
Pre-import heavy dependencies to reduce first-call latency.
NOTE: For default OpenAI usage (llm="gpt-4o-mini"), warmup is NOT needed.
The default path uses the native OpenAI SDK which is fast (~100ms import).
Warmup is only beneficial when using LiteLLM backend, which is triggered by:
- Using "/" in model name (e.g., llm="openai/gpt-4o-mini")
- Passing a dict config (e.g., llm={"model": "gpt-4o-mini"})
- Using base_url parameter
Args:
include_litellm: Pre-import LiteLLM (~2-3s). Only needed for multi-provider support.
include_openai: Pre-import OpenAI SDK (~100ms). Default path, usually fast.
Returns:
dict: Timing information for each component warmed up
Example:
# For LiteLLM multi-provider usage:
from praisonaiagents import warmup
warmup(include_litellm=True) # Pre-load LiteLLM
agent = Agent(llm="anthropic/claude-3-sonnet") # Now faster
# For default OpenAI usage, no warmup needed:
agent = Agent(llm="gpt-4o-mini") # Already fast!
"""
import time
timings = {}
if include_openai:
start = time.perf_counter()
try:
import openai
timings['openai'] = (time.perf_counter() - start) * 1000
except ImportError:
timings['openai'] = -1 # Not available
if include_litellm:
start = time.perf_counter()
try:
import litellm
# Also configure litellm to avoid first-call overhead
litellm.telemetry = False
litellm.set_verbose = False
litellm.drop_params = True
litellm.modify_params = True
timings['litellm'] = (time.perf_counter() - start) * 1000
except ImportError:
timings['litellm'] = -1 # Not available
return timings
# ============================================================================
# PUBLIC API: __all__ (controls IDE autocomplete and `from X import *`)
# ============================================================================
# DESIGN: Keep __all__ minimal for clean IDE experience.
# All 186+ symbols are still accessible via __getattr__ for backwards compat.
# Organized imports available via sub-packages: config, tools, memory, workflows
# ============================================================================
__all__ = [
# Core classes (lazy loaded)
# Core classes - the essentials
'Agent',
'ImageAgent',
'ContextAgent',
'create_context_agent',
'PraisonAIAgents',
'Agents',
'Tools',
'Task',
# Plugin system
'BaseTool',
'ToolResult',
'ToolValidationError',
'validate_tool',
# Tool essentials
'tool',
'FunctionTool',
'ToolRegistry',
'get_registry',
'register_tool',
'get_tool',
'TaskOutput',
'ReflectionOutput',
'AutoAgents',
'AutoRagAgent',
'AutoRagConfig',
'RagRetrievalPolicy',
'Session',
'Memory',
'db',
'obs',
'display_interaction',
'display_self_reflection',
'display_instruction',
'display_tool_call',
'display_error',
'display_generating',
'clean_triple_backticks',
'error_logs',
'register_display_callback',
'sync_display_callbacks',
'async_display_callbacks',
'Knowledge',
'Chunking',
'GuardrailResult',
'LLMGuardrail',
'Handoff',
'handoff',
'handoff_filters',
'RECOMMENDED_PROMPT_PREFIX',
'prompt_with_handoff_instructions',
'HandoffConfig',
'HandoffResult',
'HandoffInputData',
'ContextPolicy',
'HandoffError',
'HandoffCycleError',
'HandoffDepthError',
'HandoffTimeoutError',
'get_telemetry',
'enable_telemetry',
'disable_telemetry',
'enable_performance_mode',
'disable_performance_mode',
'cleanup_telemetry_resources',
'MinimalTelemetry',
'TelemetryCollector',
'DeepResearchAgent',
'DeepResearchResponse',
'Citation',
'ReasoningStep',
'WebSearchCall',
'CodeExecutionStep',
'MCPCall',
'FileSearchCall',
'Provider',
'QueryRewriterAgent',
'RewriteStrategy',
'RewriteResult',
'PromptExpanderAgent',
'ExpandStrategy',
'ExpandResult',
# Workflows
'Workflow',
'WorkflowStep',
'WorkflowContext',
'StepResult',
'Route',
'Parallel',
'Loop',
'Repeat',
'route',
'parallel',
'loop',
'repeat',
'Pipeline',
# MCP
'MCP',
# Flow display
'FlowDisplay',
'track_workflow',
# FastContext
'FastContext',
'FastContextResult',
'FileMatch',
'LineRange',
# Agent Skills
'SkillManager',
'SkillProperties',
'SkillMetadata',
'SkillLoader',
# Planning
'Plan',
'PlanStep',
'TodoList',
'TodoItem',
'PlanStorage',
'PlanningAgent',
'ApprovalCallback',
'READ_ONLY_TOOLS',
'RESTRICTED_TOOLS',
# UI
'AGUI',
'A2A',
# RAG
'RAG',
'RAGConfig',
'RAGResult',
'RAGCitation',
# Feature Configs (agent-centric API)
'MemoryConfig',
'KnowledgeConfig',
'PlanningConfig',
'ReflectionConfig',
'GuardrailConfig',
'WebConfig',
'OutputConfig',
'ExecutionConfig',
'TemplateConfig',
'CachingConfig',
'HooksConfig',
'SkillsConfig',
'AutonomyConfig',
'MemoryBackend',
'ChunkingStrategy',
'GuardrailAction',
'WebSearchProvider',
'OutputPreset',
'ExecutionPreset',
'AutonomyLevel',
# Multi-Agent Feature Configs
'MultiAgentHooksConfig',
'MultiAgentOutputConfig',
'MultiAgentExecutionConfig',
'MultiAgentPlanningConfig',
'MultiAgentMemoryConfig',
# Context Management
'ManagerConfig',
'ContextManager',
# Parameter Resolution (agent-centric API)
'resolve',
'ArrayMode',
'resolve_guardrails',
'resolve_guardrail_policies',
# Presets (agent-centric API)
'GUARDRAIL_PRESETS',
'KNOWLEDGE_PRESETS',
# Parse Utilities
'is_policy_string',
'parse_policy_string',
'Tools',
# Sub-packages for organized imports
# Usage: import praisonaiagents as pa; pa.config.MemoryConfig
'config',
'tools',
'memory',
'workflows',
]
def __dir__():
"""
Return clean list for dir() - matches __all__ plus standard attributes.
This keeps IDE autocomplete clean while preserving full backwards
compatibility via __getattr__ for all 186+ legacy exports.
"""
return list(__all__) + [
# Standard module attributes
'__name__', '__doc__', '__file__', '__path__', '__package__',
'__loader__', '__spec__', '__cached__', '__builtins__',
]
# ============================================================================
# BACKWARDS COMPATIBILITY: Legacy __all__ items (for reference)
# ============================================================================
# All items below are still importable via __getattr__ but NOT in autocomplete:
# - ImageAgent, ContextAgent, create_context_agent, PraisonAIAgents
# - BaseTool, ToolResult, ToolValidationError, validate_tool, FunctionTool
# - ToolRegistry, get_registry, register_tool, get_tool
# - TaskOutput, ReflectionOutput, AutoAgents, AutoRagAgent, AutoRagConfig
# - Session, Memory, db, obs, Knowledge, Chunking
# - GuardrailResult, LLMGuardrail, Handoff, handoff, handoff_filters
# - MemoryConfig, KnowledgeConfig, PlanningConfig, OutputConfig, etc.
# - Workflow, WorkflowStep, Route, Parallel, Loop, Repeat, Pipeline, etc.
# - MCP, FlowDisplay, track_workflow, FastContext, etc.
# - Plan, PlanStep, TodoList, PlanningAgent, ApprovalCallback, etc.
# - RAG, RAGConfig, RAGResult, AGUI, A2A, etc.
# - All telemetry, display, and utility functions
# ============================================================================
@@ -30,6 +30,13 @@ from .prompt_expander_agent import (
ExpandStrategy,
ExpandResult
)
from .protocols import (
AgentProtocol,
RunnableAgentProtocol,
ToolAwareAgentProtocol,
MemoryAwareAgentProtocol,
FullAgentProtocol,
)
__all__ = [
'Agent',
@@ -64,5 +71,11 @@ __all__ = [
'RewriteResult',
'PromptExpanderAgent',
'ExpandStrategy',
'ExpandResult'
'ExpandResult',
# Protocols
'AgentProtocol',
'RunnableAgentProtocol',
'ToolAwareAgentProtocol',
'MemoryAwareAgentProtocol',
'FullAgentProtocol',
]
@@ -44,7 +44,7 @@ _shared_apps = {} # Dict of port -> FastAPI app
if TYPE_CHECKING:
from ..task.task import Task
from ..main import TaskOutput
from ..handoff import Handoff
from .handoff import Handoff, HandoffConfig, HandoffResult
from ..rag.models import RAGResult, ContextPack
class Agent:
@@ -254,10 +254,11 @@ class Agent:
instructions: Optional[str] = None,
# LLM configuration
llm: Optional[Union[str, Any]] = None,
function_calling_llm: Optional[Any] = None,
llm_config: Optional[Dict[str, Any]] = None,
base_url: Optional[str] = None,
api_key: Optional[str] = None,
model: Optional[Union[str, Any]] = None, # Alias for llm=
function_calling_llm: Optional[Any] = None, # DEPRECATED: use llm= with LLMConfig
llm_config: Optional[Dict[str, Any]] = None, # DEPRECATED: use llm= with LLMConfig
base_url: Optional[str] = None, # Kept separate (connection/auth)
api_key: Optional[str] = None, # Kept separate (connection/auth)
# Tools
tools: Optional[List[Any]] = None,
allow_delegation: bool = False,
@@ -290,96 +291,87 @@ class Agent:
"""Initialize an Agent instance.
Args:
name (Optional[str], optional): Name of the agent used for identification and logging.
If None, defaults to "Agent". Defaults to None.
role (Optional[str], optional): Role or job title that defines the agent's expertise
and behavior patterns. Examples: "Data Analyst", "Content Writer". Defaults to None.
goal (Optional[str], optional): Primary objective or goal the agent aims to achieve.
Defines the agent's purpose and success criteria. Defaults to None.
backstory (Optional[str], optional): Background story or context that shapes the agent's
personality and decision-making approach. Defaults to None.
instructions (Optional[str], optional): Direct instructions that override role, goal,
and backstory when provided. Used for simple, task-specific agents. Defaults to None.
llm (Optional[Union[str, Any]], optional): Language model configuration. Can be a model
name string (e.g., "gpt-5-nano", "anthropic/claude-3-sonnet") or a configured LLM object.
Defaults to environment variable OPENAI_MODEL_NAME or "gpt-5-nano".
tools (Optional[List[Any]], optional): List of tools, functions, or capabilities
available to the agent for task execution. Can include callables, tool objects,
or MCP instances. Defaults to None.
function_calling_llm (Optional[Any], optional): Dedicated language model for function
calling operations. If None, uses the main llm parameter. Defaults to None.
max_iter (int, optional): Maximum number of iterations the agent can perform during
task execution to prevent infinite loops. Defaults to 20.
max_rpm (Optional[int], optional): Maximum requests per minute to rate limit API calls
and prevent quota exhaustion. If None, no rate limiting is applied. Defaults to None.
max_execution_time (Optional[int], optional): Maximum execution time in seconds for
agent operations before timeout. If None, no time limit is enforced. Defaults to None.
memory (Optional[Any], optional): Memory system for storing and retrieving information
across conversations. Requires memory dependencies to be installed. Defaults to None.
verbose (bool, optional): Enable detailed logging and status updates during agent
execution for debugging and monitoring. Defaults to True.
allow_delegation (bool, optional): Allow the agent to delegate tasks to other agents
or sub-processes when appropriate. Defaults to False.
step_callback (Optional[Any], optional): Callback function called after each step
of agent execution for custom monitoring or intervention. Defaults to None.
cache (bool, optional): Enable caching of responses and computations to improve
performance and reduce API costs. Defaults to True.
system_template (Optional[str], optional): Custom template for system prompts that
overrides the default system prompt generation. Defaults to None.
prompt_template (Optional[str], optional): Template for formatting user prompts
before sending to the language model. Defaults to None.
response_template (Optional[str], optional): Template for formatting agent responses
before returning to the user. Defaults to None.
allow_code_execution (Optional[bool], optional): Enable the agent to execute code
snippets during task completion. Use with caution for security. Defaults to False.
max_retry_limit (int, optional): Maximum number of retry attempts for failed operations
before giving up. Helps handle transient errors. Defaults to 2.
code_execution_mode (Literal["safe", "unsafe"], optional): Safety mode for code execution.
"safe" restricts dangerous operations, "unsafe" allows full code execution. Defaults to "safe".
embedder_config (Optional[Dict[str, Any]], optional): Configuration dictionary for
text embedding models used in knowledge retrieval and similarity search. Defaults to None.
knowledge (Optional[List[str]], optional): List of knowledge sources (file paths, URLs,
or text content) to be processed and made available to the agent. Defaults to None.
knowledge_config (Optional[Dict[str, Any]], optional): Configuration for knowledge
processing and retrieval system including chunking and indexing parameters. Defaults to None.
use_system_prompt (Optional[bool], optional): Whether to include system prompts in
conversations to establish agent behavior and context. Defaults to True.
markdown (bool, optional): Enable markdown formatting in agent responses for better
readability and structure. Defaults to True.
stream (bool, optional): Enable streaming responses from the language model for real-time
output when using Agent.start() method. Defaults to False for backward compatibility.
metrics (bool, optional): Enable automatic token usage tracking and display summary
when tasks complete. Simplifies token monitoring for cost optimization. Defaults to False.
self_reflect (bool, optional): Enable self-reflection capabilities where the agent
evaluates and improves its own responses. Defaults to False.
max_reflect (int, optional): Maximum number of self-reflection iterations to prevent
excessive reflection loops. Defaults to 3.
min_reflect (int, optional): Minimum number of self-reflection iterations required
before accepting a response as satisfactory. Defaults to 1.
reflect_llm (Optional[str], optional): Dedicated language model for self-reflection
operations. If None, uses the main llm parameter. Defaults to None.
reflect_prompt (Optional[str], optional): Custom prompt template for self-reflection
that guides the agent's self-evaluation process. Defaults to None.
user_id (Optional[str], optional): Unique identifier for the user or session to
enable personalized responses and memory isolation. Defaults to "praison".
reasoning_steps (bool, optional): Enable step-by-step reasoning output to show the
agent's thought process during problem solving. Defaults to False.
guardrail (Optional[Union[Callable[['TaskOutput'], Tuple[bool, Any]], str]], optional):
Safety mechanism to validate agent outputs. Can be a validation function or
description string for LLM-based validation. Defaults to None.
max_guardrail_retries (int, optional): Maximum number of retry attempts when guardrail
validation fails before giving up. Defaults to 3.
handoffs (Optional[List[Union['Agent', 'Handoff']]], optional): List of agents or
handoff configurations that this agent can delegate tasks to. Enables agent-to-agent
collaboration and task specialization. Defaults to None.
base_url (Optional[str], optional): Base URL for custom LLM endpoints (e.g., Ollama).
If provided, automatically creates a custom LLM instance. Defaults to None.
api_key (Optional[str], optional): API key for LLM provider. If not provided,
falls back to environment variables. Defaults to None.
name: Agent name for identification and logging. Defaults to "Agent".
role: Role/job title defining expertise (e.g., "Data Analyst").
goal: Primary objective the agent aims to achieve.
backstory: Background context shaping personality and decisions.
instructions: Direct instructions (overrides role/goal/backstory). Recommended for simple agents.
llm: Model name string ("gpt-4o", "anthropic/claude-3-sonnet") or LLM object.
Defaults to OPENAI_MODEL_NAME env var or "gpt-4o-mini".
model: Alias for llm parameter.
function_calling_llm: Dedicated LLM for function calling. Deprecated: use llm=.
llm_config: LLM configuration dict. Deprecated: use llm=.
base_url: Custom LLM endpoint URL (e.g., for Ollama). Kept separate for auth.
api_key: API key for LLM provider. Kept separate for auth.
tools: List of tools, functions, callables, or MCP instances.
allow_delegation: Allow task delegation to other agents. Defaults to False.
allow_code_execution: Enable code execution during tasks. Defaults to False.
code_execution_mode: "safe" (restricted) or "unsafe" (full access). Defaults to "safe".
handoffs: List of Agent or Handoff objects for agent-to-agent collaboration.
auto_save: Session name for automatic session saving.
rate_limiter: Rate limiter instance for API call throttling.
memory: Memory system configuration. Accepts:
- bool: True enables defaults, False disables
- MemoryConfig: Custom configuration
- MemoryManager: Pre-configured instance
knowledge: Knowledge sources. Accepts:
- bool: True enables defaults
- List[str]: File paths, URLs, or text content
- KnowledgeConfig: Custom configuration
planning: Planning mode. Accepts:
- bool: True enables with defaults
- PlanningConfig: Custom configuration
reflection: Self-reflection. Accepts:
- bool: True enables with defaults
- ReflectionConfig: Custom configuration
guardrails: Output validation. Accepts:
- bool: True enables with defaults
- Callable: Validation function
- GuardrailConfig: Custom configuration
web: Web search/fetch. Accepts:
- bool: True enables with defaults
- WebConfig: Custom configuration
context: Context management. Accepts:
- bool: True enables with defaults
- ManagerConfig: Custom configuration
autonomy: Autonomy settings. Accepts:
- bool: True enables with defaults
- Dict: Configuration dict
- AutonomyConfig: Custom configuration
verification_hooks: List of VerificationHook instances for output verification.
output: Output configuration. Accepts:
- str: Preset name ("silent", "actions", "verbose", "json", "stream")
- OutputConfig: Custom configuration
Controls: verbose, markdown, stream, metrics, reasoning_steps
execution: Execution configuration. Accepts:
- str: Preset name ("fast", "balanced", "thorough")
- ExecutionConfig: Custom configuration
Controls: max_iter, max_rpm, max_execution_time, max_retry_limit
templates: Template configuration (TemplateConfig).
Controls: system_template, prompt_template, response_template
caching: Caching configuration. Accepts:
- bool: True enables with defaults
- CachingConfig: Custom configuration
hooks: Event hooks. Accepts:
- List: List of hook callables
- HooksConfig: Custom configuration
skills: Agent skills. Accepts:
- List[str]: Skill directory paths
- SkillsConfig: Custom configuration
Raises:
ValueError: If all of name, role, goal, backstory, and instructions are None.
ImportError: If memory or LLM features are requested but dependencies are not installed.
Note:
Many legacy parameters have been consolidated into config objects:
- verbose, markdown, stream, metrics, reasoning_steps → output=
- max_iter, max_rpm, max_execution_time, max_retry_limit → execution=
- self_reflect, max_reflect, min_reflect, reflect_llm → reflection=
- guardrail, max_guardrail_retries → guardrails=
- system_template, prompt_template, response_template → templates=
- cache, prompt_caching → caching=
- web_search, web_fetch → web=
"""
# Add check at start if memory is requested
if memory is not None:
@@ -446,14 +438,31 @@ class Agent:
# ─────────────────────────────────────────────────────────────────────
# Resolve OUTPUT param using unified resolver
# Supports: None, str preset, list [preset, overrides], OutputConfig
# DEFAULT: "silent" mode (zero overhead, fastest performance)
# Can be overridden by PRAISONAI_OUTPUT env var
# ─────────────────────────────────────────────────────────────────────
from ..config.presets import DEFAULT_OUTPUT_MODE
# Check for env var override
env_output = os.environ.get('PRAISONAI_OUTPUT', '').lower()
if env_output and env_output in OUTPUT_PRESETS and output is None:
output = env_output
# Use default output mode if not specified
# Track if user explicitly configured output (for respecting in start())
if output is None:
output = DEFAULT_OUTPUT_MODE
_has_explicit_output = False
else:
_has_explicit_output = True
_output_config = resolve(
value=output,
param_name="output",
config_class=OutputConfig,
presets=OUTPUT_PRESETS,
array_mode=ArrayMode.PRESET_OVERRIDE,
default=OutputConfig(),
default=OutputConfig(), # OutputConfig defaults to silent mode (zero overhead)
)
if _output_config:
verbose = _output_config.verbose
@@ -462,8 +471,45 @@ class Agent:
metrics = _output_config.metrics
reasoning_steps = _output_config.reasoning_steps
output_style = getattr(_output_config, 'style', None)
actions_trace = getattr(_output_config, 'actions_trace', False) # Default False (silent)
json_output = getattr(_output_config, 'json_output', False)
status_trace = getattr(_output_config, 'status_trace', False) # New: clean inline status
simple_output = getattr(_output_config, 'simple_output', False) # status preset: no timestamps
else:
verbose, markdown, stream, metrics, reasoning_steps = True, True, False, False, False
# Fallback defaults match silent mode (zero overhead)
verbose, markdown, stream, metrics, reasoning_steps = False, False, False, False, False
actions_trace = False # No callbacks by default
json_output = False
status_trace = False
simple_output = False
# Enable trace output mode if configured (takes priority)
# This provides timestamped inline status with duration
if status_trace:
try:
from ..output.trace import enable_trace_output, is_trace_output_enabled
if not is_trace_output_enabled():
enable_trace_output(use_color=True, show_timestamps=True)
except ImportError:
pass # Trace module not available
# Enable status output mode if configured (simple progress, no timestamps)
# This registers callbacks to capture tool calls and final output
elif actions_trace:
try:
from ..output.status import enable_status_output, is_status_output_enabled
if not is_status_output_enabled():
output_format = "jsonl" if json_output else "text"
# simple_output=True means status preset (no timestamps)
# metrics=True means debug preset (show token/cost info)
enable_status_output(
redact=True,
use_color=True,
format=output_format,
show_timestamps=not simple_output,
show_metrics=metrics
)
except ImportError:
pass # Status module not available
# ─────────────────────────────────────────────────────────────────────
# Resolve EXECUTION param using unified resolver
@@ -754,7 +800,15 @@ class Agent:
# If instructions are provided, use them to set role, goal, and backstory
if instructions:
self.name = name or "Agent"
# Only use explicitly provided name, don't auto-generate from instructions
# Auto-generation was producing confusing names like "You Are Agent" from
# instructions like "You are a helpful assistant"
if name:
self.name = name
else:
# Don't auto-generate - None signals "no explicit name provided"
# Display logic will skip Agent Info panel when name is None
self.name = None
self.role = role or "Assistant"
self.goal = goal or instructions
self.backstory = backstory or instructions
@@ -783,13 +837,30 @@ class Agent:
self._llm_config = llm_config or {}
self._llm_configurable = self._llm_config.get('configurable', False)
# ============================================================
# LLM CONSOLIDATION: Handle model= alias and deprecation warnings
# Precedence: llm= > model= > default
# ============================================================
import warnings
# Handle model= alias for llm= (NO warnings - both are valid)
if llm is None and model is not None:
llm = model # model= is an alias for llm=
# Legacy params (llm_config, function_calling_llm) are accepted silently
# for backward compatibility - NO deprecation warnings per policy
# Store rate limiter (optional, zero overhead when None)
self._rate_limiter = rate_limiter
# Store OpenAI client parameters for lazy initialization
# Store OpenAI client parameters for lazy initialization (kept separate)
self._openai_api_key = api_key
self._openai_base_url = base_url
self.__openai_client = None
# Expose base_url and api_key as properties for tests
self.base_url = base_url
self.api_key = api_key
# If base_url is provided, always create a custom LLM instance
if base_url:
@@ -806,7 +877,7 @@ class Agent:
self.llm_instance = LLM(**llm_config)
else:
# Create LLM with model string and base_url
model_name = llm or os.getenv('OPENAI_MODEL_NAME', 'gpt-5-nano')
model_name = llm or os.getenv('OPENAI_MODEL_NAME', 'gpt-4o-mini')
self.llm_instance = LLM(
model=model_name,
base_url=base_url,
@@ -869,7 +940,7 @@ class Agent:
) from e
# Otherwise, fall back to OpenAI environment/name
else:
self.llm = llm or os.getenv('OPENAI_MODEL_NAME', 'gpt-5-nano')
self.llm = llm or os.getenv('OPENAI_MODEL_NAME', 'gpt-4o-mini')
# Handle tools parameter - ensure it's always a list
if callable(tools):
# If a single function/callable is passed, wrap it in a list
@@ -893,6 +964,7 @@ class Agent:
self._memory_instance = None
self._init_memory(memory, user_id)
self.verbose = verbose
self._has_explicit_output_config = _has_explicit_output # Track if user set output mode
self.allow_delegation = allow_delegation
self.step_callback = step_callback
self.cache = cache
@@ -915,7 +987,7 @@ class Agent:
self.min_reflect = min_reflect
self.reflect_prompt = reflect_prompt
# Use the same model selection logic for reflect_llm
self.reflect_llm = reflect_llm or os.getenv('OPENAI_MODEL_NAME', 'gpt-5-nano')
self.reflect_llm = reflect_llm or os.getenv('OPENAI_MODEL_NAME', 'gpt-4o-mini')
self._console = None # Lazy load console when needed
# Initialize system prompt
@@ -957,9 +1029,14 @@ Your Goal: {self.goal}
self.tools.append(internet_search)
logging.info("Model does not support native web search. Added DuckDuckGo fallback tool.")
# Log warning if web_fetch is enabled but model doesn't support it
# Raise error if web_fetch is explicitly enabled but model doesn't support it
# Web fetch is only supported by Anthropic/Claude models
if web_fetch and not self._model_supports_web_fetch():
logging.warning(f"Model '{self.llm}' does not support native web fetch. Web fetch will be ignored.")
raise ValueError(
f"web_fetch is only supported on Anthropic/Claude models. "
f"Model '{self.llm}' does not support web fetch. "
f"Either use a Claude model (e.g., 'anthropic/claude-sonnet-4') or disable fetch with web=WebConfig(fetch=False)."
)
# Initialize guardrail settings
self.guardrail = guardrail
@@ -1042,6 +1119,16 @@ Your Goal: {self.goal}
self._context_param = context # Store raw param for lazy init
self._context_manager = None # Lazy initialized on first use
self._context_manager_initialized = False
# Action trace mode - handled via display callbacks, not separate emitter
self._actions_trace = actions_trace
# Telemetry
try:
from ..telemetry import get_telemetry
self._telemetry = get_telemetry()
except (ImportError, AttributeError):
self._telemetry = None
@property
def auto_memory(self):
@@ -1161,7 +1248,11 @@ Your Goal: {self.goal}
@property
def console(self):
"""Lazily initialize Rich Console only when needed."""
"""Lazily initialize Rich Console only when needed AND verbose is True."""
# Only return console if verbose mode is enabled
# This prevents panels from being shown in status/silent modes
if not self.verbose:
return None
if self._console is None:
from rich.console import Console
self._console = Console()
@@ -1697,7 +1788,7 @@ Your Goal: {self.goal}
elif hasattr(self, 'llm') and self.llm:
model_name = self.llm
else:
model_name = "gpt-5-nano"
model_name = "gpt-4o-mini"
return supports_web_search(model_name)
@@ -1719,7 +1810,7 @@ Your Goal: {self.goal}
elif hasattr(self, 'llm') and self.llm:
model_name = self.llm
else:
model_name = "gpt-5-nano"
model_name = "gpt-4o-mini"
return supports_web_fetch(model_name)
@@ -1963,7 +2054,7 @@ Your Goal: {self.goal}
Returns:
The LLM model/instance being used by this agent.
- For standard models: returns the model string (e.g., "gpt-5-nano")
- For standard models: returns the model string (e.g., "gpt-4o-mini")
- For custom LLM instances: returns the LLM instance object
- For provider models: returns the LLM instance object
"""
@@ -1973,7 +2064,7 @@ Your Goal: {self.goal}
return self.llm
else:
# Default fallback
return "gpt-5-nano"
return "gpt-4o-mini"
def _ensure_knowledge_processed(self):
"""Ensure knowledge is initialized and processed when first accessed."""
@@ -2794,6 +2885,9 @@ Your Goal: {self.goal}"""
"""
logging.debug(f"{self.name} executing tool {function_name} with arguments: {arguments}")
# NOTE: tool_call callback is triggered by display_tool_call in openai_client.py
# Do NOT call it here to avoid duplicate output
# Set up injection context for tools with Injected parameters
from ..tools.injected import AgentState, with_injection_context
state = AgentState(
@@ -3108,21 +3202,20 @@ Your Goal: {self.goal}"""
This centralizes the logic for callback execution and display to avoid duplication.
"""
# Always execute callbacks regardless of verbose setting (only when not using custom LLM)
if not self._using_custom_llm:
execute_sync_callback(
'interaction',
message=prompt,
response=response,
markdown=self.markdown,
generation_time=generation_time,
agent_name=self.name,
agent_role=self.role,
agent_tools=[t.__name__ for t in self.tools] if self.tools else None,
task_name=task_name,
task_description=task_description,
task_id=task_id
)
# Always execute callbacks for status/trace output (regardless of LLM backend)
execute_sync_callback(
'interaction',
message=prompt,
response=response,
markdown=self.markdown,
generation_time=generation_time,
agent_name=self.name,
agent_role=self.role,
agent_tools=[t.__name__ for t in self.tools] if self.tools else None,
task_name=task_name,
task_description=task_description,
task_id=task_id
)
# Always display final interaction when verbose is True to ensure consistent formatting
# This ensures both OpenAI and custom LLM providers (like Gemini) show formatted output
if self.verbose and not self._final_display_shown:
@@ -3537,7 +3630,7 @@ Your Goal: {self.goal}"""
output_pydantic=output_pydantic,
verbose=self.verbose,
markdown=self.markdown,
self_reflect=self.self_reflect,
reflection=self.self_reflect,
max_reflect=self.max_reflect,
min_reflect=self.min_reflect,
console=self.console,
@@ -3673,7 +3766,7 @@ Your Goal: {self.goal}"""
if self.verbose:
logging.debug(f"Agent {self.name} final response: {response_text}")
# Return only reasoning content if reasoning_steps is True
if reasoning_steps and hasattr(response.choices[0].message, 'reasoning_content'):
if reasoning_steps and hasattr(response.choices[0].message, 'reasoning_content') and response.choices[0].message.reasoning_content:
# Apply guardrail to reasoning content
try:
validated_reasoning = self._apply_guardrail_with_retry(response.choices[0].message.reasoning_content, original_prompt, temperature, tools, task_name, task_description, task_id)
@@ -3825,6 +3918,10 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
async def achat(self, prompt: str, temperature=1.0, tools=None, output_json=None, output_pydantic=None, reasoning_steps=False, task_name=None, task_description=None, task_id=None):
"""Async version of chat method with self-reflection support."""
# Track execution via telemetry
if hasattr(self, '_telemetry') and self._telemetry:
self._telemetry.track_agent_execution(self.name, success=True)
# Reset the final display flag for each new conversation
self._final_display_shown = False
@@ -3891,7 +3988,7 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
output_pydantic=output_pydantic,
verbose=self.verbose,
markdown=self.markdown,
self_reflect=self.self_reflect,
reflection=self.self_reflect,
max_reflect=self.max_reflect,
min_reflect=self.min_reflect,
console=self.console,
@@ -4198,7 +4295,7 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
final_response = process_stream_chunks(chunks)
# Return only reasoning content if reasoning_steps is True
if reasoning_steps and hasattr(final_response.choices[0].message, 'reasoning_content'):
if reasoning_steps and hasattr(final_response.choices[0].message, 'reasoning_content') and final_response.choices[0].message.reasoning_content:
return final_response.choices[0].message.reasoning_content
return final_response.choices[0].message.content if final_response else full_response_text
@@ -4212,16 +4309,94 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
return None
async def arun(self, prompt: str, **kwargs):
"""Async alias for astart() method"""
return await self.astart(prompt, **kwargs)
"""Async version of run() - silent, non-streaming, returns structured result.
Production-friendly async execution. Does not stream or display output.
Args:
prompt: The input prompt to process
**kwargs: Additional arguments passed to achat()
Returns:
The agent's response as a string
"""
# Remove stream from kwargs since achat() doesn't accept it
kwargs.pop('stream', None)
return await self.achat(prompt, **kwargs)
async def astart(self, prompt: str, **kwargs):
"""Async version of start method"""
"""Async version of start() - interactive, streaming-aware.
Beginner-friendly async execution. Streams by default when in TTY.
Args:
prompt: The input prompt to process
**kwargs: Additional arguments passed to achat()
Returns:
The agent's response as a string
"""
import sys
# Determine streaming behavior (same logic as start())
stream_requested = kwargs.get('stream')
if stream_requested is None:
if getattr(self, 'stream', None) is not None:
stream_requested = self.stream
else:
stream_requested = sys.stdout.isatty()
kwargs['stream'] = stream_requested
return await self.achat(prompt, **kwargs)
def run(self, prompt: str, **kwargs):
"""Alias for start() method"""
return self.start(prompt, **kwargs)
"""Execute agent silently and return structured result.
Production-friendly execution. Always uses silent mode with no streaming
or verbose display, regardless of TTY status. Use this for programmatic,
scripted, or automated usage where you want just the result.
Args:
prompt: The input prompt to process
**kwargs: Additional arguments:
- stream (bool): Force streaming if True. Default: False
- output (str): Output preset override (rarely needed)
Returns:
The agent's response as a string
Example:
```python
agent = Agent(instructions="You are helpful")
result = agent.run("What is 2+2?") # Silent, returns "4"
print(result)
```
Note:
Unlike .start() which enables verbose output in TTY for interactive
use, .run() is always silent. This makes it suitable for:
- Production pipelines
- Automated scripts
- Background processing
- API endpoints
"""
# Production defaults: no streaming, no display
if 'stream' not in kwargs:
kwargs['stream'] = False
# Load history context
self._load_history_context()
# Check if planning mode is enabled
if self.planning:
result = self._start_with_planning(prompt, **kwargs)
else:
result = self.chat(prompt, **kwargs)
# Auto-save session if enabled
self._auto_save_session()
return result
def _get_planning_agent(self):
"""Lazy load PlanningAgent for planning mode."""
@@ -4355,26 +4530,297 @@ Write the complete compiled report:"""
# Chat history is preserved in self.chat_history (no action needed)
def start(self, prompt: str, **kwargs):
"""Start the agent with a prompt. This is a convenience method that wraps chat()."""
# Load history from past sessions (now handled via context= param)
def start(self, prompt: str = None, **kwargs):
"""Start the agent interactively with verbose output.
Beginner-friendly execution. Defaults to verbose output with streaming
when running in a TTY. Use this for interactive/terminal usage where
you want to see output in real-time with rich formatting.
Args:
prompt: The input prompt to process. If not provided, uses the
agent's instructions as the task (useful when instructions
already describe what the agent should do).
**kwargs: Additional arguments:
- stream (bool | None): Override streaming. None = auto-detect TTY
- output (str): Output preset override (e.g., "silent", "verbose")
Returns:
- If streaming: Generator yielding response chunks
- If not streaming: The complete response as a string
Example:
```python
# Minimal usage - instructions IS the task
agent = Agent(instructions="Research AI trends and summarize")
result = agent.start() # Uses instructions as task
# With explicit prompt (overrides/adds to instructions)
agent = Agent(instructions="You are a helpful assistant")
result = agent.start("What is 2+2?") # Uses prompt as task
```
Note:
Unlike .run() which is always silent (production use), .start()
enables verbose output by default when in a TTY for beginner-friendly
interactive use. Use .run() for programmatic/scripted usage.
"""
import sys
# If no prompt provided, use instructions as the task
if prompt is None:
prompt = self.instructions or "Hello"
# Load history from past sessions
self._load_history_context()
# Check if planning mode is enabled
if self.planning:
result = self._start_with_planning(prompt, **kwargs)
elif kwargs.get('stream', getattr(self, 'stream', False)):
# Return a generator for streaming response
result = self._start_stream(prompt, **kwargs)
else:
# Return regular chat response for backward compatibility
kwargs['stream'] = False
result = self.chat(prompt, **kwargs)
# Determine if we're in an interactive TTY
is_tty = sys.stdout.isatty()
# Determine streaming behavior
# Priority: explicit kwarg > agent's stream attribute > TTY detection
stream_requested = kwargs.get('stream')
if stream_requested is None:
# Check agent's stream attribute first
if getattr(self, 'stream', None) is not None:
stream_requested = self.stream
else:
# Auto-detect: stream if stdout is a TTY (interactive terminal)
stream_requested = is_tty
# ─────────────────────────────────────────────────────────────────────
# Enable verbose output in TTY for beginner-friendly interactive use
# Priority: agent's explicit output config > start() override > TTY auto
# ─────────────────────────────────────────────────────────────────────
original_verbose = self.verbose
original_markdown = self.markdown
output_override = kwargs.pop('output', None) # Pop to prevent passing to chat()
# Check if agent was configured with explicit output mode (not default)
# If so, respect it and don't auto-enable verbose for TTY
has_explicit_output = getattr(self, '_has_explicit_output_config', False)
try:
# Apply output override from start() call if provided
if output_override:
# Apply explicit output preset for this call
from ..config.presets import OUTPUT_PRESETS
if output_override in OUTPUT_PRESETS:
preset = OUTPUT_PRESETS[output_override]
self.verbose = preset.get('verbose', False)
self.markdown = preset.get('markdown', False)
# Only auto-enable verbose for TTY if NO explicit output was configured
elif is_tty and not has_explicit_output:
self.verbose = True
self.markdown = True
# Check if planning mode is enabled
if self.planning:
result = self._start_with_planning(prompt, **kwargs)
elif stream_requested:
# Return a generator for streaming response
kwargs['stream'] = True
result = self._start_stream(prompt, **kwargs)
else:
# Return regular chat response with animated working status
kwargs['stream'] = False
# Show animated status during LLM call if verbose
if self.verbose and is_tty:
from rich.live import Live
from rich.console import Group
from rich.status import Status
from ..main import PRAISON_COLORS, sync_display_callbacks
import threading
import time as time_module
console = Console()
start_time = time_module.time()
# ─────────────────────────────────────────────────────────────
# Shared state for dynamic status messages (thread-safe)
# Updated by callbacks during tool execution
# ─────────────────────────────────────────────────────────────
current_status = ["Analyzing query..."]
tools_called = []
# Register a temporary callback to track tool calls
def status_tool_callback(**kwargs):
tool_name = kwargs.get('tool_name', '')
if tool_name:
tools_called.append(tool_name)
current_status[0] = f"Calling tool: {tool_name}..."
# Store original callback and register ours
original_tool_callback = sync_display_callbacks.get('tool_call')
sync_display_callbacks['tool_call'] = status_tool_callback
# Animation state
result_holder = [None]
error_holder = [None]
# Temporarily disable verbose in chat to prevent duplicate output
original_verbose_chat = self.verbose
def run_chat():
try:
# Suppress verbose during animation - we'll display result ourselves
self.verbose = False
current_status[0] = "Sending to LLM..."
result_holder[0] = self.chat(prompt, **kwargs)
current_status[0] = "Finalizing response..."
except Exception as e:
error_holder[0] = e
finally:
self.verbose = original_verbose_chat
# Restore original callback
if original_tool_callback:
sync_display_callbacks['tool_call'] = original_tool_callback
elif 'tool_call' in sync_display_callbacks:
del sync_display_callbacks['tool_call']
# Start chat in background thread
chat_thread = threading.Thread(target=run_chat)
chat_thread.start()
from rich.panel import Panel
from rich.text import Text
from rich.markdown import Markdown
# ─────────────────────────────────────────────────────────────
# Smart Agent Info: Only show if user provided meaningful info
# Skip if using defaults ("Agent"/"Assistant") with single agent
# ─────────────────────────────────────────────────────────────
has_custom_name = self.name and self.name not in ("Agent", "agent", None, "")
has_custom_role = self.role and self.role not in ("Assistant", "AI Assistant", "assistant", None, "")
has_tools = bool(self.tools)
show_agent_info = has_custom_name or has_custom_role or has_tools
agent_panel = None
if show_agent_info:
agent_info_parts = []
if has_custom_name:
agent_info_parts.append(f"[bold {PRAISON_COLORS['task']}]👤 Agent:[/] [{PRAISON_COLORS['agent_text']}]{self.name}[/]")
if has_custom_role:
agent_info_parts.append(f"[bold {PRAISON_COLORS['metrics']}]Role:[/] [{PRAISON_COLORS['agent_text']}]{self.role}[/]")
if has_tools:
tools_list = [t.__name__ if hasattr(t, '__name__') else str(t) for t in self.tools][:5]
tools_str = ", ".join(f"[italic {PRAISON_COLORS['response']}]{tool}[/]" for tool in tools_list)
agent_info_parts.append(f"[bold {PRAISON_COLORS['agent']}]Tools:[/] {tools_str}")
agent_panel = Panel(
"\n".join(agent_info_parts),
border_style=PRAISON_COLORS["agent"],
title="[bold]Agent Info[/]",
title_align="left",
padding=(1, 2)
)
# Create task panel
task_panel = Panel.fit(
Markdown(prompt) if self.markdown else Text(prompt),
title="Task",
border_style=PRAISON_COLORS["task"]
)
# Show initial panels (agent info if applicable, then task)
if agent_panel:
console.print(agent_panel)
console.print(task_panel)
# ─────────────────────────────────────────────────────────────
# Animate with Rich.Status showing DYNAMIC status from callbacks
# ─────────────────────────────────────────────────────────────
with console.status(
f"[bold yellow]Working...[/] {current_status[0]}",
spinner="dots",
spinner_style="yellow"
) as status:
last_status = current_status[0]
while chat_thread.is_alive():
time_module.sleep(0.15) # More responsive updates
# Only update if status changed (reduces flicker)
if current_status[0] != last_status:
last_status = current_status[0]
status.update(f"[bold yellow]Working...[/] {current_status[0]}")
# Calculate elapsed time
elapsed = time_module.time() - start_time
# Re-raise any error from chat
if error_holder[0]:
raise error_holder[0]
result = result_holder[0]
# Display response panel
response_panel = Panel.fit(
Markdown(str(result)) if self.markdown else Text(str(result)),
title=f"Response ({elapsed:.1f}s)",
border_style=PRAISON_COLORS["response"]
)
console.print(response_panel)
# Show tool activity summary if tools were called (deduplicated)
if tools_called:
# Use dict.fromkeys to preserve order while removing duplicates
unique_tools = list(dict.fromkeys(tools_called))
tools_summary = ", ".join(unique_tools)
console.print(f"[dim]🔧 Tools used: {tools_summary}[/dim]")
else:
result = self.chat(prompt, **kwargs)
# Auto-save session if enabled
self._auto_save_session()
return result
finally:
# Restore original output settings
self.verbose = original_verbose
self.markdown = original_markdown
def iter_stream(self, prompt: str, **kwargs):
"""Stream agent response as an iterator of chunks.
App-friendly streaming. Yields response chunks without terminal display.
Use this for building custom UIs or processing streams programmatically.
Args:
prompt: The input prompt to process
**kwargs: Additional arguments:
- display (bool): Show terminal output. Default: False
- output (str): Output preset override
Yields:
str: Response chunks as they are generated
Example:
```python
agent = Agent(instructions="You are helpful")
# Process stream programmatically
full_response = ""
for chunk in agent.iter_stream("Tell me a story"):
full_response += chunk
# Custom processing here
# Or collect all at once
response = "".join(agent.iter_stream("Hello"))
```
"""
# Load history context
self._load_history_context()
# Force streaming, no display by default (app-friendly)
kwargs['stream'] = True
# Use the internal streaming generator
for chunk in self._start_stream(prompt, **kwargs):
yield chunk
# Auto-save session if enabled
self._auto_save_session()
return result
def _load_history_context(self):
"""Load history from past sessions into context.
@@ -434,8 +434,7 @@ This report contains all agent interactions and outputs from a complete ContextA
Provide comprehensive analysis that follows the PRD template principles and enables
AI assistants to implement features that perfectly match existing codebase patterns.""",
llm=self.llm if hasattr(self, 'llm') else "gpt-5-nano",
verbose=getattr(self, 'verbose', True)
llm=self.llm if hasattr(self, 'llm') else "gpt-4o-mini",
)
prompt = f"""Analyze this gitingest codebase digest following PRD template methodology:
@@ -515,8 +514,7 @@ codebase style and architecture following PRD template principles."""
role="Expert Manual Codebase Analysis Specialist",
goal="Perform comprehensive manual codebase analysis following PRD methodology",
instructions="""Analyze the codebase samples following PRD template methodology for complete understanding.""",
llm=self.llm if hasattr(self, 'llm') else "gpt-5-nano",
verbose=getattr(self, 'verbose', True)
llm=self.llm if hasattr(self, 'llm') else "gpt-4o-mini",
)
# Format comprehensive sample
@@ -566,8 +564,7 @@ Analyze following PRD principles to extract patterns, conventions, and architect
6. Design pattern implementations
7. Code complexity metrics
8. API and interface patterns""",
llm=self.llm if hasattr(self, 'llm') else "gpt-5-nano",
verbose=getattr(self, 'verbose', True)
llm=self.llm if hasattr(self, 'llm') else "gpt-4o-mini",
)
# Perform AST analysis on Python files
@@ -624,8 +621,7 @@ Extract comprehensive patterns that follow PRD template principles for implement
For each pattern, provide the pattern name, where it's used, and how to replicate it
following PRD template principles.""",
llm=self.llm if hasattr(self, 'llm') else "gpt-5-nano",
verbose=getattr(self, 'verbose', True)
llm=self.llm if hasattr(self, 'llm') else "gpt-4o-mini",
)
# Get representative code samples
@@ -674,8 +670,7 @@ patterns and best practices for first-try success."""
goal="Analyze testing patterns for comprehensive validation framework design",
instructions="""Analyze testing patterns to understand validation approaches and create
comprehensive test frameworks following PRD methodology.""",
llm=self.llm if hasattr(self, 'llm') else "gpt-5-nano",
verbose=getattr(self, 'verbose', True)
llm=self.llm if hasattr(self, 'llm') else "gpt-4o-mini",
)
# Get test files
@@ -801,8 +796,7 @@ Extract testing patterns for validation framework creation following PRD princip
Confidence level for one-pass implementation
Generate PRPs following this EXACT structure for first-try implementation success.""",
llm=self.llm if hasattr(self, 'llm') else "gpt-5-nano",
verbose=getattr(self, 'verbose', True)
llm=self.llm if hasattr(self, 'llm') else "gpt-4o-mini",
)
prompt = f"""Generate a comprehensive Product Requirements Prompt (PRP) following the EXACT PRD template structure:
@@ -850,8 +844,7 @@ on the first try following PRD template principles."""
6. CODE QUALITY: Complexity analysis, maintainability
7. DOCUMENTATION VALIDATION: Documentation completeness
8. DEPENDENCY VALIDATION: Dependency analysis and security""",
llm=self.llm if hasattr(self, 'llm') else "gpt-5-nano",
verbose=getattr(self, 'verbose', True)
llm=self.llm if hasattr(self, 'llm') else "gpt-4o-mini",
)
# Analyze existing validation patterns
@@ -897,8 +890,7 @@ following PRD template principles."""
instructions="""Compile all available documentation following PRD methodology including:
README files, API documentation, setup guides, architecture docs, and any other
relevant documentation that provides context for implementation.""",
llm=self.llm if hasattr(self, 'llm') else "gpt-5-nano",
verbose=getattr(self, 'verbose', True)
llm=self.llm if hasattr(self, 'llm') else "gpt-4o-mini",
)
# Get documentation files
@@ -943,8 +935,7 @@ following PRD template principles."""
instructions="""Analyze integration points following PRD methodology including:
APIs, databases, external services, configuration points, and any other
integration requirements that affect implementation.""",
llm=self.llm if hasattr(self, 'llm') else "gpt-5-nano",
verbose=getattr(self, 'verbose', True)
llm=self.llm if hasattr(self, 'llm') else "gpt-4o-mini",
)
# Get configuration and dependency files
@@ -1004,8 +995,7 @@ following PRD template principles."""
8. DOCUMENTATION UPDATES: Documentation to create/update
9. INTEGRATION STEPS: How to integrate with existing systems
10. VALIDATION CHECKPOINTS: Validation steps at each phase""",
llm=self.llm if hasattr(self, 'llm') else "gpt-5-nano",
verbose=getattr(self, 'verbose', True)
llm=self.llm if hasattr(self, 'llm') else "gpt-4o-mini",
)
prompt = f"""Create detailed implementation blueprint following PRD methodology:
@@ -1302,8 +1292,7 @@ Every agent interaction has been saved for full audit trail and reproducibility.
GOAL: [extracted implementation goal]
Be precise and extract only what is explicitly mentioned or clearly implied.""",
llm=self.llm if hasattr(self, 'llm') else "gpt-5-nano",
verbose=getattr(self, 'verbose', True)
llm=self.llm if hasattr(self, 'llm') else "gpt-4o-mini",
)
prompt = f"""Parse this user input to extract codebase and goal:
@@ -1536,8 +1525,7 @@ Note: Detailed function/class metadata not available due to content access limit
5. Documentation topics
Make the output easy for a file selection agent to understand which files contain what functionality.""",
llm=self.llm if hasattr(self, 'llm') else "gpt-5-nano",
verbose=getattr(self, 'verbose', True)
llm=self.llm if hasattr(self, 'llm') else "gpt-4o-mini",
)
structure_text = basic_structure.get('structure', '')
@@ -1770,8 +1758,7 @@ Focus on creating clear, structured metadata that will help with intelligent fil
["README.md", "src/auth/login.py", "config/settings.py", ...]
Maximum 50 files for efficient analysis.""",
llm=self.llm if hasattr(self, 'llm') else "gpt-5-nano",
verbose=getattr(self, 'verbose', True)
llm=self.llm if hasattr(self, 'llm') else "gpt-4o-mini",
)
# Prepare enhanced structure information
@@ -1923,8 +1910,7 @@ Maximum 50 files.""".format(goal=goal)
8. EXAMPLES: Similar features that can guide {goal} implementation
Since these files were pre-selected for relevance, provide deep analysis of how each contributes to implementing: {goal}""",
llm=self.llm if hasattr(self, 'llm') else "gpt-5-nano",
verbose=getattr(self, 'verbose', True)
llm=self.llm if hasattr(self, 'llm') else "gpt-4o-mini",
)
prompt = f"""Analyze these carefully selected repository files for implementing: {goal}
@@ -2024,8 +2010,7 @@ Since these files were pre-selected for relevance, explain how each contributes
- Success criteria for {goal}
Focus everything on successfully implementing: {goal}""",
llm=self.llm if hasattr(self, 'llm') else "gpt-5-nano",
verbose=getattr(self, 'verbose', True)
llm=self.llm if hasattr(self, 'llm') else "gpt-4o-mini",
)
chunk_info = ""
@@ -2303,13 +2288,13 @@ def create_context_agent(llm: Optional[Union[str, Any]] = None, **kwargs) -> Con
Factory function to create a ContextAgent following Context Engineering and PRD methodology.
Args:
llm: Language model to use (e.g., "gpt-5-nano", "claude-3-haiku")
llm: Language model to use (e.g., "gpt-4o-mini", "claude-3-haiku")
**kwargs: Additional arguments to pass to ContextAgent constructor
Returns:
ContextAgent: Configured ContextAgent for comprehensive context generation following PRD principles
"""
if llm is None:
llm = "gpt-5-nano"
llm = "gpt-4o-mini"
return ContextAgent(llm=llm, **kwargs)
@@ -17,7 +17,7 @@ Supported Expansion Strategies:
Example:
from praisonaiagents import PromptExpanderAgent, ExpandStrategy
agent = PromptExpanderAgent(verbose=True)
agent = PromptExpanderAgent()
# Basic expansion
result = agent.expand("write a movie script in 3 lines")
@@ -29,7 +29,7 @@ Example:
# With tools for context gathering
from praisonaiagents.tools import internet_search
agent = PromptExpanderAgent(tools=[internet_search], verbose=True)
agent = PromptExpanderAgent(tools=[internet_search])
result = agent.expand("latest AI developments")
"""
@@ -73,13 +73,12 @@ class PromptExpanderAgent:
Attributes:
name: Name of the agent
model: LLM model to use for expansion
verbose: Whether to print detailed logs
model: LLM model to use for expansion (alias for llm=)
temperature: Temperature for LLM generation (higher = more creative)
tools: Optional tools for context gathering
Example:
agent = PromptExpanderAgent(model="gpt-4o-mini", verbose=True)
agent = PromptExpanderAgent(model="gpt-4o-mini")
result = agent.expand("write a poem", strategy=ExpandStrategy.CREATIVE)
print(result.expanded_prompt)
"""
@@ -230,8 +229,7 @@ Expanded prompt:"""
backstory="You are an expert at understanding user intent and transforming brief requests into comprehensive, detailed prompts.",
tools=self.tools,
llm=self.model,
verbose=self.verbose,
markdown=False
output={"verbose": self.verbose, "markdown": False}
)
return self._agent
@@ -0,0 +1,151 @@
"""
Agent Protocol Definitions.
Provides Protocol interfaces that define the minimal contract for agent implementations.
This enables:
- Mocking agents in tests without real LLM calls
- Creating custom agent implementations
- Type checking with static analyzers
These protocols are lightweight and have zero performance impact.
"""
from typing import Protocol, runtime_checkable, Optional, Any, Dict, List
@runtime_checkable
class AgentProtocol(Protocol):
"""
Minimal Protocol for agent implementations.
This defines the essential interface that any agent must provide.
It enables proper mocking and testing without real LLM dependencies.
Example:
```python
# Create a mock agent for testing
class MockAgent:
@property
def name(self) -> str:
return "TestAgent"
def chat(self, prompt: str, **kwargs) -> str:
return "Mock response"
async def achat(self, prompt: str, **kwargs) -> str:
return "Async mock response"
# Use in tests
agent: AgentProtocol = MockAgent()
result = agent.chat("Hello")
```
"""
@property
def name(self) -> str:
"""Agent's name identifier."""
...
def chat(self, prompt: str, **kwargs) -> str:
"""
Synchronous chat with the agent.
Args:
prompt: The user prompt or message
**kwargs: Additional optional parameters (temperature, tools, etc.)
Returns:
The agent's response as a string
"""
...
async def achat(self, prompt: str, **kwargs) -> str:
"""
Asynchronous chat with the agent.
Args:
prompt: The user prompt or message
**kwargs: Additional optional parameters (temperature, tools, etc.)
Returns:
The agent's response as a string
"""
...
@runtime_checkable
class RunnableAgentProtocol(AgentProtocol, Protocol):
"""
Extended Protocol for agents that support run/start methods.
This extends AgentProtocol with the run/start interface used
by the Agent class for task execution.
"""
def run(self, prompt: str, **kwargs) -> str:
"""Run the agent with a prompt (alias for chat in most cases)."""
...
def start(self, prompt: str, **kwargs) -> str:
"""Start the agent with a prompt."""
...
async def arun(self, prompt: str, **kwargs) -> str:
"""Async run the agent with a prompt."""
...
async def astart(self, prompt: str, **kwargs) -> str:
"""Async start the agent with a prompt."""
...
@runtime_checkable
class ToolAwareAgentProtocol(AgentProtocol, Protocol):
"""
Protocol for agents that support tools.
This extends AgentProtocol with tool-related properties.
"""
@property
def tools(self) -> List[Any]:
"""List of tools available to the agent."""
...
@runtime_checkable
class MemoryAwareAgentProtocol(AgentProtocol, Protocol):
"""
Protocol for agents that support memory.
This extends AgentProtocol with memory-related properties.
"""
@property
def chat_history(self) -> List[Dict[str, Any]]:
"""The agent's conversation history."""
...
def clear_history(self) -> None:
"""Clear the agent's conversation history."""
...
# Composite protocol for fully-featured agents
@runtime_checkable
class FullAgentProtocol(RunnableAgentProtocol, ToolAwareAgentProtocol, MemoryAwareAgentProtocol, Protocol):
"""
Complete Protocol for full-featured agents.
This combines all agent protocol features for agents that support
the full interface (chat, run, tools, memory).
"""
pass
__all__ = [
'AgentProtocol',
'RunnableAgentProtocol',
'ToolAwareAgentProtocol',
'MemoryAwareAgentProtocol',
'FullAgentProtocol',
]
@@ -15,7 +15,7 @@ Supported Rewriting Strategies:
Example:
from praisonaiagents import QueryRewriterAgent, RewriteStrategy
agent = QueryRewriterAgent(verbose=True)
agent = QueryRewriterAgent()
# Basic rewriting
result = agent.rewrite("AI trends")
@@ -34,7 +34,7 @@ Example:
# With search tools for context-aware rewriting
from praisonaiagents.tools import internet_search
agent = QueryRewriterAgent(tools=[internet_search], verbose=True)
agent = QueryRewriterAgent(tools=[internet_search])
result = agent.rewrite("latest AI developments") # Searches first, then rewrites
"""
@@ -96,13 +96,12 @@ class QueryRewriterAgent:
Attributes:
name: Name of the agent
model: LLM model to use for rewriting
verbose: Whether to print detailed logs
model: LLM model to use for rewriting (alias for llm=)
max_queries: Maximum number of queries to generate for multi-query strategy
abbreviations: Dictionary of common abbreviations to expand
Example:
agent = QueryRewriterAgent(model="gpt-4o-mini", verbose=True)
agent = QueryRewriterAgent(model="gpt-4o-mini")
result = agent.rewrite("ML best practices", strategy=RewriteStrategy.MULTI_QUERY)
for query in result.rewritten_queries:
print(query)
@@ -288,8 +287,7 @@ Improved version:"""
backstory="You are an expert at understanding user intent and transforming queries for optimal information retrieval.",
tools=self.tools,
llm=self.model,
verbose=self.verbose,
markdown=False
output={"verbose": self.verbose, "markdown": False}
)
return self._agent
@@ -44,7 +44,7 @@ class RouterAgent(Agent):
# Initialize model router
self.model_router = model_router or ModelRouter()
self.routing_strategy = routing_strategy
self.fallback_model = fallback_model or os.getenv('OPENAI_MODEL_NAME', 'gpt-5-nano')
self.fallback_model = fallback_model or os.getenv('OPENAI_MODEL_NAME', 'gpt-4o-mini')
# Process models configuration
self.available_models = self._process_models_config(models)
@@ -65,6 +65,52 @@ def process_video(video_path: str, seconds_per_frame=2):
video.release()
return base64_frames
def get_multimodal_message(text_prompt: str, images: list) -> list:
"""
Build multimodal message content for LLM with text and images.
DRY helper - replaces duplicate _get_multimodal_message in aexecute_task/execute_task.
Args:
text_prompt: The text content of the message
images: List of image paths (local or URL)
Returns:
List of content items for multimodal LLM message
"""
content = [{"type": "text", "text": text_prompt}]
for img in images:
# If local file path for a valid image
if os.path.exists(img):
ext = os.path.splitext(img)[1].lower()
# If it's a .mp4, convert to frames
if ext == ".mp4":
frames = process_video(img, seconds_per_frame=1)
content.append({"type": "text", "text": "These are frames from the video."})
for f in frames:
content.append({
"type": "image_url",
"image_url": {"url": f"data:image/jpg;base64,{f}"}
})
else:
encoded = encode_file_to_base64(img)
content.append({
"type": "image_url",
"image_url": {
"url": f"data:image/{ext.lstrip('.')};base64,{encoded}"
}
})
else:
# Treat as a remote URL
content.append({
"type": "image_url",
"image_url": {"url": img}
})
return content
def process_task_context(context_item, verbose=0, user_id=None):
"""
Process a single context item for task execution.
@@ -148,6 +194,7 @@ class Agents:
guardrails: Optional[Any] = None, # Union[bool, Callable, GuardrailConfig] - validation
web: Optional[Any] = None, # Union[bool, WebConfig] - web search/fetch
reflection: Optional[Any] = None, # Union[bool, ReflectionConfig] - self-reflection
caching: Optional[Any] = None, # Union[bool, CachingConfig] - caching
):
"""
Initialize PraisonAIAgents with consolidated feature parameters.
@@ -177,6 +224,7 @@ class Agents:
self._guardrails = guardrails
self._web = web
self._reflection = reflection
self._caching = caching
# ─────────────────────────────────────────────────────────────────────
# Extract values from consolidated params using UNIFIED CANONICAL resolver
# Precedence: Instance > Config > Dict > Array > String > Bool > Default
@@ -361,7 +409,7 @@ class Agents:
self.variables = variables if variables else {}
# Check for manager_llm in environment variable if not provided
self.manager_llm = manager_llm or os.getenv('OPENAI_MODEL_NAME', 'gpt-5-nano')
self.manager_llm = manager_llm or os.getenv('OPENAI_MODEL_NAME', 'gpt-4o-mini')
# Set logger level based on verbose
if _verbose >= 5:
@@ -464,10 +512,16 @@ class Agents:
if self.shared_memory:
for task in tasks:
if not task.memory:
task.memory = self.shared_memory
logger.info(f"Assigned shared memory to task {task.id}")
# Telemetry
try:
from ..telemetry import get_telemetry
self._telemetry = get_telemetry()
except (ImportError, AttributeError):
self._telemetry = None
def add_task(self, task):
task_id = self.task_id_counter
task.id = task_id
@@ -601,40 +655,9 @@ Context:
logger.debug(f"Starting execution of task {task_id} with prompt:\n{task_prompt}")
if task.images:
def _get_multimodal_message(text_prompt, images):
content = [{"type": "text", "text": text_prompt}]
for img in images:
# If local file path for a valid image
if os.path.exists(img):
ext = os.path.splitext(img)[1].lower()
# If it's a .mp4, convert to frames
if ext == ".mp4":
frames = process_video(img, seconds_per_frame=1)
content.append({"type": "text", "text": "These are frames from the video."})
for f in frames:
content.append({
"type": "image_url",
"image_url": {"url": f"data:image/jpg;base64,{f}"}
})
else:
encoded = encode_file_to_base64(img)
content.append({
"type": "image_url",
"image_url": {
"url": f"data:image/{ext.lstrip('.')};base64,{encoded}"
}
})
else:
# Treat as a remote URL
content.append({
"type": "image_url",
"image_url": {"url": img}
})
return content
# Use shared multimodal helper (DRY - defined at module level)
agent_output = await executor_agent.achat(
_get_multimodal_message(task_prompt, task.images),
get_multimodal_message(task_prompt, task.images),
tools=tools,
output_json=task.output_json,
output_pydantic=task.output_pydantic,
@@ -819,13 +842,17 @@ Context:
await loop.run_in_executor(None, self.run_task, task_id)
async def astart(self, content=None, return_dict=False, **kwargs):
"""Async version of start method
"""Async version of start method.
Args:
content: Optional content to add to all tasks' context
return_dict: If True, returns the full results dictionary instead of only the final response
**kwargs: Additional arguments
"""
# Track execution via telemetry
if hasattr(self, '_telemetry') and self._telemetry:
self._telemetry.track_agent_execution(self.name, success=True, async_mode=True)
if content:
# Add content to context of all tasks
for task in self.tasks.values():
@@ -959,40 +986,9 @@ Context:
logger.debug(f"Starting execution of task {task_id} with prompt:\n{task_prompt}")
if task.images:
def _get_multimodal_message(text_prompt, images):
content = [{"type": "text", "text": text_prompt}]
for img in images:
# If local file path for a valid image
if os.path.exists(img):
ext = os.path.splitext(img)[1].lower()
# If it's a .mp4, convert to frames
if ext == ".mp4":
frames = process_video(img, seconds_per_frame=1)
content.append({"type": "text", "text": "These are frames from the video."})
for f in frames:
content.append({
"type": "image_url",
"image_url": {"url": f"data:image/jpg;base64,{f}"}
})
else:
encoded = encode_file_to_base64(img)
content.append({
"type": "image_url",
"image_url": {
"url": f"data:image/{ext.lstrip('.')};base64,{encoded}"
}
})
else:
# Treat as a remote URL
content.append({
"type": "image_url",
"image_url": {"url": img}
})
return content
# Use shared multimodal helper (DRY - defined at module level)
agent_output = executor_agent.chat(
_get_multimodal_message(task_prompt, task.images),
get_multimodal_message(task_prompt, task.images),
tools=task.tools,
output_json=task.output_json,
output_pydantic=task.output_pydantic,
@@ -1191,30 +1187,147 @@ Context:
return str(agent[0])
return None
def start(self, content=None, return_dict=False, **kwargs):
"""Start agent execution with optional content and config
def start(self, content=None, return_dict=False, output=None, **kwargs):
"""Start agent execution with verbose output (beginner-friendly).
Shows Rich panels with workflow progress when in TTY. Use .run() for
silent execution in production/scripts.
Args:
content: Optional content to add to all tasks' context
return_dict: If True, returns the full results dictionary instead of only the final response
return_dict: If True, returns the full results dictionary
output: Output preset - "silent", "verbose", "normal", etc.
Default in TTY: "verbose" (shows progress)
Default non-TTY: "silent"
**kwargs: Additional arguments
Example:
```python
# Interactive - shows Rich panels
agents = Agents(agents=[agent1, agent2])
result = agents.start() # Verbose output by default
# Force silent mode
result = agents.start(output="silent")
```
"""
# Track execution via telemetry
if hasattr(self, '_telemetry') and self._telemetry:
self._telemetry.track_agent_execution(self.name, success=True)
import sys
from ..main import PRAISON_COLORS
# Determine if we're in an interactive TTY
is_tty = sys.stdout.isatty()
# Resolve output mode (TTY-aware)
if output is None:
# Default: verbose in TTY (beginner-friendly), silent otherwise
# Note: Don't check self.verbose here - start() is for interactive use
show_verbose = is_tty
elif output == "silent":
show_verbose = False
elif output in ("verbose", "debug", "normal"):
show_verbose = True
else:
show_verbose = is_tty
# Add content to context if provided
if content:
# Add content to context of all tasks
for task in self.tasks.values():
if isinstance(content, (str, list)):
# If context is empty, initialize it
if not task.context:
task.context = []
# Add content to context
task.context.append(content)
# Planning Mode: Create plan and todo list before execution
if self.planning:
self._run_with_planning()
# ─────────────────────────────────────────────────────────────
# Verbose Mode: Show Rich panels for multi-agent workflow
# ─────────────────────────────────────────────────────────────
if show_verbose and is_tty:
from rich.panel import Panel
from rich.text import Text
console = Console()
import time as time_module
# Show workflow overview panel
agent_names = " → ".join([a.name for a in self.agents])
workflow_info = f"[bold {PRAISON_COLORS['metrics']}]Process:[/] {self.process}\n"
workflow_info += f"[bold {PRAISON_COLORS['metrics']}]Agents:[/] {agent_names}"
console.print(Panel(
workflow_info,
title="[bold]Multi-Agent Workflow[/]",
border_style=PRAISON_COLORS["agent"],
padding=(1, 2)
))
console.print()
# Execute tasks with verbose output
total_agents = len(self.agents)
workflow_start_time = time_module.time()
for idx, (task_id, task) in enumerate(self.tasks.items(), 1):
agent = task.agent
agent_name = agent.name if agent else "Unknown"
agent_model = getattr(agent, 'llm', 'gpt-4o-mini') if agent else "unknown"
# Show agent task panel with model info
task_desc = task.description[:100] + "..." if len(task.description) > 100 else task.description
panel_content = f"[bold {PRAISON_COLORS['task_text']}]📋 Task:[/] {task_desc}\n"
panel_content += f"[dim]🤖 Model: {agent_model}[/dim]"
console.print(Panel.fit(
panel_content,
title=f"[bold]Agent [{idx}/{total_agents}]: {agent_name}[/]",
border_style=PRAISON_COLORS["task"]
))
# Execute with timing and status
start_time = time_module.time()
# Show working spinner
with console.status(
f"[bold yellow]Working...[/] {agent_name} generating response...",
spinner="dots",
spinner_style="yellow"
):
# Run the task
if self.planning:
self._run_with_planning()
break # Planning mode handles all tasks
else:
self.run_task(task_id)
elapsed = time_module.time() - start_time
# Show response panel - FULL response, no truncation
result = self.get_task_result(task_id)
if result:
response_text = str(result.raw)
# No truncation - show full response in verbose mode
from rich.markdown import Markdown
console.print(Panel(
Markdown(response_text),
title=f"[bold]Agent [{idx}/{total_agents}] Complete ({elapsed:.1f}s)[/]",
border_style=PRAISON_COLORS["response"],
padding=(1, 2)
))
console.print()
# Workflow summary panel
total_elapsed = time_module.time() - workflow_start_time
console.print(Panel.fit(
f"[bold green]Total Time:[/] {total_elapsed:.1f}s\n"
f"[bold green]Agents Run:[/] {total_agents}/{total_agents}",
title="[bold]✅ Workflow Complete[/]",
border_style="green"
))
console.print()
else:
# Run tasks as before
self.run_all_tasks()
# Silent mode: Run tasks without display
if self.planning:
self._run_with_planning()
else:
self.run_all_tasks()
# Auto-display token metrics if any agent has metrics=True
metrics_enabled = any(getattr(agent, 'metrics', False) for agent in self.agents)
@@ -1222,10 +1335,8 @@ Context:
try:
self.display_token_usage()
except (ImportError, AttributeError) as e:
# Token tracking not available or not properly configured
logging.debug(f"Could not auto-display token usage: {e}")
except Exception as e:
# Log unexpected errors for debugging
logging.debug(f"Unexpected error in token metrics display: {e}")
# Get results
@@ -1236,7 +1347,6 @@ Context:
# By default, return only the final agent's response
if not return_dict:
# Get the last task (assuming sequential processing)
task_ids = list(self.tasks.keys())
if task_ids:
last_task_id = task_ids[-1]
@@ -1244,12 +1354,21 @@ Context:
if last_result:
return last_result.raw
# Return full results dict if return_dict is True or if no final result was found
return results
def run(self, content=None, return_dict=False, **kwargs):
"""Alias for start() method to provide consistent API with Agent class"""
return self.start(content=content, return_dict=return_dict, **kwargs)
"""Run agents silently (production use).
Unlike .start() which shows verbose output, .run() executes silently
for programmatic/production use.
Args:
content: Optional content to add to all tasks' context
return_dict: If True, returns the full results dictionary
**kwargs: Additional arguments
"""
# Always run silently - no verbose output
return self.start(content=content, return_dict=return_dict, output="silent", **kwargs)
def set_state(self, key: str, value: Any) -> None:
"""Set a state value"""
@@ -2189,7 +2308,7 @@ Context:
# Inherit from original task if available
memory=original_task.memory if original_task else None,
callback=original_task.callback if original_task else None,
guardrail=original_task.guardrail if original_task else None,
guardrails=original_task.guardrail if original_task else None,
max_retries=original_task.max_retries if original_task else 3,
output_json=original_task.output_json if original_task else None,
output_pydantic=original_task.output_pydantic if original_task else None,
@@ -74,6 +74,17 @@ class AutoAgents(Agents):
api_key: Optional[str] = None,
max_agents: int = 3, # Maximum number of agents
autonomy: Optional[Any] = None, # Union[bool, dict, AutonomyConfig] - Enable autonomy for all agents
# ============================================================
# CONSOLIDATED PARAMS (new SDK contract - take precedence)
# ============================================================
output: Optional[Any] = None, # Union[str, OutputConfig] - overrides verbose/markdown
reflection: Optional[Any] = None, # Union[bool, ReflectionConfig] - overrides self_reflect/max_reflect/min_reflect/reflect_llm
caching: Optional[Any] = None, # Union[bool, CachingConfig] - overrides cache
knowledge: Optional[Any] = None, # Union[bool, List[str], KnowledgeConfig] - overrides knowledge_sources/embedder_config
execution: Optional[Any] = None, # Union[str, ExecutionConfig] - overrides max_iter
guardrails: Optional[Any] = None, # Union[bool, Callable, GuardrailConfig]
web: Optional[Any] = None, # Union[bool, WebConfig]
hooks: Optional[Any] = None, # HooksConfig
):
"""Initialize AutoAgents with configuration for automatic agent and task creation."""
if max_agents < 1:
@@ -92,7 +103,7 @@ class AutoAgents(Agents):
self.self_reflect = self_reflect
self.max_reflect = max_reflect
self.min_reflect = min_reflect
self.llm = llm or os.getenv('OPENAI_MODEL_NAME', 'gpt-5-nano')
self.llm = llm or os.getenv('OPENAI_MODEL_NAME', 'gpt-4o-mini')
self.function_calling_llm = function_calling_llm
self.code_execution_mode = code_execution_mode
self.embedder_config = embedder_config
@@ -112,6 +123,16 @@ class AutoAgents(Agents):
self.api_key = api_key
self.autonomy = autonomy # Store autonomy config for agents
# Store consolidated params (new SDK contract - take precedence over legacy)
self._output = output
self._reflection = reflection
self._caching = caching
self._knowledge = knowledge
self._execution = execution
self._guardrails = guardrails
self._web = web
self._hooks = hooks
# Display initial instruction
if self.verbose:
display_instruction(f"🎯 Main Task: {self.instructions}")
@@ -129,14 +150,21 @@ class AutoAgents(Agents):
self._display_agents_and_tasks(agents, tasks)
# Initialize parent class with generated agents and tasks
# Use consolidated output= instead of legacy verbose=
super().__init__(
agents=agents,
tasks=tasks,
verbose=verbose,
completion_checker=completion_checker,
max_retries=max_retries,
process=process,
manager_llm=manager_llm or self.llm
manager_llm=manager_llm or self.llm,
output=self._output, # Consolidated output param
execution=self._execution,
hooks=self._hooks,
memory=self.memory,
reflection=self._reflection,
caching=self._caching,
guardrails=self._guardrails,
web=self._web,
autonomy=self.autonomy,
)
def _display_agents_and_tasks(self, agents: List[Agent], tasks: List[Task]):
@@ -431,38 +459,33 @@ DO NOT use strings for tasks. Each task MUST be a complete object with all four
agent_tools = self._assign_tools_to_agent(agent_config)
# Create the agent with all parameters
# Consolidated params take precedence over legacy params
# Use consolidated knowledge if provided, else fall back to legacy knowledge_sources
effective_knowledge = self._knowledge if self._knowledge is not None else self.knowledge_sources
agent = Agent(
name=agent_config.name,
role=agent_config.role,
goal=agent_config.goal,
backstory=agent_config.backstory,
tools=agent_tools, # Use assigned tools
verbose=self.verbose >= 1,
allow_code_execution=self.allow_code_execution,
memory=self.memory,
markdown=self.markdown,
self_reflect=self.self_reflect,
max_reflect=self.max_reflect,
min_reflect=self.min_reflect,
llm=self.llm,
function_calling_llm=self.function_calling_llm,
llm=self.llm, # Consolidated LLM param
code_execution_mode=self.code_execution_mode,
embedder_config=self.embedder_config,
knowledge=self.knowledge_sources,
use_system_prompt=self.use_system_prompt,
cache=self.cache,
allow_delegation=self.allow_delegation,
step_callback=self.step_callback,
system_template=self.system_template,
prompt_template=self.prompt_template,
response_template=self.response_template,
max_rpm=self.max_rpm,
max_execution_time=self.max_execution_time,
max_iter=self.max_iter,
reflect_llm=self.reflect_llm,
base_url=self.base_url,
api_key=self.api_key,
autonomy=self.autonomy # Pass autonomy config to agents
# Consolidated params (new SDK contract)
output=self._output, # Overrides verbose/markdown
reflection=self._reflection, # Overrides self_reflect/max_reflect/min_reflect/reflect_llm
caching=self._caching, # Overrides cache
knowledge=effective_knowledge, # Overrides knowledge_sources/embedder_config
execution=self._execution, # Overrides max_iter
guardrails=self._guardrails,
web=self._web,
hooks=self._hooks,
autonomy=self.autonomy,
)
agents.append(agent)
@@ -376,7 +376,7 @@ class SubagentDelegator:
return result
async def _create_agent(self, profile: AgentProfile, task: DelegationTask) -> Any:
"""Create an agent from profile."""
"""Create an agent from profile using consolidated params."""
if self.agent_factory:
return self.agent_factory(profile.name)
@@ -387,14 +387,17 @@ class SubagentDelegator:
# Filter tools based on profile
tools = self._get_tools_for_profile(profile)
# Use consolidated params (protocol-compliant)
# output='silent' replaces verbose=False
# execution={max_iter: N} replaces max_iter=N
agent = Agent(
name=profile.name,
role=profile.name.title(),
goal=task.objective,
backstory=profile.system_prompt,
tools=tools,
verbose=False,
max_iter=min(task.max_steps, profile.max_steps),
output='silent',
execution={'max_iter': min(task.max_steps, profile.max_steps)},
)
return agent
@@ -13,10 +13,35 @@ from typing import Dict, Set, Optional, Callable, Any, Literal, List
from functools import wraps
from contextvars import ContextVar
from dataclasses import dataclass, field
from rich.console import Console
from rich.panel import Panel
from rich.text import Text
from rich.prompt import Confirm
# Lazy import for rich components
_rich_console = None
_rich_panel = None
_rich_confirm = None
def _get_rich_console():
"""Lazy import rich Console."""
global _rich_console
if _rich_console is None:
from rich.console import Console
_rich_console = Console
return _rich_console
def _get_rich_panel():
"""Lazy import rich Panel."""
global _rich_panel
if _rich_panel is None:
from rich.panel import Panel
_rich_panel = Panel
return _rich_panel
def _get_rich_confirm():
"""Lazy import rich Confirm."""
global _rich_confirm
if _rich_confirm is None:
from rich.prompt import Confirm
_rich_confirm = Confirm
return _rich_confirm
# Global registries for approval requirements
APPROVAL_REQUIRED_TOOLS: Set[str] = set()
@@ -308,6 +333,10 @@ def console_approval_callback(function_name: str, arguments: Dict[str, Any], ris
Displays tool information and prompts user for approval via console.
"""
Console = _get_rich_console()
Panel = _get_rich_panel()
Confirm = _get_rich_confirm()
console = Console()
# Create risk level styling
@@ -44,12 +44,79 @@ class MemoryBackend(str, Enum):
MONGODB = "mongodb"
class LearnScope(str, Enum):
"""Scope for learning data visibility.
PRIVATE: Learning data is private to this user/agent (default, safest)
SHARED: Learning data is shared with all agents
"""
PRIVATE = "private" # Private to this user/agent
SHARED = "shared" # Shared with all agents
@dataclass
class LearnConfig:
"""
Configuration for continuous learning within memory system.
Learning captures patterns, preferences, and insights from agent interactions
to improve future responses. All learning data is stored within the memory system.
Usage:
# Simple enable
Agent(memory=MemoryConfig(learn=True))
# With specific capabilities
Agent(memory=MemoryConfig(
learn=LearnConfig(
persona=True, # User preferences
insights=True, # Observations
patterns=True, # Reusable knowledge
)
))
"""
# Learning capabilities
persona: bool = True # User preferences and profile
insights: bool = True # Observations and learnings
thread: bool = True # Session/conversation context
patterns: bool = False # Reusable knowledge patterns
decisions: bool = False # Decision logging
feedback: bool = False # Outcome signals
improvements: bool = False # Self-improvement proposals
# Scope configuration
scope: Union[str, LearnScope] = LearnScope.PRIVATE
# Storage configuration
store_path: Optional[str] = None # Custom storage path
# Maintenance settings
auto_consolidate: bool = True # Auto-consolidate learnings
retention_days: Optional[int] = None # Days to retain (None = forever)
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary."""
return {
"persona": self.persona,
"insights": self.insights,
"thread": self.thread,
"patterns": self.patterns,
"decisions": self.decisions,
"feedback": self.feedback,
"improvements": self.improvements,
"scope": self.scope.value if isinstance(self.scope, LearnScope) else self.scope,
"store_path": self.store_path,
"auto_consolidate": self.auto_consolidate,
"retention_days": self.retention_days,
}
@dataclass
class MemoryConfig:
"""
Configuration for agent memory and session management.
Consolidates: memory, auto_memory, claude_memory, user_id, session_id, db
Consolidates: memory, auto_memory, claude_memory, user_id, session_id, db, learn
Usage:
# Simple enable (uses FileMemory)
@@ -58,12 +125,22 @@ class MemoryConfig:
# With backend
Agent(memory=MemoryConfig(backend="redis"))
# Full config
# Full config with learning
Agent(memory=MemoryConfig(
backend="sqlite",
user_id="user123",
session_id="session456",
auto_memory=True,
learn=True, # Enable continuous learning
))
# With detailed learn config
Agent(memory=MemoryConfig(
learn=LearnConfig(
persona=True,
insights=True,
patterns=True,
)
))
"""
# Backend selection
@@ -85,8 +162,17 @@ class MemoryConfig:
# Memory configuration dict (for provider-specific settings)
config: Optional[Dict[str, Any]] = None
# Continuous learning configuration
learn: Optional[Union[bool, LearnConfig]] = None
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary."""
learn_dict = None
if self.learn is True:
learn_dict = LearnConfig().to_dict()
elif isinstance(self.learn, LearnConfig):
learn_dict = self.learn.to_dict()
return {
"backend": self.backend.value if isinstance(self.backend, MemoryBackend) else self.backend,
"user_id": self.user_id,
@@ -94,6 +180,7 @@ class MemoryConfig:
"auto_memory": self.auto_memory,
"claude_memory": self.claude_memory,
"config": self.config,
"learn": learn_dict,
}
@@ -391,10 +478,21 @@ class OutputConfig:
Consolidates: verbose, markdown, stream, metrics, reasoning_steps, output_style
DEFAULT: output="silent" (zero overhead, fastest performance)
Usage:
# Simple preset
# Default is silent mode (no output overhead, programmatic use)
Agent(instructions="...") # Uses output="silent"
# Actions mode (tool calls + final output trace)
Agent(output="actions")
# Verbose mode with Rich panels
Agent(output="verbose")
# JSON mode for piping
Agent(output="json")
# With config
Agent(output=OutputConfig(
verbose=True,
@@ -404,11 +502,11 @@ class OutputConfig:
reasoning_steps=True,
))
"""
# Verbosity
verbose: bool = True
# Verbosity - False by default for silent mode (fastest)
verbose: bool = False
# Formatting
markdown: bool = True
# Formatting - False by default for silent mode
markdown: bool = False
# Streaming
stream: bool = False
@@ -422,6 +520,24 @@ class OutputConfig:
# Output style (custom styling)
style: Optional[Any] = None
# Actions trace mode - shows tool calls, agent lifecycle, final output
# When True, registers callbacks and outputs to stderr
# DEFAULT: False for zero overhead (silent mode)
actions_trace: bool = False
# JSON output mode - emit JSONL events for piping
json_output: bool = False
# Simple output mode - just print response without panels
simple_output: bool = False
# Show LLM parameters (for debug mode)
show_parameters: bool = False
# Status trace mode - clean inline status updates
# Shows: [timestamp] Calling LLM..., Executing tool..., Response: ...
status_trace: bool = False
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary."""
return {
@@ -430,6 +546,11 @@ class OutputConfig:
"stream": self.stream,
"metrics": self.metrics,
"reasoning_steps": self.reasoning_steps,
"actions_trace": self.actions_trace,
"json_output": self.json_output,
"simple_output": self.simple_output,
"show_parameters": self.show_parameters,
"status_trace": self.status_trace,
}
@@ -857,9 +978,259 @@ SkillsParam = Union[List[str], SkillsConfig]
AutonomyParam = Union[bool, Dict[str, Any], AutonomyConfig]
# =============================================================================
# PRECEDENCE LADDER RESOLVERS
# =============================================================================
# Precedence: Instance > Config > Array > Dict > String > Bool > Default
#
# These resolvers normalize various input types into the canonical Config form.
# This allows users to pass simplified inputs while maintaining type safety.
def resolve_memory(value: MemoryParam) -> Optional[MemoryConfig]:
"""
Resolve memory= parameter following precedence ladder.
Precedence: Instance > Config > Dict > String > Bool > Default
Args:
value: Memory parameter in any supported form
Returns:
MemoryConfig if enabled, None if disabled
Examples:
>>> resolve_memory(None) # Default: disabled
None
>>> resolve_memory(False) # Explicit disable
None
>>> resolve_memory(True) # Enable with defaults
MemoryConfig(backend='file')
>>> resolve_memory("redis") # String shorthand
MemoryConfig(backend='redis')
>>> resolve_memory({"backend": "sqlite", "user_id": "alice"}) # Dict
MemoryConfig(backend='sqlite', user_id='alice')
>>> resolve_memory(MemoryConfig(backend="postgres")) # Config passthrough
MemoryConfig(backend='postgres')
"""
# Default: disabled
if value is None:
return None
# Bool: False = disabled, True = defaults
if value is False:
return None
if value is True:
return MemoryConfig()
# String: backend shorthand
if isinstance(value, str):
try:
backend = MemoryBackend(value.lower())
except ValueError:
backend = value # Allow custom backend strings
return MemoryConfig(backend=backend)
# Dict: expand to config
if isinstance(value, dict):
# Handle backend enum conversion
backend = value.get("backend", MemoryBackend.FILE)
if isinstance(backend, str):
try:
backend = MemoryBackend(backend.lower())
except ValueError:
pass # Keep as string for custom backends
return MemoryConfig(
backend=backend,
user_id=value.get("user_id"),
session_id=value.get("session_id"),
auto_memory=value.get("auto_memory", False),
claude_memory=value.get("claude_memory", False),
db=value.get("db"),
config=value.get("config"),
)
# Config: passthrough
if isinstance(value, MemoryConfig):
return value
# Instance (MemoryManager): return as-is (caller handles)
# This is the highest precedence - user provided a pre-configured instance
return value
def resolve_knowledge(value: KnowledgeParam) -> Optional[KnowledgeConfig]:
"""
Resolve knowledge= parameter following precedence ladder.
Precedence: Instance > Config > Array > Dict > String > Bool > Default
Args:
value: Knowledge parameter in any supported form
Returns:
KnowledgeConfig if enabled, None if disabled
"""
# Default: disabled
if value is None:
return None
# Bool: False = disabled, True = defaults
if value is False:
return None
if value is True:
return KnowledgeConfig()
# String: single source
if isinstance(value, str):
return KnowledgeConfig(sources=[value])
# Array: list of sources
if isinstance(value, list):
return KnowledgeConfig(sources=value)
# Dict: expand to config
if isinstance(value, dict):
return KnowledgeConfig(
sources=value.get("sources", []),
embedder=value.get("embedder", "openai"),
embedder_config=value.get("embedder_config"),
chunking_strategy=value.get("chunking_strategy", ChunkingStrategy.SEMANTIC),
chunk_size=value.get("chunk_size", 1000),
chunk_overlap=value.get("chunk_overlap", 200),
retrieval_k=value.get("retrieval_k", 5),
retrieval_threshold=value.get("retrieval_threshold", 0.0),
rerank=value.get("rerank", False),
rerank_model=value.get("rerank_model"),
auto_retrieve=value.get("auto_retrieve", True),
)
# Config: passthrough
if isinstance(value, KnowledgeConfig):
return value
# Instance: return as-is
return value
def resolve_planning(value: PlanningParam) -> Optional[PlanningConfig]:
"""Resolve planning= parameter following precedence ladder."""
if value is None or value is False:
return None
if value is True:
return PlanningConfig()
if isinstance(value, dict):
return PlanningConfig(**value)
if isinstance(value, PlanningConfig):
return value
return value
def resolve_reflection(value: ReflectionParam) -> Optional[ReflectionConfig]:
"""Resolve reflection= parameter following precedence ladder."""
if value is None or value is False:
return None
if value is True:
return ReflectionConfig()
if isinstance(value, dict):
return ReflectionConfig(**value)
if isinstance(value, ReflectionConfig):
return value
return value
def resolve_guardrails(value: GuardrailParam) -> Optional[GuardrailConfig]:
"""Resolve guardrails= parameter following precedence ladder."""
if value is None or value is False:
return None
if value is True:
return GuardrailConfig()
if callable(value):
return GuardrailConfig(validator=value)
if isinstance(value, dict):
return GuardrailConfig(**value)
if isinstance(value, GuardrailConfig):
return value
return value
def resolve_web(value: WebParam) -> Optional[WebConfig]:
"""Resolve web= parameter following precedence ladder."""
if value is None or value is False:
return None
if value is True:
return WebConfig()
if isinstance(value, dict):
return WebConfig(**value)
if isinstance(value, WebConfig):
return value
return value
def resolve_output(value: OutputParam) -> Optional[OutputConfig]:
"""Resolve output= parameter following precedence ladder."""
if value is None:
return None
if isinstance(value, str):
try:
preset = OutputPreset(value.lower())
return OutputConfig.from_preset(preset)
except ValueError:
return None
if isinstance(value, dict):
return OutputConfig(**value)
if isinstance(value, OutputConfig):
return value
return value
def resolve_execution(value: ExecutionParam) -> Optional[ExecutionConfig]:
"""Resolve execution= parameter following precedence ladder."""
if value is None:
return None
if isinstance(value, str):
try:
preset = ExecutionPreset(value.lower())
return ExecutionConfig.from_preset(preset)
except ValueError:
return None
if isinstance(value, dict):
return ExecutionConfig(**value)
if isinstance(value, ExecutionConfig):
return value
return value
def resolve_caching(value: CachingParam) -> Optional[CachingConfig]:
"""Resolve caching= parameter following precedence ladder."""
if value is None or value is False:
return None
if value is True:
return CachingConfig()
if isinstance(value, dict):
return CachingConfig(**value)
if isinstance(value, CachingConfig):
return value
return value
def resolve_autonomy(value: AutonomyParam) -> Optional[AutonomyConfig]:
"""Resolve autonomy= parameter following precedence ladder."""
if value is None or value is False:
return None
if value is True:
return AutonomyConfig()
if isinstance(value, dict):
return AutonomyConfig(**value)
if isinstance(value, AutonomyConfig):
return value
return value
__all__ = [
# Enums
"MemoryBackend",
"LearnScope",
"ChunkingStrategy",
"GuardrailAction",
"WebSearchProvider",
@@ -867,6 +1238,7 @@ __all__ = [
"ExecutionPreset",
"AutonomyLevel",
# Config classes (Agent)
"LearnConfig",
"MemoryConfig",
"KnowledgeConfig",
"PlanningConfig",
@@ -900,4 +1272,15 @@ __all__ = [
"HooksParam",
"SkillsParam",
"AutonomyParam",
# Precedence ladder resolvers
"resolve_memory",
"resolve_knowledge",
"resolve_planning",
"resolve_reflection",
"resolve_guardrails",
"resolve_web",
"resolve_output",
"resolve_execution",
"resolve_caching",
"resolve_autonomy",
]
@@ -39,8 +39,107 @@ MEMORY_URL_SCHEMES: Dict[str, str] = {
# =============================================================================
# Output Presets
# =============================================================================
#
# Preset Hierarchy (from least to most output):
# silent → Nothing (default for SDK, max performance)
# text → Response only, simple format
# verbose → Task + Tools inline + Response panel
# debug → verbose + metrics footer (tokens, cost, model)
# stream → Real-time token streaming
# json → Machine-readable JSONL events
#
# =============================================================================
OUTPUT_PRESETS: Dict[str, Dict[str, Any]] = {
# Silent preset - DEFAULT for SDK
# Zero overhead, no callbacks, no I/O on hot path
# Fastest performance for programmatic use
"silent": {
"verbose": False,
"markdown": False,
"stream": False,
"metrics": False,
"reasoning_steps": False,
"actions_trace": False,
},
# Status preset - Shows tool calls and response without timestamps
# Simple progress indicator: ▸ tool → result ✓
"status": {
"verbose": False,
"markdown": False,
"stream": False,
"metrics": False,
"reasoning_steps": False,
"actions_trace": True,
"simple_output": True, # Simple format without timestamps
},
# Trace preset - Full execution trace with timestamps
# Shows: [HH:MM:SS] ▸ tool → result [0.2s] ✓
# Ideal for debugging and monitoring
"trace": {
"verbose": False,
"markdown": False,
"stream": False,
"metrics": False,
"reasoning_steps": False,
"actions_trace": True,
"status_trace": True, # Enable timestamped status output
},
# Verbose preset - Full interactive output with panels
# Shows: Task prompt, Tool calls (inline), Response panel
# Best for interactive terminal use
"verbose": {
"verbose": True,
"markdown": True,
"stream": False,
"metrics": False,
"reasoning_steps": False,
},
# Debug preset - trace + metrics (NO boxes)
# Shows: [timestamp] tool calls, metrics footer, reasoning steps
# Best for developers debugging agent behavior
"debug": {
"verbose": False, # No boxes
"markdown": False,
"stream": False,
"metrics": True, # Shows metrics footer
"reasoning_steps": True,
"actions_trace": True,
"status_trace": True, # Timestamps like trace
"show_parameters": True,
},
# Streaming preset - enables streaming by default
"stream": {
"verbose": True,
"markdown": True,
"stream": True,
"metrics": False,
"reasoning_steps": False,
},
# JSON preset - JSONL output for piping/scripting
"json": {
"verbose": False,
"markdown": False,
"stream": False,
"metrics": False,
"reasoning_steps": False,
"actions_trace": True,
"json_output": True, # Output as JSONL
},
# ==========================================================================
# ALIASES (for backward compatibility)
# ==========================================================================
# plain → silent (identical behavior)
"plain": {
"verbose": False,
"markdown": False,
"stream": False,
"metrics": False,
"reasoning_steps": False,
"actions_trace": False,
},
# minimal → silent (identical behavior)
"minimal": {
"verbose": False,
"markdown": False,
@@ -48,6 +147,7 @@ OUTPUT_PRESETS: Dict[str, Dict[str, Any]] = {
"metrics": False,
"reasoning_steps": False,
},
# normal → verbose (consolidated)
"normal": {
"verbose": True,
"markdown": True,
@@ -55,29 +155,34 @@ OUTPUT_PRESETS: Dict[str, Dict[str, Any]] = {
"metrics": False,
"reasoning_steps": False,
},
"verbose": {
"verbose": True,
"markdown": True,
"stream": False,
"metrics": True,
"reasoning_steps": True,
},
"debug": {
"verbose": True,
"markdown": True,
"stream": False,
"metrics": True,
"reasoning_steps": True,
},
"silent": {
# actions → status (renamed for clarity)
"actions": {
"verbose": False,
"markdown": False,
"stream": False,
"metrics": False,
"reasoning_steps": False,
"actions_trace": True,
"simple_output": True,
},
# text → status (old name, kept for backward compat)
"text": {
"verbose": False,
"markdown": False,
"stream": False,
"metrics": False,
"reasoning_steps": False,
"actions_trace": True,
"simple_output": True,
},
}
# Default output mode - can be overridden by PRAISONAI_OUTPUT env var
# "silent" = zero overhead, fastest performance (DEFAULT)
# "verbose" = full interactive display
# "debug" = verbose + metrics
DEFAULT_OUTPUT_MODE = "silent"
# =============================================================================
# Execution Presets
@@ -157,6 +157,16 @@ __all__ = [
"FastContextResult",
"FileMatch",
"LineRange",
# Artifacts (Dynamic Context Discovery)
"ArtifactRef",
"ArtifactMetadata",
"GrepMatch",
"QueueConfig",
"ArtifactStoreProtocol",
"HistoryStoreProtocol",
"TerminalLoggerProtocol",
"compute_checksum",
"generate_summary",
]
@@ -252,4 +262,11 @@ def __getattr__(name: str):
from praisonaiagents.context.fast.result import LineRange
return LineRange
# Artifacts (Dynamic Context Discovery)
if name in ("ArtifactRef", "ArtifactMetadata", "GrepMatch", "QueueConfig",
"ArtifactStoreProtocol", "HistoryStoreProtocol", "TerminalLoggerProtocol",
"compute_checksum", "generate_summary"):
from . import artifacts
return getattr(artifacts, name)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
@@ -0,0 +1,580 @@
"""
Artifact Types and Protocols for Dynamic Context Discovery.
This module provides lightweight types and protocols for artifact-based
context management. Implementations live in the wrapper (praisonai).
Zero Performance Impact:
- Pure dataclasses and protocols
- No heavy imports
- No I/O at import time
Usage:
from praisonaiagents.context.artifacts import ArtifactRef, ArtifactStoreProtocol
# Create a reference to a stored artifact
ref = ArtifactRef(
path="/path/to/artifact.json",
summary="API response with 50,000 records",
size_bytes=1_200_000,
mime_type="application/json",
)
# Implement the protocol in wrapper
class FileSystemArtifactStore:
def store(self, content, metadata): ...
def load(self, ref): ...
"""
import time
import hashlib
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Protocol, Union, runtime_checkable
@dataclass
class ArtifactRef:
"""
Reference to a stored artifact.
Used to replace large tool outputs in context with a lightweight pointer.
The agent can then use tail/grep/load operations to access content on demand.
Attributes:
path: Absolute path to the artifact file
summary: Brief description or first ~200 chars of content
size_bytes: Original content size in bytes
mime_type: Content type (e.g., "application/json", "text/plain")
checksum: SHA256 hash for integrity verification
created_at: Unix timestamp when artifact was created
artifact_id: Unique identifier for the artifact
agent_id: ID of the agent that created this artifact
run_id: ID of the run/session this artifact belongs to
tool_name: Name of the tool that produced this output (if applicable)
turn_id: Conversation turn number when artifact was created
"""
path: str
summary: str
size_bytes: int
mime_type: str = "application/octet-stream"
checksum: str = ""
created_at: float = field(default_factory=time.time)
artifact_id: str = ""
agent_id: str = ""
run_id: str = ""
tool_name: Optional[str] = None
turn_id: int = 0
def to_inline(self) -> str:
"""
Generate inline representation for context.
Returns a compact string that can be included in the conversation
context, allowing the agent to reference the artifact.
"""
size_str = self._format_size(self.size_bytes)
tool_info = f" from {self.tool_name}" if self.tool_name else ""
return f"[Artifact{tool_info}: {self.path} ({size_str}) - {self.summary}]"
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary for serialization."""
return {
"path": self.path,
"summary": self.summary,
"size_bytes": self.size_bytes,
"mime_type": self.mime_type,
"checksum": self.checksum,
"created_at": self.created_at,
"artifact_id": self.artifact_id,
"agent_id": self.agent_id,
"run_id": self.run_id,
"tool_name": self.tool_name,
"turn_id": self.turn_id,
}
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "ArtifactRef":
"""Create from dictionary."""
return cls(
path=data.get("path", ""),
summary=data.get("summary", ""),
size_bytes=data.get("size_bytes", 0),
mime_type=data.get("mime_type", "application/octet-stream"),
checksum=data.get("checksum", ""),
created_at=data.get("created_at", time.time()),
artifact_id=data.get("artifact_id", ""),
agent_id=data.get("agent_id", ""),
run_id=data.get("run_id", ""),
tool_name=data.get("tool_name"),
turn_id=data.get("turn_id", 0),
)
@staticmethod
def _format_size(size_bytes: int) -> str:
"""Format size in human-readable form."""
if size_bytes < 1024:
return f"{size_bytes} B"
elif size_bytes < 1024 * 1024:
return f"{size_bytes / 1024:.1f} KB"
elif size_bytes < 1024 * 1024 * 1024:
return f"{size_bytes / (1024 * 1024):.1f} MB"
else:
return f"{size_bytes / (1024 * 1024 * 1024):.1f} GB"
@dataclass
class ArtifactMetadata:
"""
Metadata for artifact creation and tracking.
Passed to ArtifactStore.store() to provide context about the artifact.
"""
agent_id: str = ""
run_id: str = ""
tool_name: Optional[str] = None
turn_id: int = 0
content_type: str = "auto" # auto, json, text, binary
tags: List[str] = field(default_factory=list)
extra: Dict[str, Any] = field(default_factory=dict)
def to_dict(self) -> Dict[str, Any]:
return {
"agent_id": self.agent_id,
"run_id": self.run_id,
"tool_name": self.tool_name,
"turn_id": self.turn_id,
"content_type": self.content_type,
"tags": self.tags,
"extra": self.extra,
}
@dataclass
class GrepMatch:
"""A single match from grep operation."""
line_number: int
line_content: str
context_before: List[str] = field(default_factory=list)
context_after: List[str] = field(default_factory=list)
def to_dict(self) -> Dict[str, Any]:
return {
"line_number": self.line_number,
"line_content": self.line_content,
"context_before": self.context_before,
"context_after": self.context_after,
}
@runtime_checkable
class ArtifactStoreProtocol(Protocol):
"""
Protocol for artifact storage backends.
Implementations should handle:
- Storing content to persistent storage
- Loading content back
- Efficient tail/grep operations for large files
- Checksum verification
- Secret redaction (if enabled)
Example implementation in wrapper:
class FileSystemArtifactStore:
def store(self, content, metadata):
# Write to ~/.praison/runs/{run_id}/artifacts/
...
"""
def store(
self,
content: Any,
metadata: ArtifactMetadata,
) -> ArtifactRef:
"""
Store content as an artifact.
Args:
content: The content to store (will be serialized)
metadata: Metadata about the artifact
Returns:
ArtifactRef pointing to the stored artifact
"""
...
def load(self, ref: ArtifactRef) -> Any:
"""
Load full content from an artifact.
Args:
ref: Reference to the artifact
Returns:
The deserialized content
"""
...
def tail(self, ref: ArtifactRef, lines: int = 50) -> str:
"""
Get the last N lines of an artifact.
Args:
ref: Reference to the artifact
lines: Number of lines to return
Returns:
String containing the last N lines
"""
...
def head(self, ref: ArtifactRef, lines: int = 50) -> str:
"""
Get the first N lines of an artifact.
Args:
ref: Reference to the artifact
lines: Number of lines to return
Returns:
String containing the first N lines
"""
...
def grep(
self,
ref: ArtifactRef,
pattern: str,
context_lines: int = 2,
max_matches: int = 50,
) -> List[GrepMatch]:
"""
Search for pattern in artifact content.
Args:
ref: Reference to the artifact
pattern: Regex pattern to search for
context_lines: Number of context lines before/after match
max_matches: Maximum number of matches to return
Returns:
List of GrepMatch objects
"""
...
def chunk(
self,
ref: ArtifactRef,
start_line: int = 1,
end_line: Optional[int] = None,
) -> str:
"""
Get a chunk of lines from an artifact.
Args:
ref: Reference to the artifact
start_line: Starting line number (1-indexed)
end_line: Ending line number (inclusive), None for end of file
Returns:
String containing the requested lines
"""
...
def delete(self, ref: ArtifactRef) -> bool:
"""
Delete an artifact.
Args:
ref: Reference to the artifact
Returns:
True if deleted successfully
"""
...
def list_artifacts(
self,
run_id: Optional[str] = None,
agent_id: Optional[str] = None,
tool_name: Optional[str] = None,
) -> List[ArtifactRef]:
"""
List artifacts matching filters.
Args:
run_id: Filter by run ID
agent_id: Filter by agent ID
tool_name: Filter by tool name
Returns:
List of matching ArtifactRef objects
"""
...
@runtime_checkable
class HistoryStoreProtocol(Protocol):
"""
Protocol for conversation history storage.
Enables loss recovery during summarization by persisting
full conversation history to artifacts.
"""
def append(
self,
message: Dict[str, Any],
agent_id: str,
run_id: str,
) -> None:
"""
Append a message to history.
Args:
message: The message dict (role, content, etc.)
agent_id: ID of the agent
run_id: ID of the run/session
"""
...
def get_ref(self, agent_id: str, run_id: str) -> Optional[ArtifactRef]:
"""
Get artifact reference for history file.
Args:
agent_id: ID of the agent
run_id: ID of the run/session
Returns:
ArtifactRef if history exists, None otherwise
"""
...
def search(
self,
query: str,
agent_id: str,
run_id: str,
max_results: int = 20,
) -> List[Dict[str, Any]]:
"""
Search history for matching messages.
Args:
query: Search query (supports regex)
agent_id: ID of the agent
run_id: ID of the run/session
max_results: Maximum results to return
Returns:
List of matching message dicts
"""
...
def get_messages(
self,
agent_id: str,
run_id: str,
start_turn: int = 0,
end_turn: Optional[int] = None,
) -> List[Dict[str, Any]]:
"""
Get messages from history.
Args:
agent_id: ID of the agent
run_id: ID of the run/session
start_turn: Starting turn number
end_turn: Ending turn number (None for all)
Returns:
List of message dicts
"""
...
@runtime_checkable
class TerminalLoggerProtocol(Protocol):
"""
Protocol for terminal session logging.
Captures shell command outputs to artifacts for later retrieval.
"""
def log_command(
self,
command: str,
output: str,
exit_code: int,
agent_id: str,
run_id: str,
) -> ArtifactRef:
"""
Log a command execution.
Args:
command: The command that was executed
output: Combined stdout/stderr output
exit_code: Command exit code
agent_id: ID of the agent
run_id: ID of the run/session
Returns:
ArtifactRef to the logged output
"""
...
def get_session_ref(self, agent_id: str, run_id: str) -> Optional[ArtifactRef]:
"""
Get artifact reference for terminal session log.
Args:
agent_id: ID of the agent
run_id: ID of the run/session
Returns:
ArtifactRef if session exists, None otherwise
"""
...
def tail_session(
self,
agent_id: str,
run_id: str,
lines: int = 100,
) -> str:
"""
Get last N lines from terminal session.
Args:
agent_id: ID of the agent
run_id: ID of the run/session
lines: Number of lines to return
Returns:
String containing the last N lines
"""
...
def grep_session(
self,
pattern: str,
agent_id: str,
run_id: str,
max_matches: int = 50,
) -> List[GrepMatch]:
"""
Search terminal session for pattern.
Args:
pattern: Regex pattern to search for
agent_id: ID of the agent
run_id: ID of the run/session
max_matches: Maximum matches to return
Returns:
List of GrepMatch objects
"""
...
@dataclass
class QueueConfig:
"""
Configuration for tool output queuing.
Controls when and how large tool outputs are queued to artifacts
instead of being included inline in the context.
"""
enabled: bool = True
inline_max_bytes: int = 32 * 1024 # 32KB default
redact_secrets: bool = True
summary_max_chars: int = 200
checksum_algorithm: str = "sha256"
# Secret patterns to redact (regex)
secret_patterns: List[str] = field(default_factory=lambda: [
r'(?i)(api[_-]?key|apikey)["\']?\s*[:=]\s*["\']?[\w\-]+',
r'(?i)(secret|password|passwd|pwd)["\']?\s*[:=]\s*["\']?[\w\-]+',
r'(?i)(token|bearer)["\']?\s*[:=]\s*["\']?[\w\-]+',
r'(?i)(auth|authorization)["\']?\s*[:=]\s*["\']?[\w\-]+',
r'sk-[a-zA-Z0-9]{20,}', # OpenAI keys
r'ghp_[a-zA-Z0-9]{36}', # GitHub tokens
r'gho_[a-zA-Z0-9]{36}', # GitHub OAuth
r'xox[baprs]-[a-zA-Z0-9\-]+', # Slack tokens
])
def to_dict(self) -> Dict[str, Any]:
return {
"enabled": self.enabled,
"inline_max_bytes": self.inline_max_bytes,
"redact_secrets": self.redact_secrets,
"summary_max_chars": self.summary_max_chars,
"checksum_algorithm": self.checksum_algorithm,
"secret_patterns": self.secret_patterns,
}
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "QueueConfig":
return cls(
enabled=data.get("enabled", True),
inline_max_bytes=data.get("inline_max_bytes", 32 * 1024),
redact_secrets=data.get("redact_secrets", True),
summary_max_chars=data.get("summary_max_chars", 200),
checksum_algorithm=data.get("checksum_algorithm", "sha256"),
secret_patterns=data.get("secret_patterns", cls().secret_patterns),
)
def compute_checksum(content: Union[str, bytes], algorithm: str = "sha256") -> str:
"""
Compute checksum of content.
Args:
content: String or bytes to hash
algorithm: Hash algorithm (sha256, md5, etc.)
Returns:
Hex digest of the hash
"""
if isinstance(content, str):
content = content.encode("utf-8")
hasher = hashlib.new(algorithm)
hasher.update(content)
return hasher.hexdigest()
def generate_summary(content: Any, max_chars: int = 200) -> str:
"""
Generate a brief summary of content.
Args:
content: Content to summarize
max_chars: Maximum characters in summary
Returns:
Brief summary string
"""
if isinstance(content, (dict, list)):
# For structured data, describe the structure
if isinstance(content, dict):
keys = list(content.keys())[:5]
key_str = ", ".join(str(k) for k in keys)
if len(content) > 5:
key_str += f", ... ({len(content)} total keys)"
return f"Dict with keys: {key_str}"[:max_chars]
else:
if len(content) > 0:
first_type = type(content[0]).__name__
return f"List of {len(content)} {first_type} items"[:max_chars]
return "Empty list"
# For strings, take first N chars
text = str(content)
if len(text) <= max_chars:
return text
return text[:max_chars - 3] + "..."
@@ -7,19 +7,35 @@ Visual display with agents in center and tools on sides.
from typing import Dict, List, Set, Tuple
from collections import defaultdict
import threading
from rich.console import Console
from rich.panel import Panel
from rich.text import Text
from rich.align import Align
from rich.columns import Columns
from rich.table import Table
from rich import box
# Lazy imports for rich components
_rich_cache = {}
def _get_rich_components():
"""Lazy import all rich components."""
if not _rich_cache:
from rich.console import Console
from rich.panel import Panel
from rich.text import Text
from rich.align import Align
from rich.columns import Columns
from rich.table import Table
from rich import box
_rich_cache['Console'] = Console
_rich_cache['Panel'] = Panel
_rich_cache['Text'] = Text
_rich_cache['Align'] = Align
_rich_cache['Columns'] = Columns
_rich_cache['Table'] = Table
_rich_cache['box'] = box
return _rich_cache
class FlowDisplay:
"""Displays agent workflow with agents centered and tools on sides."""
def __init__(self):
self.console = Console()
rich = _get_rich_components()
self.console = rich['Console']()
self.agents = [] # List of agents in order
self.agent_tools = defaultdict(list) # agent -> [tools]
self.tracking = False
@@ -98,6 +114,13 @@ class FlowDisplay:
def _display_agent_with_tools(self, agent: str):
"""Display agent with tools on the sides."""
rich = _get_rich_components()
Table = rich['Table']
Panel = rich['Panel']
Text = rich['Text']
Align = rich['Align']
box = rich['box']
tools = self.agent_tools.get(agent, [])
if not tools:
@@ -149,8 +172,13 @@ class FlowDisplay:
self.console.print(Align.center(Text("".join(arrow_parts))))
def _create_tools_panel(self, tools: List[str]) -> Panel:
def _create_tools_panel(self, tools: List[str]):
"""Create a panel for tools."""
rich = _get_rich_components()
Panel = rich['Panel']
Text = rich['Text']
box = rich['box']
if not tools:
return ""
@@ -173,6 +201,12 @@ class FlowDisplay:
def _display_centered_node(self, label: str, color: str):
"""Display a centered node."""
rich = _get_rich_components()
Panel = rich['Panel']
Text = rich['Text']
Align = rich['Align']
box = rich['box']
panel = Panel(
Text(label, style=f"white on {color}", justify="center"),
style=f"white on {color}",
@@ -183,6 +217,9 @@ class FlowDisplay:
def _display_arrow_down(self):
"""Display a downward arrow."""
rich = _get_rich_components()
Align = rich['Align']
self.console.print()
self.console.print(Align.center("↓"))
self.console.print()
+96 -21
View File
@@ -68,8 +68,8 @@ class LLM:
MODEL_WINDOWS = {
# OpenAI
"gpt-4": 6144, # 8,192 actual
"gpt-5-nano": 96000, # 128,000 actual
"gpt-5-nano": 96000, # 128,000 actual
"gpt-4o-mini": 96000, # 128,000 actual
"gpt-4o-mini": 96000, # 128,000 actual
"gpt-4-turbo": 96000, # 128,000 actual
"o1-preview": 96000, # 128,000 actual
"o1-mini": 96000, # 128,000 actual
@@ -379,7 +379,7 @@ Respond with ONLY a valid JSON tool call in this format:
base_url=self.base_url,
verbose=self.verbose,
markdown=self.markdown,
self_reflect=self.self_reflect,
reflection=self.self_reflect,
max_reflect=self.max_reflect,
min_reflect=self.min_reflect,
reasoning_steps=self.reasoning_steps,
@@ -1430,6 +1430,8 @@ Now provide your final answer using this result."""
task_id: Optional[str] = None,
execute_tool_fn: Optional[Callable] = None,
stream: bool = True,
stream_callback: Optional[Callable] = None,
emit_events: bool = False,
**kwargs
) -> str:
"""Enhanced get_response with all OpenAI-like features"""
@@ -1456,7 +1458,7 @@ Now provide your final answer using this result."""
base_url=self.base_url,
verbose=self.verbose,
markdown=self.markdown,
self_reflect=self.self_reflect,
reflection=self.self_reflect,
max_reflect=self.max_reflect,
min_reflect=self.min_reflect,
reasoning_steps=self.reasoning_steps
@@ -1474,7 +1476,7 @@ Now provide your final answer using this result."""
output_pydantic=output_pydantic,
verbose=verbose,
markdown=markdown,
self_reflect=self_reflect,
reflection=self_reflect,
max_reflect=max_reflect,
min_reflect=min_reflect,
agent_name=agent_name,
@@ -1489,6 +1491,19 @@ Now provide your final answer using this result."""
# Disable litellm debug messages
litellm.set_verbose = False
# Setup StreamEvent emission if enabled
_emit = emit_events and stream_callback is not None
if _emit:
from ..streaming.events import StreamEvent, StreamEventType
_first_token_emitted = False
_last_content_time = None
_request_start_perf = time.perf_counter()
stream_callback(StreamEvent(
type=StreamEventType.REQUEST_START,
timestamp=_request_start_perf,
metadata={"model": self.model, "provider": "litellm"}
))
# Format tools if provided
formatted_tools = self._format_tools_for_litellm(tools)
@@ -1525,6 +1540,7 @@ Now provide your final answer using this result."""
max_iterations = 10 # Prevent infinite loops
iteration_count = 0
final_response_text = ""
response_text = "" # Initialize to prevent UnboundLocalError on API errors
stored_reasoning_content = None # Store reasoning content from tool execution
accumulated_tool_results = [] # Store all tool results across iterations
@@ -1532,6 +1548,10 @@ Now provide your final answer using this result."""
try:
# Get response from LiteLLM
current_time = time.time()
# Trigger LLM callback for subsequent iterations (after tool calls)
# Initial callback is in agent._chat_completion, so this triggers for all loop iterations
execute_sync_callback('llm_start', model=self.model, agent_name=agent_name)
# If reasoning_steps is True, do a single non-streaming call
if reasoning_steps:
@@ -1554,6 +1574,39 @@ Now provide your final answer using this result."""
if self.metrics:
self._track_token_usage(final_response, self.model)
# Trigger llm_end callback with metrics for debug output
llm_latency_ms = (time.time() - current_time) * 1000
# Extract usage - handle both dict and ModelResponse object
tokens_in = 0
tokens_out = 0
if isinstance(final_response, dict):
usage = final_response.get("usage", {})
tokens_in = usage.get("prompt_tokens", 0)
tokens_out = usage.get("completion_tokens", 0)
else:
# ModelResponse object
usage = getattr(final_response, 'usage', None)
if usage:
tokens_in = getattr(usage, 'prompt_tokens', 0) or 0
tokens_out = getattr(usage, 'completion_tokens', 0) or 0
# Calculate cost if available
llm_cost = None
try:
llm_cost = litellm.completion_cost(completion_response=final_response)
except Exception:
pass
execute_sync_callback(
'llm_end',
model=self.model,
tokens_in=tokens_in,
tokens_out=tokens_out,
cost=llm_cost,
latency_ms=llm_latency_ms
)
# Execute callbacks and display based on verbose setting
generation_time_val = time.time() - current_time
response_content = f"Reasoning:\n{reasoning_content}\n\nAnswer:\n{response_text}" if reasoning_content else response_text
@@ -2139,6 +2192,16 @@ Now provide your final answer using this result."""
logging.debug(f"[TOOL_EXEC_DEBUG] About to display tool call with message: {display_message}")
display_tool_call(display_message, console=self.console)
# Always trigger callback for status output (even when verbose=False)
result_str = json.dumps(tool_result) if tool_result else "empty"
execute_sync_callback(
'tool_call',
message=f"Calling function: {function_name}",
tool_name=function_name,
tool_input=arguments,
tool_output=result_str[:200] if result_str else None
)
# Check if this is Ollama provider
if self._is_ollama_provider():
@@ -2917,7 +2980,7 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
base_url=self.base_url,
verbose=self.verbose,
markdown=self.markdown,
self_reflect=self.self_reflect,
reflection=self.self_reflect,
max_reflect=self.max_reflect,
min_reflect=self.min_reflect,
reasoning_steps=self.reasoning_steps
@@ -2935,7 +2998,7 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
output_pydantic=output_pydantic,
verbose=verbose,
markdown=markdown,
self_reflect=self_reflect,
reflection=self_reflect,
max_reflect=max_reflect,
min_reflect=min_reflect,
agent_name=agent_name,
@@ -3621,12 +3684,14 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
self.session_token_metrics = self.session_token_metrics + metrics
# Track in global collector
# Extract provider from model string (e.g., 'openai' from 'openai/gpt-4o-mini')
provider = model.split('/')[0] if '/' in model else 'litellm'
_token_collector.track_tokens(
model=model,
agent=self.current_agent_name,
metrics=metrics,
metadata={
"provider": self.provider,
"provider": provider,
"stream": False
}
)
@@ -3691,9 +3756,19 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
output_json = override_params.get('output_json')
output_pydantic = override_params.get('output_pydantic')
# Always remove these from params as they're not native litellm parameters
params.pop('output_json', None)
params.pop('output_pydantic', None)
# Always remove internal PraisonAI parameters that are not native litellm parameters
# These are used internally by PraisonAI but should not be passed to the LLM API
internal_params = [
'output_json', 'output_pydantic', # Structured output handling
'reflection', 'self_reflect', 'max_reflect', 'min_reflect', 'reflect_llm', # Self-reflection
'verbose', 'markdown', # Display options
'agent_name', 'agent_role', 'agent_tools', # Agent metadata
'task_name', 'task_description', 'task_id', # Task metadata
'execute_tool_fn', 'stream_callback', 'emit_events', # Callbacks
'console', # Rich console
]
for param in internal_params:
params.pop(param, None)
if output_json or output_pydantic:
@@ -3712,18 +3787,18 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
logging.debug(f"Using Gemini native structured output with schema: {json.dumps(schema, indent=2)}")
# Add tool_choice="auto" when tools are provided (unless already specified)
# This encourages the model to use tools when appropriate
if 'tools' in params and params['tools'] and 'tool_choice' not in params:
# For Gemini models, use tool_choice to encourage tool usage
if self._is_gemini_model():
try:
import litellm
# Check if model supports function calling before setting tool_choice
if litellm.supports_function_calling(model=self.model):
params['tool_choice'] = 'auto'
except Exception as e:
# If check fails, still set tool_choice for known Gemini models
logging.debug(f"Could not verify function calling support: {e}. Setting tool_choice anyway.")
try:
import litellm
# Check if model supports function calling before setting tool_choice
if litellm.supports_function_calling(model=self.model):
params['tool_choice'] = 'auto'
logging.debug(f"Set tool_choice='auto' for model {self.model}")
except Exception as e:
# If check fails, still set tool_choice to encourage tool usage
logging.debug(f"Could not verify function calling support: {e}. Setting tool_choice='auto' anyway.")
params['tool_choice'] = 'auto'
# Add web_search_options if web_search is enabled and model supports it
if self.web_search and self._supports_web_search():
@@ -51,7 +51,7 @@ class ModelRouter:
DEFAULT_MODELS = [
# Lightweight/cheap models for simple tasks
ModelProfile(
name="gpt-5-nano",
name="gpt-4o-mini",
provider="openai",
complexity_range=(TaskComplexity.SIMPLE, TaskComplexity.MODERATE),
cost_per_1k_tokens=0.00075, # Average of $0.00015 input, $0.0006 output
@@ -80,7 +80,7 @@ class ModelRouter:
# Mid-tier models for moderate complexity
ModelProfile(
name="gpt-5-nano",
name="gpt-4o-mini",
provider="openai",
complexity_range=(TaskComplexity.MODERATE, TaskComplexity.COMPLEX),
cost_per_1k_tokens=0.0075, # Average of $0.0025 input, $0.01 output
@@ -145,7 +145,7 @@ class ModelRouter:
preferred_providers: List of preferred providers in order
"""
self.models = models or self.DEFAULT_MODELS
self.default_model = default_model or os.getenv('OPENAI_MODEL_NAME', 'gpt-5-nano')
self.default_model = default_model or os.getenv('OPENAI_MODEL_NAME', 'gpt-4o-mini')
self.cost_threshold = cost_threshold
self.preferred_providers = preferred_providers or []
@@ -10,15 +10,52 @@ import logging
import time
import json
import asyncio
from typing import Any, Dict, List, Optional, Union, AsyncIterator, Iterator, Callable, Tuple
from openai import OpenAI, AsyncOpenAI
from openai.types.chat import ChatCompletionChunk
from typing import Any, Dict, List, Optional, Union, AsyncIterator, Iterator, Callable, Tuple, TYPE_CHECKING
from pydantic import BaseModel
from dataclasses import dataclass
from rich.console import Console
from rich.live import Live
import inspect
# Lazy imports for optional dependencies
_openai_module = None
_rich_console = None
_rich_live = None
def _get_openai():
"""Lazy import openai module."""
global _openai_module
if _openai_module is None:
try:
import openai as _openai
_openai_module = _openai
except ImportError:
raise ImportError(
"openai is required for OpenAI client. "
"Install with: pip install openai"
)
return _openai_module
def _get_openai_classes():
"""Get OpenAI and AsyncOpenAI classes lazily."""
openai = _get_openai()
return openai.OpenAI, openai.AsyncOpenAI
def _get_rich_console():
"""Lazy import rich Console."""
global _rich_console
if _rich_console is None:
from rich.console import Console
_rich_console = Console
return _rich_console
def _get_rich_live():
"""Lazy import rich Live."""
global _rich_live
if _rich_live is None:
from rich.live import Live
_rich_live = Live
return _rich_live
# Import display_tool_call for callback support (lazy import to avoid circular imports)
_display_tool_call = None
def _get_display_tool_call():
@@ -258,21 +295,23 @@ class OpenAIClient:
def console(self):
"""Lazily initialize Rich Console only when needed."""
if self._console is None:
from rich.console import Console
Console = _get_rich_console()
self._console = Console()
return self._console
@property
def sync_client(self) -> OpenAI:
def sync_client(self):
"""Get the synchronous OpenAI client (lazy initialization)."""
if self._sync_client is None:
OpenAI, _ = _get_openai_classes()
self._sync_client = OpenAI(api_key=self.api_key, base_url=self.base_url)
return self._sync_client
@property
def async_client(self) -> AsyncOpenAI:
def async_client(self):
"""Get the asynchronous OpenAI client (lazy initialization)."""
if self._async_client is None:
_, AsyncOpenAI = _get_openai_classes()
self._async_client = AsyncOpenAI(api_key=self.api_key, base_url=self.base_url)
return self._async_client
@@ -579,9 +618,11 @@ class OpenAIClient:
temperature: float = 1.0,
tools: Optional[List[Dict]] = None,
start_time: Optional[float] = None,
console: Optional[Console] = None,
console: Optional[Any] = None,
display_fn: Optional[Callable] = None,
reasoning_steps: bool = False,
stream_callback: Optional[Callable] = None,
emit_events: bool = False,
**kwargs
) -> Optional[ChatCompletion]:
"""
@@ -596,11 +637,18 @@ class OpenAIClient:
console: Console for output
display_fn: Display function for live updates
reasoning_steps: Whether to show reasoning steps
stream_callback: Optional callback for StreamEvent emission
emit_events: Whether to emit StreamEvents (requires stream_callback)
**kwargs: Additional parameters for the API
Returns:
ChatCompletion object or None if error
"""
# Lazy import StreamEvent types only when needed
_emit = emit_events and stream_callback is not None
if _emit:
from ..streaming.events import StreamEvent, StreamEventType
try:
# Default start time and console if not provided
if start_time is None:
@@ -608,6 +656,15 @@ class OpenAIClient:
if console is None:
console = self.console
# Emit REQUEST_START event
request_start_perf = time.perf_counter()
if _emit:
stream_callback(StreamEvent(
type=StreamEventType.REQUEST_START,
timestamp=request_start_perf,
metadata={"model": model, "message_count": len(messages)}
))
# Create the response stream
response_stream = self.sync_client.chat.completions.create(
model=model,
@@ -615,29 +672,82 @@ class OpenAIClient:
temperature=temperature,
tools=tools if tools else None,
stream=True,
stream_options={"include_usage": True} if _emit else None,
**kwargs
)
# Emit HEADERS_RECEIVED event (stream object created means headers received)
if _emit:
stream_callback(StreamEvent(
type=StreamEventType.HEADERS_RECEIVED,
timestamp=time.perf_counter()
))
full_response_text = ""
reasoning_content = ""
chunks = []
first_token_emitted = False
last_content_time = None
ttft_logged = False
# If display function provided, use Live display
if display_fn:
Live = _get_rich_live()
with Live(
display_fn("", start_time),
console=console,
refresh_per_second=4,
refresh_per_second=10, # Increased for more responsive streaming
transient=True,
vertical_overflow="ellipsis",
auto_refresh=True
) as live:
for chunk in response_stream:
chunks.append(chunk)
if chunk.choices[0].delta.content:
full_response_text += chunk.choices[0].delta.content
# Check for content delta
if chunk.choices and chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
full_response_text += content
last_content_time = time.perf_counter()
# Log TTFT on first token
if not ttft_logged:
ttft_ms = (last_content_time - request_start_perf) * 1000
logging.debug(f"TTFT (Time To First Token): {ttft_ms:.0f}ms")
ttft_logged = True
# Emit FIRST_TOKEN on first content
if _emit and not first_token_emitted:
stream_callback(StreamEvent(
type=StreamEventType.FIRST_TOKEN,
timestamp=last_content_time,
content=content
))
first_token_emitted = True
elif _emit:
# Emit DELTA_TEXT for subsequent tokens
stream_callback(StreamEvent(
type=StreamEventType.DELTA_TEXT,
timestamp=last_content_time,
content=content
))
live.update(display_fn(full_response_text, start_time))
# Handle tool calls streaming
if _emit and chunk.choices and hasattr(chunk.choices[0].delta, 'tool_calls') and chunk.choices[0].delta.tool_calls:
for tc in chunk.choices[0].delta.tool_calls:
stream_callback(StreamEvent(
type=StreamEventType.DELTA_TOOL_CALL,
timestamp=time.perf_counter(),
tool_call={
"index": tc.index,
"id": getattr(tc, 'id', None),
"name": getattr(tc.function, 'name', None) if tc.function else None,
"arguments": getattr(tc.function, 'arguments', None) if tc.function else None
}
))
# Update live display with reasoning content if enabled
if reasoning_steps and hasattr(chunk.choices[0].delta, "reasoning_content"):
rc = chunk.choices[0].delta.reasoning_content
@@ -648,9 +758,61 @@ class OpenAIClient:
# Clear the last generating display with a blank line
console.print()
else:
# Just collect chunks without display
# Just collect chunks without display, but still emit events
for chunk in response_stream:
chunks.append(chunk)
if chunk.choices and chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
last_content_time = time.perf_counter()
# Log TTFT on first token
if not ttft_logged:
ttft_ms = (last_content_time - request_start_perf) * 1000
logging.debug(f"TTFT (Time To First Token): {ttft_ms:.0f}ms")
ttft_logged = True
if _emit and not first_token_emitted:
stream_callback(StreamEvent(
type=StreamEventType.FIRST_TOKEN,
timestamp=last_content_time,
content=content
))
first_token_emitted = True
elif _emit:
# Emit DELTA_TEXT for subsequent tokens
stream_callback(StreamEvent(
type=StreamEventType.DELTA_TEXT,
timestamp=last_content_time,
content=content
))
# Handle tool calls streaming
if _emit and chunk.choices and hasattr(chunk.choices[0].delta, 'tool_calls') and chunk.choices[0].delta.tool_calls:
for tc in chunk.choices[0].delta.tool_calls:
stream_callback(StreamEvent(
type=StreamEventType.DELTA_TOOL_CALL,
timestamp=time.perf_counter(),
tool_call={
"index": tc.index,
"id": getattr(tc, 'id', None),
"name": getattr(tc.function, 'name', None) if tc.function else None,
"arguments": getattr(tc.function, 'arguments', None) if tc.function else None
}
))
# Emit LAST_TOKEN and STREAM_END events
if _emit:
if last_content_time:
stream_callback(StreamEvent(
type=StreamEventType.LAST_TOKEN,
timestamp=last_content_time
))
stream_callback(StreamEvent(
type=StreamEventType.STREAM_END,
timestamp=time.perf_counter(),
metadata={"chunk_count": len(chunks)}
))
final_response = process_stream_chunks(chunks)
return final_response
@@ -666,9 +828,11 @@ class OpenAIClient:
temperature: float = 1.0,
tools: Optional[List[Dict]] = None,
start_time: Optional[float] = None,
console: Optional[Console] = None,
console: Optional[Any] = None,
display_fn: Optional[Callable] = None,
reasoning_steps: bool = False,
stream_callback: Optional[Callable] = None,
emit_events: bool = False,
**kwargs
) -> Optional[ChatCompletion]:
"""
@@ -683,11 +847,18 @@ class OpenAIClient:
console: Console for output
display_fn: Display function for live updates
reasoning_steps: Whether to show reasoning steps
stream_callback: Optional callback for StreamEvent emission (can be sync or async)
emit_events: Whether to emit StreamEvents (requires stream_callback)
**kwargs: Additional parameters for the API
Returns:
ChatCompletion object or None if error
"""
# Lazy import StreamEvent types only when needed
_emit = emit_events and stream_callback is not None
if _emit:
from ..streaming.events import StreamEvent, StreamEventType
try:
# Default start time and console if not provided
if start_time is None:
@@ -695,6 +866,19 @@ class OpenAIClient:
if console is None:
console = self.console
# Emit REQUEST_START event
request_start_perf = time.perf_counter()
if _emit:
event = StreamEvent(
type=StreamEventType.REQUEST_START,
timestamp=request_start_perf,
metadata={"model": model, "message_count": len(messages)}
)
if asyncio.iscoroutinefunction(stream_callback):
await stream_callback(event)
else:
stream_callback(event)
# Create the response stream
response_stream = await self.async_client.chat.completions.create(
model=model,
@@ -702,29 +886,82 @@ class OpenAIClient:
temperature=temperature,
tools=tools if tools else None,
stream=True,
stream_options={"include_usage": True} if _emit else None,
**kwargs
)
# Emit HEADERS_RECEIVED event
if _emit:
event = StreamEvent(
type=StreamEventType.HEADERS_RECEIVED,
timestamp=time.perf_counter()
)
if asyncio.iscoroutinefunction(stream_callback):
await stream_callback(event)
else:
stream_callback(event)
full_response_text = ""
reasoning_content = ""
chunks = []
first_token_emitted = False
last_content_time = None
async def emit_event(event: 'StreamEvent'):
if asyncio.iscoroutinefunction(stream_callback):
await stream_callback(event)
else:
stream_callback(event)
# If display function provided, use Live display
if display_fn:
Live = _get_rich_live()
with Live(
display_fn("", start_time),
console=console,
refresh_per_second=4,
refresh_per_second=10,
transient=True,
vertical_overflow="ellipsis",
auto_refresh=True
) as live:
async for chunk in response_stream:
chunks.append(chunk)
if chunk.choices[0].delta.content:
full_response_text += chunk.choices[0].delta.content
if chunk.choices and chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
full_response_text += content
last_content_time = time.perf_counter()
if _emit and not first_token_emitted:
await emit_event(StreamEvent(
type=StreamEventType.FIRST_TOKEN,
timestamp=last_content_time,
content=content
))
first_token_emitted = True
elif _emit:
await emit_event(StreamEvent(
type=StreamEventType.DELTA_TEXT,
timestamp=last_content_time,
content=content
))
live.update(display_fn(full_response_text, start_time))
# Handle tool calls streaming
if _emit and chunk.choices and hasattr(chunk.choices[0].delta, 'tool_calls') and chunk.choices[0].delta.tool_calls:
for tc in chunk.choices[0].delta.tool_calls:
await emit_event(StreamEvent(
type=StreamEventType.DELTA_TOOL_CALL,
timestamp=time.perf_counter(),
tool_call={
"index": tc.index,
"id": getattr(tc, 'id', None),
"name": getattr(tc.function, 'name', None) if tc.function else None,
"arguments": getattr(tc.function, 'arguments', None) if tc.function else None
}
))
# Update live display with reasoning content if enabled
if reasoning_steps and hasattr(chunk.choices[0].delta, "reasoning_content"):
rc = chunk.choices[0].delta.reasoning_content
@@ -738,6 +975,50 @@ class OpenAIClient:
# Just collect chunks without display
async for chunk in response_stream:
chunks.append(chunk)
if _emit and chunk.choices and chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
last_content_time = time.perf_counter()
if not first_token_emitted:
await emit_event(StreamEvent(
type=StreamEventType.FIRST_TOKEN,
timestamp=last_content_time,
content=content
))
first_token_emitted = True
else:
await emit_event(StreamEvent(
type=StreamEventType.DELTA_TEXT,
timestamp=last_content_time,
content=content
))
if _emit and chunk.choices and hasattr(chunk.choices[0].delta, 'tool_calls') and chunk.choices[0].delta.tool_calls:
for tc in chunk.choices[0].delta.tool_calls:
await emit_event(StreamEvent(
type=StreamEventType.DELTA_TOOL_CALL,
timestamp=time.perf_counter(),
tool_call={
"index": tc.index,
"id": getattr(tc, 'id', None),
"name": getattr(tc.function, 'name', None) if tc.function else None,
"arguments": getattr(tc.function, 'arguments', None) if tc.function else None
}
))
# Emit LAST_TOKEN and STREAM_END events
if _emit:
if last_content_time:
await emit_event(StreamEvent(
type=StreamEventType.LAST_TOKEN,
timestamp=last_content_time
))
await emit_event(StreamEvent(
type=StreamEventType.STREAM_END,
timestamp=time.perf_counter(),
metadata={"chunk_count": len(chunks)}
))
final_response = process_stream_chunks(chunks)
return final_response
@@ -749,13 +1030,13 @@ class OpenAIClient:
def create_completion(
self,
messages: List[Dict[str, Any]],
model: str = "gpt-5-nano",
model: str = "gpt-4o-mini",
temperature: float = 1.0,
stream: bool = False,
tools: Optional[List[Dict[str, Any]]] = None,
tool_choice: Optional[Union[str, Dict[str, Any]]] = None,
**kwargs
) -> Union[Any, Iterator[ChatCompletionChunk]]:
) -> Union[Any, Iterator[Any]]:
"""
Create a chat completion using the synchronous client.
@@ -794,13 +1075,13 @@ class OpenAIClient:
async def acreate_completion(
self,
messages: List[Dict[str, Any]],
model: str = "gpt-5-nano",
model: str = "gpt-4o-mini",
temperature: float = 1.0,
stream: bool = False,
tools: Optional[List[Dict[str, Any]]] = None,
tool_choice: Optional[Union[str, Dict[str, Any]]] = None,
**kwargs
) -> Union[Any, AsyncIterator[ChatCompletionChunk]]:
) -> Union[Any, AsyncIterator[Any]]:
"""
Create a chat completion using the asynchronous client.
@@ -839,12 +1120,12 @@ class OpenAIClient:
def chat_completion_with_tools(
self,
messages: List[Dict[str, Any]],
model: str = "gpt-5-nano",
model: str = "gpt-4o-mini",
temperature: float = 1.0,
tools: Optional[List[Any]] = None,
execute_tool_fn: Optional[Callable] = None,
stream: bool = True,
console: Optional[Console] = None,
console: Optional[Any] = None,
display_fn: Optional[Callable] = None,
reasoning_steps: bool = False,
verbose: bool = True,
@@ -886,6 +1167,10 @@ class OpenAIClient:
iteration_count = 0
while iteration_count < max_iterations:
# Trigger LLM callback for status/trace output
from ..main import execute_sync_callback
execute_sync_callback('llm_start', model=model, agent_name=None)
if stream:
# Process as streaming response with formatted tools
final_response = self.process_stream_response(
@@ -904,7 +1189,10 @@ class OpenAIClient:
if display_fn and console:
# When verbose (display_fn provided), use streaming for better UX
try:
with Live(display_fn("", start_time), console=console, refresh_per_second=4, transient=True) as live:
request_start_perf = time.perf_counter()
ttft_logged = False
Live = _get_rich_live()
with Live(display_fn("", start_time), console=console, refresh_per_second=10, transient=True) as live:
# Use streaming when display_fn is provided for progressive display
response_stream = self.create_completion(
messages=messages,
@@ -922,6 +1210,11 @@ class OpenAIClient:
for chunk in response_stream:
chunks.append(chunk)
if chunk.choices[0].delta.content:
# Log TTFT on first token
if not ttft_logged:
ttft_ms = (time.perf_counter() - request_start_perf) * 1000
logging.debug(f"TTFT (Time To First Token): {ttft_ms:.0f}ms")
ttft_logged = True
full_response_text += chunk.choices[0].delta.content
live.update(display_fn(full_response_text, start_time))
@@ -954,6 +1247,35 @@ class OpenAIClient:
if not final_response:
return None
# Trigger llm_end callback with metrics for debug output
llm_end_time = time.perf_counter()
llm_latency_ms = (llm_end_time - start_time) * 1000
# Extract usage info if available
usage = getattr(final_response, 'usage', None)
tokens_in = getattr(usage, 'prompt_tokens', 0) if usage else 0
tokens_out = getattr(usage, 'completion_tokens', 0) if usage else 0
# Calculate cost if litellm available
cost = None
try:
import litellm
if hasattr(final_response, 'model_dump'):
cost = litellm.completion_cost(completion_response=final_response.model_dump())
elif isinstance(final_response, dict):
cost = litellm.completion_cost(completion_response=final_response)
except Exception:
pass # Cost calculation is optional
execute_sync_callback(
'llm_end',
model=model,
tokens_in=tokens_in,
tokens_out=tokens_out,
cost=cost,
latency_ms=llm_latency_ms
)
# Check for tool calls
tool_calls = getattr(final_response.choices[0].message, 'tool_calls', None)
@@ -989,17 +1311,19 @@ class OpenAIClient:
# Always trigger callback for tool call tracking (even when verbose=False)
display_tool_call_fn = _get_display_tool_call()
display_tool_call_fn(f"Calling function: {function_name}", console=console if verbose else None)
if verbose and console:
console.print(f"[dim]Arguments:[/dim] {arguments}")
# Execute the tool
tool_result = execute_tool_fn(function_name, arguments)
results_str = json.dumps(tool_result) if tool_result else "Function returned an empty output"
# Trigger callback with result
display_tool_call_fn(f"Function {function_name} returned: {results_str[:200]}{'...' if len(results_str) > 200 else ''}", console=console if verbose else None)
# Trigger callback with structured parameters for status output
display_tool_call_fn(
f"Calling function: {function_name}",
console=console if verbose else None,
tool_name=function_name,
tool_input=arguments,
tool_output=results_str[:200] if results_str else None
)
messages.append({
"role": "tool",
@@ -1020,12 +1344,12 @@ class OpenAIClient:
async def achat_completion_with_tools(
self,
messages: List[Dict[str, Any]],
model: str = "gpt-5-nano",
model: str = "gpt-4o-mini",
temperature: float = 1.0,
tools: Optional[List[Any]] = None,
execute_tool_fn: Optional[Callable] = None,
stream: bool = True,
console: Optional[Console] = None,
console: Optional[Any] = None,
display_fn: Optional[Callable] = None,
reasoning_steps: bool = False,
verbose: bool = True,
@@ -1079,6 +1403,7 @@ class OpenAIClient:
if display_fn and console:
# When verbose (display_fn provided), use streaming for better UX
try:
Live = _get_rich_live()
with Live(display_fn("", start_time), console=console, refresh_per_second=4, transient=True) as live:
# Use streaming when display_fn is provided for progressive display
response_stream = await self.acreate_completion(
@@ -1204,7 +1529,7 @@ class OpenAIClient:
def chat_completion_with_tools_stream(
self,
messages: List[Dict[str, Any]],
model: str = "gpt-5-nano",
model: str = "gpt-4o-mini",
temperature: float = 1.0,
tools: Optional[List[Any]] = None,
execute_tool_fn: Optional[Callable] = None,
@@ -1356,7 +1681,7 @@ class OpenAIClient:
self,
messages: List[Dict[str, Any]],
response_format: BaseModel,
model: str = "gpt-5-nano",
model: str = "gpt-4o-mini",
temperature: float = 1.0,
**kwargs
) -> Any:
@@ -1390,7 +1715,7 @@ class OpenAIClient:
self,
messages: List[Dict[str, Any]],
response_format: BaseModel,
model: str = "gpt-5-nano",
model: str = "gpt-4o-mini",
temperature: float = 1.0,
**kwargs
) -> Any:
+204 -23
View File
@@ -30,6 +30,53 @@ async_display_callbacks = {}
# Global approval callback registry
approval_callback = None
# ─────────────────────────────────────────────────────────────────────────────
# PraisonAI Unique Color Palette: "Elegant Intelligence"
# Creates a visual narrative flow: Agent → Task → Working → Response
# ─────────────────────────────────────────────────────────────────────────────
PRAISON_COLORS = {
# Agent identity - grounded, stable
"agent": "#86A789", # Soft Sage Green
"agent_text": "#D2E3C8", # Light sage for text
# Task/Question - input, attention-grabbing
"task": "#FF9B9B", # Warm Coral
"task_text": "#FFE5E5", # Light coral for text
# Working/Processing - action, energy
"working": "#FFB347", # Amber
"working_text": "#FFF3E0", # Light amber for text
# Response/Output - completion, calm
"response": "#4A90D9", # Ocean Blue
"response_text": "#E3F2FD", # Light blue for text
# Tool calls - special action
"tool": "#9B7EDE", # Violet Accent
"tool_text": "#EDE7F6", # Light violet for text
# Reasoning - thought process
"reasoning": "#78909C", # Blue Gray
"reasoning_text": "#ECEFF1", # Light gray for text
# Error/Warning - alert
"error": "#E57373", # Alert Red
"error_text": "#FFEBEE", # Light red for text
# Metrics - meta information
"metrics": "#B4B4B3", # Cool Gray
"metrics_text": "#FAFAFA", # Near white for text
}
# Status animation frames for "Working" indicator
WORKING_FRAMES = ["●○○", "○●○", "○○●", "○●○"]
WORKING_PHASES = [
"Analyzing query...",
"Processing context...",
"Generating response...",
"Finalizing output...",
]
# At the top of the file, add display_callbacks to __all__
__all__ = [
'error_logs',
@@ -39,7 +86,19 @@ __all__ = [
'async_display_callbacks',
'execute_callback',
'approval_callback',
# ... other exports
# Color palette and animation constants
'PRAISON_COLORS',
'WORKING_FRAMES',
'WORKING_PHASES',
# Display functions
'display_interaction',
'display_instruction',
'display_tool_call',
'display_error',
'display_generating',
'display_reasoning_steps',
'display_working_status',
'display_self_reflection',
]
def register_display_callback(display_type: str, callback_fn, is_async: bool = False):
@@ -149,10 +208,14 @@ def _clean_display_content(content: str, max_length: int = 20000) -> str:
return content.strip()
def display_interaction(message, response, markdown=True, generation_time=None, console=None, agent_name=None, agent_role=None, agent_tools=None, task_name=None, task_description=None, task_id=None):
def display_interaction(message, response, markdown=True, generation_time=None, console=None, agent_name=None, agent_role=None, agent_tools=None, task_name=None, task_description=None, task_id=None, metrics=None):
"""Synchronous version of display_interaction.
Displays the task/message and response in clean panels with response time in title.
Displays the task/message and response in clean panels with semantic colors
and optional metrics footer. Uses PraisonAI's unique color palette.
Args:
metrics: Optional dict with token_in, token_out, cost, model for footer display
"""
if console is None:
console = Console()
@@ -180,20 +243,40 @@ def display_interaction(message, response, markdown=True, generation_time=None,
agent_tools=agent_tools,
task_name=task_name,
task_description=task_description,
task_id=task_id
task_id=task_id,
metrics=metrics
)
# Build response title with time (Agno-style: "Response (1.2s)")
# Build response title with time
response_title = "Response"
if generation_time:
response_title = f"Response ({generation_time:.1f}s)"
# Build response content with optional metrics footer
response_content = response
if metrics and isinstance(metrics, dict):
# Add dashed separator and compact metrics line
tokens_in = metrics.get('tokens_in', 0)
tokens_out = metrics.get('tokens_out', 0)
cost = metrics.get('cost', 0)
model = metrics.get('model', '')
metrics_line = f"\n\n─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─\n📊 {tokens_in} tokens in · {tokens_out} out"
if cost > 0:
metrics_line += f" · ${cost:.4f}"
if model:
metrics_line += f" · {model}"
response_content = response + metrics_line
# Task is inline (less visual weight), Response keeps panel (it's the main content)
# Format: 📝 Task message
task_prefix = "[bold #FF9B9B]📝[/]"
if markdown:
console.print(Panel.fit(Markdown(message), title="Task", border_style="cyan"))
console.print(Panel.fit(Markdown(response), title=response_title, border_style="cyan"))
console.print(f"{task_prefix} {message}\n")
console.print(Panel.fit(Markdown(response_content), title=response_title, border_style=PRAISON_COLORS["response"]))
else:
console.print(Panel.fit(Text(message, style="bold green"), title="Task", border_style="cyan"))
console.print(Panel.fit(Text(response, style="bold blue"), title=response_title, border_style="cyan"))
console.print(f"{task_prefix} [bold {PRAISON_COLORS['task_text']}]{message}[/]\n")
console.print(Panel.fit(Text(response_content, style=f"bold {PRAISON_COLORS['response_text']}"), title=response_title, border_style=PRAISON_COLORS["response"]))
def display_self_reflection(message: str, console=None):
if not message or not message.strip():
@@ -231,8 +314,11 @@ def display_instruction(message: str, console=None, agent_name: str = None, agen
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
console.print(Panel.fit(Text(message, style="bold blue"), title="Instruction", border_style="cyan"))
def display_tool_call(message: str, console=None, tool_name: str = None, tool_input: dict = None, tool_output: str = None):
"""Display tool call information in a consolidated panel.
def display_tool_call(message: str, console=None, tool_name: str = None, tool_input: dict = None, tool_output: str = None, elapsed_time: float = None, success: bool = True):
"""Display tool call information in PraisonAI's unique timeline format.
Uses ▸ prefix, inline timing [X.Xs], and status icons ✓/✗ for a clean,
scannable tool activity display.
Args:
message: The tool call message (legacy format)
@@ -240,6 +326,8 @@ def display_tool_call(message: str, console=None, tool_name: str = None, tool_in
tool_name: Name of the tool being called
tool_input: Input arguments to the tool
tool_output: Output from the tool (if available)
elapsed_time: Time taken for tool execution in seconds
success: Whether the tool call succeeded
"""
logging.debug(f"display_tool_call called with message: {repr(message)}")
if not message or not message.strip():
@@ -251,19 +339,41 @@ def display_tool_call(message: str, console=None, tool_name: str = None, tool_in
# Execute synchronous callbacks (always, even when console is None)
execute_sync_callback('tool_call', message=message, tool_name=tool_name, tool_input=tool_input, tool_output=tool_output)
# Only print panel if console is provided (verbose mode)
# Only print if console is provided (verbose mode)
if console is not None:
# Build consolidated tool call display (Agno-style)
if tool_name and tool_input is not None:
# Format: tool_name(arg1=val1, arg2=val2)
args_str = ", ".join(f"{k}={v}" for k, v in tool_input.items()) if tool_input else ""
tool_display = f"• {tool_name}({args_str})"
# Build clean inline format - no panels, just prefixed lines
if tool_name:
# Format: ▸ tool_name(args) → result [X.Xs] ✓
args_str = ""
if tool_input:
# Truncate long values for display
args_parts = []
for k, v in tool_input.items():
v_str = str(v)
if len(v_str) > 50:
v_str = v_str[:47] + "..."
args_parts.append(f"{k}={repr(v_str) if isinstance(v, str) else v_str}")
args_str = ", ".join(args_parts)
# Build the inline entry
status_icon = "[green]✓[/]" if success else "[red]✗[/]"
time_str = f"[dim][{elapsed_time:.1f}s][/]" if elapsed_time else ""
# Base tool call line
tool_line = f"[bold #86A789]▸[/] [#B4D4FF]{tool_name}[/]({args_str})"
# Add output inline with arrow if available
if tool_output:
tool_display += f"\n → {tool_output}"
console.print(Panel.fit(Text(tool_display, style="bold cyan"), title="Tool Call", border_style="green"))
output_str = str(tool_output)
if len(output_str) > 80:
output_str = output_str[:77] + "..."
tool_line += f" [dim]→[/] [italic]{output_str}[/]"
tool_line += f" {time_str} {status_icon}"
console.print(tool_line)
else:
# Legacy format
console.print(Panel.fit(Text(message, style="bold cyan"), title="Tool Call", border_style="green"))
# Legacy format - simple inline
console.print(f"[bold #86A789]▸[/] {message}")
def display_error(message: str, console=None):
if not message or not message.strip():
@@ -275,7 +385,12 @@ def display_error(message: str, console=None):
# Execute synchronous callbacks
execute_sync_callback('error', message=message)
console.print(Panel.fit(Text(message, style="bold red"), title="Error", border_style="red"))
# Use semantic error color
console.print(Panel.fit(
Text(message, style=f"bold {PRAISON_COLORS['error_text']}"),
title="⚠ Error",
border_style=PRAISON_COLORS["error"]
))
error_logs.append(message)
def display_generating(content: str = "", start_time: Optional[float] = None):
@@ -293,7 +408,73 @@ def display_generating(content: str = "", start_time: Optional[float] = None):
# Execute synchronous callbacks
execute_sync_callback('generating', content=content, elapsed_time=elapsed_str.strip() if elapsed_str else None)
return Panel(Markdown(content), title=f"Generating...{elapsed_str}", border_style="green")
# Use semantic response color
return Panel(Markdown(content), title=f"Generating...{elapsed_str}", border_style=PRAISON_COLORS["response"])
def display_reasoning_steps(steps: List[str], console=None):
"""Display reasoning steps with unique numbered circles.
Uses ①②③ numbered circles for a distinctive, scannable format
that shows the agent's thought process.
Args:
steps: List of reasoning step descriptions
console: Rich console for output
"""
if not steps:
return
if console is None:
console = Console()
# Circle number mapping (supports up to 20 steps)
circle_numbers = "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳"
# Build reasoning display with numbered circles
reasoning_lines = []
for i, step in enumerate(steps):
circle = circle_numbers[i] if i < len(circle_numbers) else f"({i+1})"
step_text = _clean_display_content(str(step))
if len(step_text) > 80:
step_text = step_text[:77] + "..."
reasoning_lines.append(f"{circle} {step_text}")
reasoning_display = "\n".join(reasoning_lines)
console.print(Panel.fit(
Text(reasoning_display, style=f"italic {PRAISON_COLORS['reasoning_text']}"),
title="Reasoning",
border_style=PRAISON_COLORS["reasoning"]
))
def display_working_status(phase: int = 0, status_text: str = None, console=None):
"""Display animated working status with pulsing dots.
Shows a unique "Working ●○○" indicator with phase-specific status.
This is PraisonAI's distinctive approach to showing processing status.
Args:
phase: Current animation phase (0-3)
status_text: Optional status description
console: Rich console for output
Returns:
Panel object for use with Rich.Live
"""
if console is None:
console = Console()
# Get current frame and phase text
frame = WORKING_FRAMES[phase % len(WORKING_FRAMES)]
phase_text = status_text or WORKING_PHASES[phase % len(WORKING_PHASES)]
# Build working status display
working_display = f"Working {frame} {phase_text}"
return Panel.fit(
Text(working_display, style=f"bold {PRAISON_COLORS['working_text']}"),
title="Status",
border_style=PRAISON_COLORS["working"]
)
# Async versions with 'a' prefix
async def adisplay_interaction(message, response, markdown=True, generation_time=None, console=None, agent_name=None, agent_role=None, agent_tools=None, task_name=None, task_description=None, task_id=None):
@@ -45,6 +45,15 @@ def __getattr__(name):
elif name == "python_type_to_json_schema":
from .mcp_utils import python_type_to_json_schema
return python_type_to_json_schema
elif name == "fix_array_schemas":
from .mcp_schema_utils import fix_array_schemas
return fix_array_schemas
elif name == "ThreadLocalEventLoop":
from .mcp_schema_utils import ThreadLocalEventLoop
return ThreadLocalEventLoop
elif name == "get_thread_local_event_loop":
from .mcp_schema_utils import get_thread_local_event_loop
return get_thread_local_event_loop
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
__all__ = [
@@ -57,6 +66,10 @@ __all__ = [
"function_to_mcp_schema",
"get_tool_metadata",
"python_type_to_json_schema",
# Schema utilities (lazy loaded)
"fix_array_schemas",
"ThreadLocalEventLoop",
"get_thread_local_event_loop",
# Transport components (lazy loaded)
"WebSocketMCPClient",
"SessionManager",
@@ -162,7 +162,7 @@ class MCP:
# Method 1: Using command and args separately
agent = Agent(
instructions="You are a helpful assistant...",
llm="gpt-5-nano",
llm="gpt-4o-mini",
tools=MCP(
command="/path/to/python",
args=["/path/to/app.py"]
@@ -172,14 +172,14 @@ class MCP:
# Method 2: Using a single command string
agent = Agent(
instructions="You are a helpful assistant...",
llm="gpt-5-nano",
llm="gpt-4o-mini",
tools=MCP("/path/to/python /path/to/app.py")
)
# Method 3: Using an SSE endpoint
agent = Agent(
instructions="You are a helpful assistant...",
llm="gpt-5-nano",
llm="gpt-4o-mini",
tools=MCP("http://localhost:8080/sse")
)
@@ -507,6 +507,26 @@ class MCP:
"""
return iter(self._tools)
def get_tools(self) -> List[Callable]:
"""
Get the list of tool functions from this MCP instance.
This method provides explicit access to the tools list, which is useful
when you need to inspect or manipulate the tools programmatically.
Returns:
List[Callable]: List of tool functions that can be called
Example:
```python
mcp = MCP("npx -y @modelcontextprotocol/server-time")
tools = mcp.get_tools()
for tool in tools:
print(f"Tool: {tool.__name__}")
```
"""
return self._tools
def _fix_array_schemas(self, schema):
"""
Fix array schemas by adding missing 'items' attribute required by OpenAI.
@@ -548,7 +568,7 @@ class MCP:
"""Convert the MCP tool to an OpenAI-compatible tool definition.
This method is specifically invoked by the Agent class when using
provider/model format (e.g., "openai/gpt-5-nano").
provider/model format (e.g., "openai/gpt-4o-mini").
Returns:
dict or list: OpenAI-compatible tool definition(s)
@@ -30,20 +30,19 @@ except ImportError:
logger = logging.getLogger("mcp-http-stream")
# Global event loop for async operations
_event_loop = None
# Import shared utilities for thread-safe event loop and schema fixing
from .mcp_schema_utils import ThreadLocalEventLoop, fix_array_schemas
# Thread-local event loop for async operations (thread-safe)
_event_loop_manager = ThreadLocalEventLoop()
# Global registry of active clients for cleanup
_active_clients = weakref.WeakSet()
_cleanup_registered = False
def get_event_loop():
"""Get or create a global event loop."""
global _event_loop
if _event_loop is None or _event_loop.is_closed():
_event_loop = asyncio.new_event_loop()
asyncio.set_event_loop(_event_loop)
return _event_loop
"""Get or create a thread-local event loop."""
return _event_loop_manager.get_loop()
def _cleanup_all_clients():
@@ -148,47 +147,10 @@ class HTTPStreamMCPTool:
logger.error(f"Error in _async_call for {self.name}: {e}")
raise
def _fix_array_schemas(self, schema):
"""
Fix array schemas by adding missing 'items' attribute required by OpenAI.
This ensures compatibility with OpenAI's function calling format which
requires array types to specify the type of items they contain.
Args:
schema: The schema dictionary to fix
Returns:
dict: The fixed schema
"""
if not isinstance(schema, dict):
return schema
# Create a copy to avoid modifying the original
fixed_schema = schema.copy()
# Fix array types at the current level
if fixed_schema.get("type") == "array" and "items" not in fixed_schema:
# Add a default items schema for arrays without it
fixed_schema["items"] = {"type": "string"}
# Recursively fix nested schemas
if "properties" in fixed_schema:
fixed_properties = {}
for prop_name, prop_schema in fixed_schema["properties"].items():
fixed_properties[prop_name] = self._fix_array_schemas(prop_schema)
fixed_schema["properties"] = fixed_properties
# Fix items schema if it exists
if "items" in fixed_schema:
fixed_schema["items"] = self._fix_array_schemas(fixed_schema["items"])
return fixed_schema
def to_openai_tool(self):
"""Convert the tool to OpenAI format."""
# Fix array schemas to include 'items' attribute
fixed_schema = self._fix_array_schemas(self.input_schema)
# Fix array schemas to include 'items' attribute (using shared utility)
fixed_schema = fix_array_schemas(self.input_schema)
return {
"type": "function",
@@ -0,0 +1,189 @@
"""
MCP Schema Utilities.
This module provides shared schema manipulation utilities for MCP transports.
These utilities ensure compatibility with OpenAI's function calling format
and other LLM providers.
DRY: This module centralizes schema fixing logic that was previously
duplicated across mcp.py, mcp_sse.py, mcp_http_stream.py, and mcp_websocket.py.
"""
import threading
from typing import Any, Dict, Optional
def fix_array_schemas(schema: Any) -> Any:
"""
Fix array schemas by adding missing 'items' attribute required by OpenAI.
This ensures compatibility with OpenAI's function calling format which
requires array types to specify the type of items they contain.
Args:
schema: The schema dictionary to fix
Returns:
dict: The fixed schema with 'items' added to array types
Example:
>>> schema = {"type": "array"}
>>> fixed = fix_array_schemas(schema)
>>> fixed
{'type': 'array', 'items': {'type': 'string'}}
"""
if not isinstance(schema, dict):
return schema
# Create a copy to avoid modifying the original
fixed_schema = schema.copy()
# Fix array types at the current level
if fixed_schema.get("type") == "array" and "items" not in fixed_schema:
# Add a default items schema for arrays without it
fixed_schema["items"] = {"type": "string"}
# Recursively fix nested schemas
if "properties" in fixed_schema:
fixed_properties = {}
for prop_name, prop_schema in fixed_schema["properties"].items():
fixed_properties[prop_name] = fix_array_schemas(prop_schema)
fixed_schema["properties"] = fixed_properties
# Fix items schema if it exists
if "items" in fixed_schema:
fixed_schema["items"] = fix_array_schemas(fixed_schema["items"])
# Fix additionalProperties if it's a schema
if isinstance(fixed_schema.get("additionalProperties"), dict):
fixed_schema["additionalProperties"] = fix_array_schemas(
fixed_schema["additionalProperties"]
)
# Fix anyOf/oneOf/allOf schemas
for key in ("anyOf", "oneOf", "allOf"):
if key in fixed_schema and isinstance(fixed_schema[key], list):
fixed_schema[key] = [fix_array_schemas(s) for s in fixed_schema[key]]
return fixed_schema
class ThreadLocalEventLoop:
"""
Thread-local event loop storage for MCP transports.
This class provides thread-safe event loop management, ensuring that
multiple MCP instances don't share the same event loop, which could
cause race conditions.
Usage:
event_loop_manager = ThreadLocalEventLoop()
loop = event_loop_manager.get_loop()
# Use loop for async operations
event_loop_manager.cleanup()
"""
def __init__(self):
"""Initialize thread-local storage."""
self._local = threading.local()
self._lock = threading.Lock()
def get_loop(self):
"""
Get or create an event loop for the current thread.
Returns:
asyncio.AbstractEventLoop: Event loop for current thread
"""
import asyncio
# Check if we have a loop for this thread
loop = getattr(self._local, 'loop', None)
if loop is None or loop.is_closed():
with self._lock:
# Double-check after acquiring lock
loop = getattr(self._local, 'loop', None)
if loop is None or loop.is_closed():
loop = asyncio.new_event_loop()
self._local.loop = loop
return loop
def set_loop(self, loop):
"""
Set the event loop for the current thread.
Args:
loop: The event loop to set
"""
self._local.loop = loop
def cleanup(self):
"""Clean up the event loop for the current thread."""
loop = getattr(self._local, 'loop', None)
if loop is not None and not loop.is_closed():
try:
loop.close()
except Exception:
pass
self._local.loop = None
# Singleton instance for backward compatibility with existing code
# that uses get_event_loop() function pattern
_default_event_loop_manager = ThreadLocalEventLoop()
def get_thread_local_event_loop():
"""
Get or create a thread-local event loop.
This is a drop-in replacement for the global get_event_loop() functions
in the transport modules, providing thread safety.
Returns:
asyncio.AbstractEventLoop: Event loop for current thread
"""
return _default_event_loop_manager.get_loop()
def json_type_to_python_type(json_type: str) -> type:
"""
Convert JSON Schema type to Python type annotation.
Args:
json_type: JSON Schema type string
Returns:
Python type corresponding to the JSON type
"""
type_map = {
'string': str,
'integer': int,
'number': float,
'boolean': bool,
'array': list,
'object': dict,
'null': type(None),
}
return type_map.get(json_type, str)
def extract_schema_properties(input_schema: Optional[Dict[str, Any]]) -> Dict[str, Any]:
"""
Extract properties and required fields from an input schema.
Args:
input_schema: JSON Schema for tool input
Returns:
Dictionary with 'properties' and 'required' keys
"""
if not input_schema:
return {'properties': {}, 'required': []}
return {
'properties': input_schema.get('properties', {}),
'required': input_schema.get('required', [])
}
@@ -22,16 +22,15 @@ except ImportError:
logger = logging.getLogger("mcp-sse")
# Global event loop for async operations
_event_loop = None
# Import shared utilities for thread-safe event loop and schema fixing
from .mcp_schema_utils import ThreadLocalEventLoop, fix_array_schemas
# Thread-local event loop for async operations (thread-safe)
_event_loop_manager = ThreadLocalEventLoop()
def get_event_loop():
"""Get or create a global event loop."""
global _event_loop
if _event_loop is None or _event_loop.is_closed():
_event_loop = asyncio.new_event_loop()
asyncio.set_event_loop(_event_loop)
return _event_loop
"""Get or create a thread-local event loop."""
return _event_loop_manager.get_loop()
class SSEMCPTool:
@@ -111,47 +110,10 @@ class SSEMCPTool:
logger.error(f"Error in _async_call for {self.name}: {e}")
raise
def _fix_array_schemas(self, schema):
"""
Fix array schemas by adding missing 'items' attribute required by OpenAI.
This ensures compatibility with OpenAI's function calling format which
requires array types to specify the type of items they contain.
Args:
schema: The schema dictionary to fix
Returns:
dict: The fixed schema
"""
if not isinstance(schema, dict):
return schema
# Create a copy to avoid modifying the original
fixed_schema = schema.copy()
# Fix array types at the current level
if fixed_schema.get("type") == "array" and "items" not in fixed_schema:
# Add a default items schema for arrays without it
fixed_schema["items"] = {"type": "string"}
# Recursively fix nested schemas
if "properties" in fixed_schema:
fixed_properties = {}
for prop_name, prop_schema in fixed_schema["properties"].items():
fixed_properties[prop_name] = self._fix_array_schemas(prop_schema)
fixed_schema["properties"] = fixed_properties
# Fix items schema if it exists
if "items" in fixed_schema:
fixed_schema["items"] = self._fix_array_schemas(fixed_schema["items"])
return fixed_schema
def to_openai_tool(self):
"""Convert the tool to OpenAI format."""
# Fix array schemas to include 'items' attribute
fixed_schema = self._fix_array_schemas(self.input_schema)
# Fix array schemas to include 'items' attribute (using shared utility)
fixed_schema = fix_array_schemas(self.input_schema)
return {
"type": "function",
@@ -26,6 +26,9 @@ from typing import Any, Dict, Optional, List
logger = logging.getLogger("mcp-websocket")
# Import shared utilities for thread-safe event loop and schema fixing
from .mcp_schema_utils import ThreadLocalEventLoop, fix_array_schemas
def is_websocket_url(url: str) -> bool:
"""
@@ -58,18 +61,13 @@ def calculate_backoff(attempt: int, base_delay: float = 1.0, max_delay: float =
return min(delay, max_delay)
# Global event loop for WebSocket operations (similar to other MCP transports)
_event_loop = None
_loop_lock = threading.Lock()
# Thread-local event loop for WebSocket operations (thread-safe)
_event_loop_manager = ThreadLocalEventLoop()
def get_event_loop():
"""Get or create a global event loop for WebSocket operations."""
global _event_loop
with _loop_lock:
if _event_loop is None or _event_loop.is_closed():
_event_loop = asyncio.new_event_loop()
return _event_loop
"""Get or create a thread-local event loop for WebSocket operations."""
return _event_loop_manager.get_loop()
class WebSocketTransport:
@@ -359,36 +357,10 @@ class WebSocketMCPTool:
logger.error(f"Error in _async_call for {self.name}: {e}")
raise
def _fix_array_schemas(self, schema: Dict[str, Any]) -> Dict[str, Any]:
"""
Fix array schemas by adding missing 'items' attribute.
OpenAI requires array types to specify item types.
"""
if not isinstance(schema, dict):
return schema
fixed_schema = schema.copy()
# Fix array types at current level
if fixed_schema.get("type") == "array" and "items" not in fixed_schema:
fixed_schema["items"] = {"type": "string"}
# Recursively fix nested schemas
if "properties" in fixed_schema:
fixed_properties = {}
for prop_name, prop_schema in fixed_schema["properties"].items():
fixed_properties[prop_name] = self._fix_array_schemas(prop_schema)
fixed_schema["properties"] = fixed_properties
if "items" in fixed_schema:
fixed_schema["items"] = self._fix_array_schemas(fixed_schema["items"])
return fixed_schema
def to_openai_tool(self) -> Dict[str, Any]:
"""Convert the tool to OpenAI function calling format."""
fixed_schema = self._fix_array_schemas(self.input_schema)
# Fix array schemas to include 'items' attribute (using shared utility)
fixed_schema = fix_array_schemas(self.input_schema)
return {
"type": "function",
@@ -18,6 +18,11 @@ This module provides memory management capabilities including:
- Docs management (like Cursor docs)
- MCP config management (like Cursor .cursor/mcp/)
Protocols:
- MemoryProtocol: Minimal interface for memory implementations
- AsyncMemoryProtocol: Async interface for memory
- ResettableMemoryProtocol: Interface with reset methods
Memory Providers:
- FileMemory: Zero-dependency JSON file-based storage (default)
- Memory: Full-featured with SQLite, ChromaDB, Mem0, MongoDB support
@@ -33,6 +38,13 @@ from .file_memory import FileMemory, create_memory
from .rules_manager import RulesManager, Rule, create_rules_manager
from .docs_manager import DocsManager, Doc
from .mcp_config import MCPConfigManager, MCPConfig
from .protocols import (
MemoryProtocol,
AsyncMemoryProtocol,
ResettableMemoryProtocol,
EntityMemoryProtocol,
)
# Lazy imports for optional modules to avoid dependency issues and improve startup time
def __getattr__(name):
@@ -110,6 +122,31 @@ def __getattr__(name):
if name == "create_hooks_manager":
from .hooks import create_hooks_manager
return create_hooks_manager
# Learn module
if name == "LearnManager":
from .learn import LearnManager
return LearnManager
if name == "PersonaStore":
from .learn import PersonaStore
return PersonaStore
if name == "InsightStore":
from .learn import InsightStore
return InsightStore
if name == "ThreadStore":
from .learn import ThreadStore
return ThreadStore
if name == "PatternStore":
from .learn import PatternStore
return PatternStore
if name == "DecisionStore":
from .learn import DecisionStore
return DecisionStore
if name == "FeedbackStore":
from .learn import FeedbackStore
return FeedbackStore
if name == "ImprovementStore":
from .learn import ImprovementStore
return ImprovementStore
# DocsManager and MCPConfigManager are already imported at module level
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
@@ -153,4 +190,18 @@ __all__ = [
# MCP config management
"MCPConfigManager",
"MCPConfig",
# Learn module
"LearnManager",
"PersonaStore",
"InsightStore",
"ThreadStore",
"PatternStore",
"DecisionStore",
"FeedbackStore",
"ImprovementStore",
# Protocols
"MemoryProtocol",
"AsyncMemoryProtocol",
"ResettableMemoryProtocol",
"EntityMemoryProtocol",
]
@@ -18,7 +18,7 @@ Storage Structure:
import json
import time
import fcntl
import sys
import hashlib
import logging
from pathlib import Path
@@ -26,9 +26,17 @@ from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional
from dataclasses import dataclass, asdict, field
# fcntl is Unix-only; on Windows, skip file locking (acceptable for single-process usage)
if sys.platform != 'win32':
import fcntl
_HAS_FCNTL = True
else:
_HAS_FCNTL = False
logger = logging.getLogger(__name__)
@dataclass
class MemoryItem:
"""A single memory item."""
@@ -178,35 +186,41 @@ class FileMemory:
# -------------------------------------------------------------------------
def _read_json(self, filepath: Path, default: Any = None) -> Any:
"""Read JSON file with file locking."""
"""Read JSON file with file locking (Unix only)."""
if not filepath.exists():
return default if default is not None else []
try:
with open(filepath, 'r', encoding='utf-8') as f:
fcntl.flock(f.fileno(), fcntl.LOCK_SH)
if _HAS_FCNTL:
fcntl.flock(f.fileno(), fcntl.LOCK_SH)
try:
data = json.load(f)
finally:
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
if _HAS_FCNTL:
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
return data
except (json.JSONDecodeError, IOError) as e:
self._log(f"Error reading {filepath}: {e}", logging.WARNING)
return default if default is not None else []
def _write_json(self, filepath: Path, data: Any) -> bool:
"""Write JSON file with file locking."""
"""Write JSON file with file locking (Unix only)."""
try:
with open(filepath, 'w', encoding='utf-8') as f:
fcntl.flock(f.fileno(), fcntl.LOCK_EX)
if _HAS_FCNTL:
fcntl.flock(f.fileno(), fcntl.LOCK_EX)
try:
json.dump(data, f, indent=2, ensure_ascii=False)
finally:
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
if _HAS_FCNTL:
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
return True
except IOError as e:
self._log(f"Error writing {filepath}: {e}", logging.ERROR)
return False
def _load_config(self, overrides: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
"""Load or create configuration."""
Loaded 100 of 453 files, more files were not shown because too many files have changed in this diff. Show more