mirror of
https://github.com/MervinPraison/PraisonAI.git
synced 2026-11-07 17:33:33 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af56a753bd | ||
|
|
15c2559a3c | ||
|
|
3215bdc0a6 | ||
|
|
0ff0d83744 | ||
|
|
8f956f91dd | ||
|
|
954f6ac33d | ||
|
|
264d11d8d9 | ||
|
|
88da2bdd7b | ||
|
|
b84e6c2460 | ||
|
|
0434b23ada | ||
|
|
a9951e0694 | ||
|
|
073062cd96 | ||
|
|
936b422876 | ||
|
|
99ab421ce2 | ||
|
|
c1f334905e | ||
|
|
6d3ba7cdf8 | ||
|
|
96616498d2 | ||
|
|
1cad345415 | ||
|
|
802e7e52f9 | ||
|
|
53704f234c | ||
|
|
b575994d91 |
No files matched your search
@@ -252,20 +252,20 @@ npm install praisonai
|
||||
| 🪝 Hooks | [Example](#9-hooks) | [📖](https://docs.praison.ai/features/hooks) |
|
||||
| 📈 Telemetry | [Example](examples/python/telemetry/production-telemetry-example.py) | [📖](https://docs.praison.ai/features/telemetry) |
|
||||
| 📹 Camera Integration | [Example](examples/python/camera/) | [📖](https://docs.praison.ai/features/camera-integration) |
|
||||
| 📄 Project Docs (.praison/docs/) | [Example](#docs-cli) | [📖](https://docs.praison.ai/cli/docs) |
|
||||
| 🔌 MCP Config Management | [Example](#mcp-config-cli) | [📖](https://docs.praison.ai/cli/mcp) |
|
||||
| 💬 AI Commit Messages | [Example](#ai-commit-cli) | [📖](https://docs.praison.ai/cli/commit) |
|
||||
| @ @Mentions in Prompts | [Example](#mentions-in-prompts) | [📖](https://docs.praison.ai/cli/mentions) |
|
||||
| 💾 Auto-Save Sessions | [Example](#session-management-python) | [📖](https://docs.praison.ai/cli/session) |
|
||||
| 📜 History in Context | [Example](#session-management-python) | [📖](https://docs.praison.ai/cli/session) |
|
||||
| 📄 Project Docs (.praison/docs/) | [Example](#docs-cli) | [📖](https://docs.praison.ai/docs/cli/docs) |
|
||||
| 🔌 MCP Config Management | [Example](#mcp-config-cli) | [📖](https://docs.praison.ai/docs/cli/mcp) |
|
||||
| 💬 AI Commit Messages | [Example](#ai-commit-cli) | [📖](https://docs.praison.ai/docs/cli/commit) |
|
||||
| @ @Mentions in Prompts | [Example](#mentions-in-prompts) | [📖](https://docs.praison.ai/docs/cli/mentions) |
|
||||
| 💾 Auto-Save Sessions | [Example](#session-management-python) | [📖](https://docs.praison.ai/docs/cli/session) |
|
||||
| 📜 History in Context | [Example](#session-management-python) | [📖](https://docs.praison.ai/docs/cli/session) |
|
||||
| **🖥️ CLI Features** | | |
|
||||
| ↳ Slash Commands | [Example](examples/python/cli/slash_commands_example.py) | [📖](https://docs.praison.ai/cli/slash-commands) |
|
||||
| ↳ Autonomy Modes | [Example](examples/python/cli/autonomy_modes_example.py) | [📖](https://docs.praison.ai/cli/autonomy-modes) |
|
||||
| ↳ Cost Tracking | [Example](examples/python/cli/cost_tracking_example.py) | [📖](https://docs.praison.ai/cli/cost-tracking) |
|
||||
| ↳ Repository Map | [Example](examples/python/cli/repo_map_example.py) | [📖](https://docs.praison.ai/cli/repo-map) |
|
||||
| ↳ Interactive TUI | [Example](examples/python/cli/interactive_tui_example.py) | [📖](https://docs.praison.ai/cli/interactive-tui) |
|
||||
| ↳ Git Integration | [Example](examples/python/cli/git_integration_example.py) | [📖](https://docs.praison.ai/cli/git-integration) |
|
||||
| ↳ Sandbox Execution | [Example](examples/python/cli/sandbox_execution_example.py) | [📖](https://docs.praison.ai/cli/sandbox-execution) |
|
||||
| ↳ Slash Commands | [Example](examples/python/cli/slash_commands_example.py) | [📖](https://docs.praison.ai/docs/cli/slash-commands) |
|
||||
| ↳ Autonomy Modes | [Example](examples/python/cli/autonomy_modes_example.py) | [📖](https://docs.praison.ai/docs/cli/autonomy-modes) |
|
||||
| ↳ Cost Tracking | [Example](examples/python/cli/cost_tracking_example.py) | [📖](https://docs.praison.ai/docs/cli/cost-tracking) |
|
||||
| ↳ Repository Map | [Example](examples/python/cli/repo_map_example.py) | [📖](https://docs.praison.ai/docs/cli/repo-map) |
|
||||
| ↳ Interactive TUI | [Example](examples/python/cli/interactive_tui_example.py) | [📖](https://docs.praison.ai/docs/cli/interactive-tui) |
|
||||
| ↳ Git Integration | [Example](examples/python/cli/git_integration_example.py) | [📖](https://docs.praison.ai/docs/cli/git-integration) |
|
||||
| ↳ Sandbox Execution | [Example](examples/python/cli/sandbox_execution_example.py) | [📖](https://docs.praison.ai/docs/cli/sandbox-execution) |
|
||||
|
||||
---
|
||||
|
||||
@@ -972,6 +972,32 @@ export OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxx
|
||||
praisonai --auto create a movie script about Robots in Mars
|
||||
```
|
||||
|
||||
### Interactive Mode CLI:
|
||||
```bash
|
||||
# Start interactive terminal mode (inspired by Gemini CLI, Codex CLI, Claude Code)
|
||||
praisonai --interactive
|
||||
praisonai -i
|
||||
|
||||
# Features:
|
||||
# - Streaming responses (no boxes)
|
||||
# - Built-in tools: read_file, write_file, list_files, execute_command, internet_search
|
||||
# - Slash commands: /help, /exit, /tools, /clear
|
||||
|
||||
# Chat mode - single prompt with interactive style (for testing/scripting)
|
||||
# Use --chat (or --chat-mode for backward compatibility)
|
||||
praisonai "list files in current folder" --chat
|
||||
praisonai "search the web for AI news" --chat
|
||||
praisonai "read README.md" --chat
|
||||
```
|
||||
|
||||
### Chat UI (Web Interface):
|
||||
```bash
|
||||
# Start web-based Chainlit chat interface (requires praisonai[chat])
|
||||
pip install "praisonai[chat]"
|
||||
praisonai chat
|
||||
# Opens browser at http://localhost:8084
|
||||
```
|
||||
|
||||
### Query Rewriting (works with any command):
|
||||
```bash
|
||||
# Rewrite query for better results (uses QueryRewriterAgent)
|
||||
@@ -1032,6 +1058,20 @@ praisonai "Complex analysis task" --planning --planning-reasoning
|
||||
praisonai "Task" --planning --auto-approve-plan
|
||||
```
|
||||
|
||||
### Tool Approval CLI:
|
||||
```bash
|
||||
# Auto-approve ALL tool executions (use with caution!)
|
||||
praisonai "run ls command" --trust
|
||||
|
||||
# Auto-approve tools up to a risk level (prompt for higher)
|
||||
# Levels: low, medium, high, critical
|
||||
praisonai "write to file" --approve-level high # Prompts for critical tools only
|
||||
praisonai "task" --approve-level medium # Prompts for high and critical
|
||||
|
||||
# Default behavior (no flags): prompts for all dangerous tools
|
||||
praisonai "run shell command" # Will prompt for approval
|
||||
```
|
||||
|
||||
### Memory CLI:
|
||||
```bash
|
||||
# Enable memory for agent (persists across sessions)
|
||||
@@ -1446,13 +1486,31 @@ praisonai mcp help
|
||||
|
||||
### AI Commit CLI:
|
||||
```bash
|
||||
# Generate AI commit message for staged changes
|
||||
# Full auto mode: stage all, security check, commit, and push
|
||||
praisonai commit -a
|
||||
|
||||
# Interactive mode (requires git add first)
|
||||
praisonai commit
|
||||
|
||||
# Generate, commit, and push
|
||||
# Interactive with auto-push
|
||||
praisonai commit --push
|
||||
|
||||
# Skip security check (not recommended)
|
||||
praisonai commit -a --no-verify
|
||||
```
|
||||
|
||||
**Features:**
|
||||
- 🤖 AI-generated conventional commit messages
|
||||
- 🔒 Built-in security scanning (API keys, passwords, secrets, sensitive files)
|
||||
- 📦 Auto-staging with `-a` flag
|
||||
- 🚀 Auto-push in full auto mode
|
||||
- ✏️ Edit message before commit in interactive mode
|
||||
|
||||
**Security Detection:**
|
||||
- API keys, secrets, tokens (AWS, GitHub, GitLab, Slack)
|
||||
- Passwords and private keys
|
||||
- Sensitive files (`.env`, `id_rsa`, `.pem`, `.key`, etc.)
|
||||
|
||||
### Serve CLI (API Server):
|
||||
```bash
|
||||
# Start API server for agents defined in YAML
|
||||
@@ -1757,32 +1815,32 @@ app.include_router(a2a.get_router())
|
||||
|
||||
| Feature | Docs |
|
||||
|---------|:----:|
|
||||
| 🔄 Query Rewrite - RAG optimization | [📖](https://docs.praison.ai/cli/query-rewrite) |
|
||||
| 🔬 Deep Research - Automated research | [📖](https://docs.praison.ai/cli/deep-research) |
|
||||
| 📋 Planning - Step-by-step execution | [📖](https://docs.praison.ai/cli/planning) |
|
||||
| 💾 Memory - Persistent agent memory | [📖](https://docs.praison.ai/cli/memory) |
|
||||
| 📜 Rules - Auto-discovered instructions | [📖](https://docs.praison.ai/cli/rules) |
|
||||
| 🔄 Workflow - Multi-step workflows | [📖](https://docs.praison.ai/cli/workflow) |
|
||||
| 🪝 Hooks - Event-driven actions | [📖](https://docs.praison.ai/cli/hooks) |
|
||||
| 🧠 Claude Memory - Anthropic memory tool | [📖](https://docs.praison.ai/cli/claude-memory) |
|
||||
| 🛡️ Guardrail - Output validation | [📖](https://docs.praison.ai/cli/guardrail) |
|
||||
| 📊 Metrics - Token usage tracking | [📖](https://docs.praison.ai/cli/metrics) |
|
||||
| 🖼️ Image - Vision processing | [📖](https://docs.praison.ai/cli/image) |
|
||||
| 📡 Telemetry - Usage monitoring | [📖](https://docs.praison.ai/cli/telemetry) |
|
||||
| 🔌 MCP - Model Context Protocol | [📖](https://docs.praison.ai/cli/mcp) |
|
||||
| ⚡ Fast Context - Codebase search | [📖](https://docs.praison.ai/cli/fast-context) |
|
||||
| 📚 Knowledge - RAG management | [📖](https://docs.praison.ai/cli/knowledge) |
|
||||
| 💬 Session - Conversation management | [📖](https://docs.praison.ai/cli/session) |
|
||||
| 🔧 Tools - Tool discovery | [📖](https://docs.praison.ai/cli/tools) |
|
||||
| 🤝 Handoff - Agent delegation | [📖](https://docs.praison.ai/cli/handoff) |
|
||||
| 🧠 Auto Memory - Memory extraction | [📖](https://docs.praison.ai/cli/auto-memory) |
|
||||
| 📋 Todo - Task management | [📖](https://docs.praison.ai/cli/todo) |
|
||||
| 🎯 Router - Smart model selection | [📖](https://docs.praison.ai/cli/router) |
|
||||
| 📈 Flow Display - Visual workflow | [📖](https://docs.praison.ai/cli/flow-display) |
|
||||
| ✨ Prompt Expansion - Detailed prompts | [📖](https://docs.praison.ai/cli/prompt-expansion) |
|
||||
| 🌐 Web Search - Real-time search | [📖](https://docs.praison.ai/cli/web-search) |
|
||||
| 📥 Web Fetch - URL content retrieval | [📖](https://docs.praison.ai/cli/web-fetch) |
|
||||
| 💾 Prompt Caching - Cost reduction | [📖](https://docs.praison.ai/cli/prompt-caching) |
|
||||
| 🔄 Query Rewrite - RAG optimization | [📖](https://docs.praison.ai/docs/cli/query-rewrite) |
|
||||
| 🔬 Deep Research - Automated research | [📖](https://docs.praison.ai/docs/cli/deep-research) |
|
||||
| 📋 Planning - Step-by-step execution | [📖](https://docs.praison.ai/docs/cli/planning) |
|
||||
| 💾 Memory - Persistent agent memory | [📖](https://docs.praison.ai/docs/cli/memory) |
|
||||
| 📜 Rules - Auto-discovered instructions | [📖](https://docs.praison.ai/docs/cli/rules) |
|
||||
| 🔄 Workflow - Multi-step workflows | [📖](https://docs.praison.ai/docs/cli/workflow) |
|
||||
| 🪝 Hooks - Event-driven actions | [📖](https://docs.praison.ai/docs/cli/hooks) |
|
||||
| 🧠 Claude Memory - Anthropic memory tool | [📖](https://docs.praison.ai/docs/cli/claude-memory) |
|
||||
| 🛡️ Guardrail - Output validation | [📖](https://docs.praison.ai/docs/cli/guardrail) |
|
||||
| 📊 Metrics - Token usage tracking | [📖](https://docs.praison.ai/docs/cli/metrics) |
|
||||
| 🖼️ Image - Vision processing | [📖](https://docs.praison.ai/docs/cli/image) |
|
||||
| 📡 Telemetry - Usage monitoring | [📖](https://docs.praison.ai/docs/cli/telemetry) |
|
||||
| 🔌 MCP - Model Context Protocol | [📖](https://docs.praison.ai/docs/cli/mcp) |
|
||||
| ⚡ Fast Context - Codebase search | [📖](https://docs.praison.ai/docs/cli/fast-context) |
|
||||
| 📚 Knowledge - RAG management | [📖](https://docs.praison.ai/docs/cli/knowledge) |
|
||||
| 💬 Session - Conversation management | [📖](https://docs.praison.ai/docs/cli/session) |
|
||||
| 🔧 Tools - Tool discovery | [📖](https://docs.praison.ai/docs/cli/tools) |
|
||||
| 🤝 Handoff - Agent delegation | [📖](https://docs.praison.ai/docs/cli/handoff) |
|
||||
| 🧠 Auto Memory - Memory extraction | [📖](https://docs.praison.ai/docs/cli/auto-memory) |
|
||||
| 📋 Todo - Task management | [📖](https://docs.praison.ai/docs/cli/todo) |
|
||||
| 🎯 Router - Smart model selection | [📖](https://docs.praison.ai/docs/cli/router) |
|
||||
| 📈 Flow Display - Visual workflow | [📖](https://docs.praison.ai/docs/cli/flow-display) |
|
||||
| ✨ Prompt Expansion - Detailed prompts | [📖](https://docs.praison.ai/docs/cli/prompt-expansion) |
|
||||
| 🌐 Web Search - Real-time search | [📖](https://docs.praison.ai/docs/cli/web-search) |
|
||||
| 📥 Web Fetch - URL content retrieval | [📖](https://docs.praison.ai/docs/cli/web-fetch) |
|
||||
| 💾 Prompt Caching - Cost reduction | [📖](https://docs.praison.ai/docs/cli/prompt-caching) |
|
||||
|
||||
---
|
||||
|
||||
@@ -2355,6 +2413,7 @@ PraisonAI provides zero-dependency persistent memory for agents. For detailed ex
|
||||
- 🤝 **Agent Handoffs** - Transfer context between specialised agents
|
||||
- 🛡️ **Guardrails** - Input/output validation and safety checks
|
||||
- ✅ **Human Approval** - Require human confirmation for critical actions
|
||||
- 🔐 **Tool Approval CLI** - `--trust` (auto-approve all) and `--approve-level` (risk-based approval)
|
||||
- 💬 **Sessions Management** - Isolated conversation contexts
|
||||
- 🔄 **Stateful Agents** - Maintain state across interactions
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=2.3.48" \
|
||||
"praisonai>=2.3.63" \
|
||||
"praisonai[chat]" \
|
||||
"embedchain[github,youtube]"
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=2.3.48" \
|
||||
"praisonai>=2.3.63" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[chat]" \
|
||||
"praisonai[realtime]" \
|
||||
|
||||
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=2.3.48" \
|
||||
"praisonai>=2.3.63" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[crewai]"
|
||||
|
||||
|
||||
@@ -55,10 +55,10 @@ def _get_all_noisy_loggers() -> List[str]:
|
||||
|
||||
def _configure_loggers():
|
||||
"""Configure all loggers based on LOGLEVEL environment variable."""
|
||||
loglevel = os.environ.get('LOGLEVEL', 'INFO').upper()
|
||||
loglevel = os.environ.get('LOGLEVEL', 'WARNING').upper()
|
||||
|
||||
# When DEBUG is set, allow some HTTP logging for API endpoints
|
||||
if loglevel == 'DEBUG':
|
||||
# When DEBUG or INFO is set, allow some HTTP logging for API endpoints
|
||||
if loglevel in ('DEBUG', 'INFO'):
|
||||
allowed_debug_loggers = {"httpx", "httpx._client", "openai._client"}
|
||||
|
||||
for logger_name in _get_all_noisy_loggers():
|
||||
@@ -109,10 +109,10 @@ def configure_root_logger():
|
||||
"""Configure the root logger with RichHandler."""
|
||||
from rich.logging import RichHandler
|
||||
|
||||
loglevel = os.environ.get('LOGLEVEL', 'INFO').upper()
|
||||
loglevel = os.environ.get('LOGLEVEL', 'WARNING').upper()
|
||||
|
||||
logging.basicConfig(
|
||||
level=getattr(logging, loglevel, logging.INFO),
|
||||
level=getattr(logging, loglevel, logging.WARNING),
|
||||
format="%(asctime)s %(filename)s:%(lineno)d %(levelname)s %(message)s",
|
||||
datefmt="[%X]",
|
||||
handlers=[RichHandler(rich_tracebacks=True)],
|
||||
|
||||
@@ -1538,13 +1538,13 @@ Your Goal: {self.goal}"""
|
||||
logging.debug(f"{self.name} executing tool {function_name} with arguments: {arguments}")
|
||||
|
||||
# Check if approval is required for this tool
|
||||
from ..approval import is_approval_required, console_approval_callback, get_risk_level, mark_approved, ApprovalDecision
|
||||
from ..approval import is_approval_required, console_approval_callback, get_risk_level, mark_approved, ApprovalDecision, get_approval_callback
|
||||
if is_approval_required(function_name):
|
||||
risk_level = get_risk_level(function_name)
|
||||
logging.info(f"Tool {function_name} requires approval (risk level: {risk_level})")
|
||||
logging.debug(f"Tool {function_name} requires approval (risk level: {risk_level})")
|
||||
|
||||
# Use global approval callback or default console callback
|
||||
callback = approval_callback or console_approval_callback
|
||||
callback = get_approval_callback() or console_approval_callback
|
||||
|
||||
try:
|
||||
decision = callback(function_name, arguments, risk_level)
|
||||
|
||||
@@ -43,6 +43,15 @@ def set_approval_callback(callback_fn: Callable):
|
||||
global approval_callback
|
||||
approval_callback = callback_fn
|
||||
|
||||
def get_approval_callback() -> Optional[Callable]:
|
||||
"""Get the current approval callback function.
|
||||
|
||||
Returns the custom callback if set, otherwise None.
|
||||
This should be used instead of directly accessing approval_callback
|
||||
to ensure the latest callback is always used.
|
||||
"""
|
||||
return approval_callback
|
||||
|
||||
def mark_approved(tool_name: str):
|
||||
"""Mark a tool as approved in the current context."""
|
||||
approved = _approved_context.get(set())
|
||||
@@ -90,7 +99,7 @@ def require_approval(risk_level: RiskLevel = "high"):
|
||||
except Exception as e:
|
||||
# Fallback to sync approval if async fails
|
||||
logging.warning(f"Async approval failed, using sync fallback: {e}")
|
||||
callback = approval_callback or console_approval_callback
|
||||
callback = get_approval_callback() or console_approval_callback
|
||||
decision = callback(tool_name, kwargs, risk_level)
|
||||
|
||||
if not decision.approved:
|
||||
@@ -198,7 +207,7 @@ async def request_approval(function_name: str, arguments: Dict[str, Any]) -> App
|
||||
risk_level = TOOL_RISK_LEVELS.get(function_name, "medium")
|
||||
|
||||
# Use custom callback if set, otherwise use console callback
|
||||
callback = approval_callback or console_approval_callback
|
||||
callback = get_approval_callback() or console_approval_callback
|
||||
|
||||
try:
|
||||
# Handle async callbacks
|
||||
|
||||
@@ -1054,7 +1054,7 @@ class LLM:
|
||||
**kwargs
|
||||
) -> str:
|
||||
"""Enhanced get_response with all OpenAI-like features"""
|
||||
logging.info(f"Getting response from {self.model}")
|
||||
logging.debug(f"Getting response from {self.model}")
|
||||
# Log all self values when in debug mode
|
||||
self._log_llm_config(
|
||||
'LLM instance',
|
||||
@@ -2472,7 +2472,7 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
"""Async version of get_response with identical functionality."""
|
||||
try:
|
||||
import litellm
|
||||
logging.info(f"Getting async response from {self.model}")
|
||||
logging.debug(f"Getting async response from {self.model}")
|
||||
# Log all self values when in debug mode
|
||||
self._log_llm_config(
|
||||
'get_response_async',
|
||||
|
||||
@@ -19,6 +19,15 @@ from rich.console import Console
|
||||
from rich.live import Live
|
||||
import inspect
|
||||
|
||||
# Import display_tool_call for callback support (lazy import to avoid circular imports)
|
||||
_display_tool_call = None
|
||||
def _get_display_tool_call():
|
||||
global _display_tool_call
|
||||
if _display_tool_call is None:
|
||||
from ..main import display_tool_call
|
||||
_display_tool_call = display_tool_call
|
||||
return _display_tool_call
|
||||
|
||||
# Constants
|
||||
LOCAL_SERVER_API_KEY_PLACEHOLDER = "not-needed"
|
||||
|
||||
@@ -978,16 +987,19 @@ class OpenAIClient:
|
||||
function_name = tool_call.function.name
|
||||
arguments = json.loads(tool_call.function.arguments)
|
||||
|
||||
# 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"[bold]Calling function:[/bold] {function_name}")
|
||||
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"
|
||||
|
||||
if verbose and console:
|
||||
console.print(f"[dim]Result:[/dim] {results_str}")
|
||||
# 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)
|
||||
|
||||
messages.append({
|
||||
"role": "tool",
|
||||
@@ -1150,8 +1162,11 @@ class OpenAIClient:
|
||||
function_name = tool_call.function.name
|
||||
arguments = json.loads(tool_call.function.arguments)
|
||||
|
||||
# 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"[bold]Calling function:[/bold] {function_name}")
|
||||
console.print(f"[dim]Arguments:[/dim] {arguments}")
|
||||
|
||||
# Execute the tool (async)
|
||||
@@ -1167,8 +1182,8 @@ class OpenAIClient:
|
||||
|
||||
results_str = json.dumps(tool_result) if tool_result else "Function returned an empty output"
|
||||
|
||||
if verbose and console:
|
||||
console.print(f"[dim]Result:[/dim] {results_str}")
|
||||
# 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)
|
||||
|
||||
messages.append({
|
||||
"role": "tool",
|
||||
@@ -1299,6 +1314,10 @@ class OpenAIClient:
|
||||
yield f"\n[Error parsing arguments for {function_name if 'function_name' in locals() else 'unknown function'}: {str(e)}]"
|
||||
continue
|
||||
|
||||
# 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=None)
|
||||
|
||||
if verbose:
|
||||
yield f"\n[Calling function: {function_name}]"
|
||||
|
||||
@@ -1311,6 +1330,9 @@ class OpenAIClient:
|
||||
if verbose:
|
||||
yield f"\n[Function error: {str(e)}]"
|
||||
|
||||
# Trigger callback with result
|
||||
display_tool_call_fn(f"Function {function_name} returned: {results_str[:200]}{'...' if len(results_str) > 200 else ''}", console=None)
|
||||
|
||||
if verbose:
|
||||
yield f"\n[Function result: {results_str}]"
|
||||
|
||||
|
||||
@@ -231,15 +231,15 @@ def display_tool_call(message: str, console=None):
|
||||
if not message or not message.strip():
|
||||
logging.debug("Empty message in display_tool_call, returning early")
|
||||
return
|
||||
if console is None:
|
||||
console = Console()
|
||||
message = _clean_display_content(str(message))
|
||||
logging.debug(f"Cleaned message in display_tool_call: {repr(message)}")
|
||||
|
||||
# Execute synchronous callbacks
|
||||
# Execute synchronous callbacks (always, even when console is None)
|
||||
execute_sync_callback('tool_call', message=message)
|
||||
|
||||
console.print(Panel.fit(Text(message, style="bold cyan"), title="Tool Call", border_style="green"))
|
||||
# Only print panel if console is provided (verbose mode)
|
||||
if console is not None:
|
||||
console.print(Panel.fit(Text(message, style="bold cyan"), title="Tool Call", border_style="green"))
|
||||
|
||||
def display_error(message: str, console=None):
|
||||
if not message or not message.strip():
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "praisonaiagents"
|
||||
version = "0.1.17"
|
||||
version = "0.1.21"
|
||||
description = "Praison AI agents for completing complex tasks with Self Reflection Agents"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
|
||||
Generated
+1
-1
@@ -2422,7 +2422,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "praisonaiagents"
|
||||
version = "0.1.17"
|
||||
version = "0.1.21"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "aiohttp" },
|
||||
|
||||
+100
-41
@@ -252,20 +252,20 @@ npm install praisonai
|
||||
| 🪝 Hooks | [Example](#9-hooks) | [📖](https://docs.praison.ai/features/hooks) |
|
||||
| 📈 Telemetry | [Example](examples/python/telemetry/production-telemetry-example.py) | [📖](https://docs.praison.ai/features/telemetry) |
|
||||
| 📹 Camera Integration | [Example](examples/python/camera/) | [📖](https://docs.praison.ai/features/camera-integration) |
|
||||
| 📄 Project Docs (.praison/docs/) | [Example](#docs-cli) | [📖](https://docs.praison.ai/cli/docs) |
|
||||
| 🔌 MCP Config Management | [Example](#mcp-config-cli) | [📖](https://docs.praison.ai/cli/mcp) |
|
||||
| 💬 AI Commit Messages | [Example](#ai-commit-cli) | [📖](https://docs.praison.ai/cli/commit) |
|
||||
| @ @Mentions in Prompts | [Example](#mentions-in-prompts) | [📖](https://docs.praison.ai/cli/mentions) |
|
||||
| 💾 Auto-Save Sessions | [Example](#session-management-python) | [📖](https://docs.praison.ai/cli/session) |
|
||||
| 📜 History in Context | [Example](#session-management-python) | [📖](https://docs.praison.ai/cli/session) |
|
||||
| 📄 Project Docs (.praison/docs/) | [Example](#docs-cli) | [📖](https://docs.praison.ai/docs/cli/docs) |
|
||||
| 🔌 MCP Config Management | [Example](#mcp-config-cli) | [📖](https://docs.praison.ai/docs/cli/mcp) |
|
||||
| 💬 AI Commit Messages | [Example](#ai-commit-cli) | [📖](https://docs.praison.ai/docs/cli/commit) |
|
||||
| @ @Mentions in Prompts | [Example](#mentions-in-prompts) | [📖](https://docs.praison.ai/docs/cli/mentions) |
|
||||
| 💾 Auto-Save Sessions | [Example](#session-management-python) | [📖](https://docs.praison.ai/docs/cli/session) |
|
||||
| 📜 History in Context | [Example](#session-management-python) | [📖](https://docs.praison.ai/docs/cli/session) |
|
||||
| **🖥️ CLI Features** | | |
|
||||
| ↳ Slash Commands | [Example](examples/python/cli/slash_commands_example.py) | [📖](https://docs.praison.ai/cli/slash-commands) |
|
||||
| ↳ Autonomy Modes | [Example](examples/python/cli/autonomy_modes_example.py) | [📖](https://docs.praison.ai/cli/autonomy-modes) |
|
||||
| ↳ Cost Tracking | [Example](examples/python/cli/cost_tracking_example.py) | [📖](https://docs.praison.ai/cli/cost-tracking) |
|
||||
| ↳ Repository Map | [Example](examples/python/cli/repo_map_example.py) | [📖](https://docs.praison.ai/cli/repo-map) |
|
||||
| ↳ Interactive TUI | [Example](examples/python/cli/interactive_tui_example.py) | [📖](https://docs.praison.ai/cli/interactive-tui) |
|
||||
| ↳ Git Integration | [Example](examples/python/cli/git_integration_example.py) | [📖](https://docs.praison.ai/cli/git-integration) |
|
||||
| ↳ Sandbox Execution | [Example](examples/python/cli/sandbox_execution_example.py) | [📖](https://docs.praison.ai/cli/sandbox-execution) |
|
||||
| ↳ Slash Commands | [Example](examples/python/cli/slash_commands_example.py) | [📖](https://docs.praison.ai/docs/cli/slash-commands) |
|
||||
| ↳ Autonomy Modes | [Example](examples/python/cli/autonomy_modes_example.py) | [📖](https://docs.praison.ai/docs/cli/autonomy-modes) |
|
||||
| ↳ Cost Tracking | [Example](examples/python/cli/cost_tracking_example.py) | [📖](https://docs.praison.ai/docs/cli/cost-tracking) |
|
||||
| ↳ Repository Map | [Example](examples/python/cli/repo_map_example.py) | [📖](https://docs.praison.ai/docs/cli/repo-map) |
|
||||
| ↳ Interactive TUI | [Example](examples/python/cli/interactive_tui_example.py) | [📖](https://docs.praison.ai/docs/cli/interactive-tui) |
|
||||
| ↳ Git Integration | [Example](examples/python/cli/git_integration_example.py) | [📖](https://docs.praison.ai/docs/cli/git-integration) |
|
||||
| ↳ Sandbox Execution | [Example](examples/python/cli/sandbox_execution_example.py) | [📖](https://docs.praison.ai/docs/cli/sandbox-execution) |
|
||||
|
||||
---
|
||||
|
||||
@@ -972,6 +972,32 @@ export OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxx
|
||||
praisonai --auto create a movie script about Robots in Mars
|
||||
```
|
||||
|
||||
### Interactive Mode CLI:
|
||||
```bash
|
||||
# Start interactive terminal mode (inspired by Gemini CLI, Codex CLI, Claude Code)
|
||||
praisonai --interactive
|
||||
praisonai -i
|
||||
|
||||
# Features:
|
||||
# - Streaming responses (no boxes)
|
||||
# - Built-in tools: read_file, write_file, list_files, execute_command, internet_search
|
||||
# - Slash commands: /help, /exit, /tools, /clear
|
||||
|
||||
# Chat mode - single prompt with interactive style (for testing/scripting)
|
||||
# Use --chat (or --chat-mode for backward compatibility)
|
||||
praisonai "list files in current folder" --chat
|
||||
praisonai "search the web for AI news" --chat
|
||||
praisonai "read README.md" --chat
|
||||
```
|
||||
|
||||
### Chat UI (Web Interface):
|
||||
```bash
|
||||
# Start web-based Chainlit chat interface (requires praisonai[chat])
|
||||
pip install "praisonai[chat]"
|
||||
praisonai chat
|
||||
# Opens browser at http://localhost:8084
|
||||
```
|
||||
|
||||
### Query Rewriting (works with any command):
|
||||
```bash
|
||||
# Rewrite query for better results (uses QueryRewriterAgent)
|
||||
@@ -1032,6 +1058,20 @@ praisonai "Complex analysis task" --planning --planning-reasoning
|
||||
praisonai "Task" --planning --auto-approve-plan
|
||||
```
|
||||
|
||||
### Tool Approval CLI:
|
||||
```bash
|
||||
# Auto-approve ALL tool executions (use with caution!)
|
||||
praisonai "run ls command" --trust
|
||||
|
||||
# Auto-approve tools up to a risk level (prompt for higher)
|
||||
# Levels: low, medium, high, critical
|
||||
praisonai "write to file" --approve-level high # Prompts for critical tools only
|
||||
praisonai "task" --approve-level medium # Prompts for high and critical
|
||||
|
||||
# Default behavior (no flags): prompts for all dangerous tools
|
||||
praisonai "run shell command" # Will prompt for approval
|
||||
```
|
||||
|
||||
### Memory CLI:
|
||||
```bash
|
||||
# Enable memory for agent (persists across sessions)
|
||||
@@ -1446,13 +1486,31 @@ praisonai mcp help
|
||||
|
||||
### AI Commit CLI:
|
||||
```bash
|
||||
# Generate AI commit message for staged changes
|
||||
# Full auto mode: stage all, security check, commit, and push
|
||||
praisonai commit -a
|
||||
|
||||
# Interactive mode (requires git add first)
|
||||
praisonai commit
|
||||
|
||||
# Generate, commit, and push
|
||||
# Interactive with auto-push
|
||||
praisonai commit --push
|
||||
|
||||
# Skip security check (not recommended)
|
||||
praisonai commit -a --no-verify
|
||||
```
|
||||
|
||||
**Features:**
|
||||
- 🤖 AI-generated conventional commit messages
|
||||
- 🔒 Built-in security scanning (API keys, passwords, secrets, sensitive files)
|
||||
- 📦 Auto-staging with `-a` flag
|
||||
- 🚀 Auto-push in full auto mode
|
||||
- ✏️ Edit message before commit in interactive mode
|
||||
|
||||
**Security Detection:**
|
||||
- API keys, secrets, tokens (AWS, GitHub, GitLab, Slack)
|
||||
- Passwords and private keys
|
||||
- Sensitive files (`.env`, `id_rsa`, `.pem`, `.key`, etc.)
|
||||
|
||||
### Serve CLI (API Server):
|
||||
```bash
|
||||
# Start API server for agents defined in YAML
|
||||
@@ -1757,32 +1815,32 @@ app.include_router(a2a.get_router())
|
||||
|
||||
| Feature | Docs |
|
||||
|---------|:----:|
|
||||
| 🔄 Query Rewrite - RAG optimization | [📖](https://docs.praison.ai/cli/query-rewrite) |
|
||||
| 🔬 Deep Research - Automated research | [📖](https://docs.praison.ai/cli/deep-research) |
|
||||
| 📋 Planning - Step-by-step execution | [📖](https://docs.praison.ai/cli/planning) |
|
||||
| 💾 Memory - Persistent agent memory | [📖](https://docs.praison.ai/cli/memory) |
|
||||
| 📜 Rules - Auto-discovered instructions | [📖](https://docs.praison.ai/cli/rules) |
|
||||
| 🔄 Workflow - Multi-step workflows | [📖](https://docs.praison.ai/cli/workflow) |
|
||||
| 🪝 Hooks - Event-driven actions | [📖](https://docs.praison.ai/cli/hooks) |
|
||||
| 🧠 Claude Memory - Anthropic memory tool | [📖](https://docs.praison.ai/cli/claude-memory) |
|
||||
| 🛡️ Guardrail - Output validation | [📖](https://docs.praison.ai/cli/guardrail) |
|
||||
| 📊 Metrics - Token usage tracking | [📖](https://docs.praison.ai/cli/metrics) |
|
||||
| 🖼️ Image - Vision processing | [📖](https://docs.praison.ai/cli/image) |
|
||||
| 📡 Telemetry - Usage monitoring | [📖](https://docs.praison.ai/cli/telemetry) |
|
||||
| 🔌 MCP - Model Context Protocol | [📖](https://docs.praison.ai/cli/mcp) |
|
||||
| ⚡ Fast Context - Codebase search | [📖](https://docs.praison.ai/cli/fast-context) |
|
||||
| 📚 Knowledge - RAG management | [📖](https://docs.praison.ai/cli/knowledge) |
|
||||
| 💬 Session - Conversation management | [📖](https://docs.praison.ai/cli/session) |
|
||||
| 🔧 Tools - Tool discovery | [📖](https://docs.praison.ai/cli/tools) |
|
||||
| 🤝 Handoff - Agent delegation | [📖](https://docs.praison.ai/cli/handoff) |
|
||||
| 🧠 Auto Memory - Memory extraction | [📖](https://docs.praison.ai/cli/auto-memory) |
|
||||
| 📋 Todo - Task management | [📖](https://docs.praison.ai/cli/todo) |
|
||||
| 🎯 Router - Smart model selection | [📖](https://docs.praison.ai/cli/router) |
|
||||
| 📈 Flow Display - Visual workflow | [📖](https://docs.praison.ai/cli/flow-display) |
|
||||
| ✨ Prompt Expansion - Detailed prompts | [📖](https://docs.praison.ai/cli/prompt-expansion) |
|
||||
| 🌐 Web Search - Real-time search | [📖](https://docs.praison.ai/cli/web-search) |
|
||||
| 📥 Web Fetch - URL content retrieval | [📖](https://docs.praison.ai/cli/web-fetch) |
|
||||
| 💾 Prompt Caching - Cost reduction | [📖](https://docs.praison.ai/cli/prompt-caching) |
|
||||
| 🔄 Query Rewrite - RAG optimization | [📖](https://docs.praison.ai/docs/cli/query-rewrite) |
|
||||
| 🔬 Deep Research - Automated research | [📖](https://docs.praison.ai/docs/cli/deep-research) |
|
||||
| 📋 Planning - Step-by-step execution | [📖](https://docs.praison.ai/docs/cli/planning) |
|
||||
| 💾 Memory - Persistent agent memory | [📖](https://docs.praison.ai/docs/cli/memory) |
|
||||
| 📜 Rules - Auto-discovered instructions | [📖](https://docs.praison.ai/docs/cli/rules) |
|
||||
| 🔄 Workflow - Multi-step workflows | [📖](https://docs.praison.ai/docs/cli/workflow) |
|
||||
| 🪝 Hooks - Event-driven actions | [📖](https://docs.praison.ai/docs/cli/hooks) |
|
||||
| 🧠 Claude Memory - Anthropic memory tool | [📖](https://docs.praison.ai/docs/cli/claude-memory) |
|
||||
| 🛡️ Guardrail - Output validation | [📖](https://docs.praison.ai/docs/cli/guardrail) |
|
||||
| 📊 Metrics - Token usage tracking | [📖](https://docs.praison.ai/docs/cli/metrics) |
|
||||
| 🖼️ Image - Vision processing | [📖](https://docs.praison.ai/docs/cli/image) |
|
||||
| 📡 Telemetry - Usage monitoring | [📖](https://docs.praison.ai/docs/cli/telemetry) |
|
||||
| 🔌 MCP - Model Context Protocol | [📖](https://docs.praison.ai/docs/cli/mcp) |
|
||||
| ⚡ Fast Context - Codebase search | [📖](https://docs.praison.ai/docs/cli/fast-context) |
|
||||
| 📚 Knowledge - RAG management | [📖](https://docs.praison.ai/docs/cli/knowledge) |
|
||||
| 💬 Session - Conversation management | [📖](https://docs.praison.ai/docs/cli/session) |
|
||||
| 🔧 Tools - Tool discovery | [📖](https://docs.praison.ai/docs/cli/tools) |
|
||||
| 🤝 Handoff - Agent delegation | [📖](https://docs.praison.ai/docs/cli/handoff) |
|
||||
| 🧠 Auto Memory - Memory extraction | [📖](https://docs.praison.ai/docs/cli/auto-memory) |
|
||||
| 📋 Todo - Task management | [📖](https://docs.praison.ai/docs/cli/todo) |
|
||||
| 🎯 Router - Smart model selection | [📖](https://docs.praison.ai/docs/cli/router) |
|
||||
| 📈 Flow Display - Visual workflow | [📖](https://docs.praison.ai/docs/cli/flow-display) |
|
||||
| ✨ Prompt Expansion - Detailed prompts | [📖](https://docs.praison.ai/docs/cli/prompt-expansion) |
|
||||
| 🌐 Web Search - Real-time search | [📖](https://docs.praison.ai/docs/cli/web-search) |
|
||||
| 📥 Web Fetch - URL content retrieval | [📖](https://docs.praison.ai/docs/cli/web-fetch) |
|
||||
| 💾 Prompt Caching - Cost reduction | [📖](https://docs.praison.ai/docs/cli/prompt-caching) |
|
||||
|
||||
---
|
||||
|
||||
@@ -2355,6 +2413,7 @@ PraisonAI provides zero-dependency persistent memory for agents. For detailed ex
|
||||
- 🤝 **Agent Handoffs** - Transfer context between specialised agents
|
||||
- 🛡️ **Guardrails** - Input/output validation and safety checks
|
||||
- ✅ **Human Approval** - Require human confirmation for critical actions
|
||||
- 🔐 **Tool Approval CLI** - `--trust` (auto-approve all) and `--approve-level` (risk-based approval)
|
||||
- 💬 **Sessions Management** - Isolated conversation contexts
|
||||
- 🔄 **Stateful Agents** - Maintain state across interactions
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# PraisonAI CLI - Benchmark Results
|
||||
|
||||
**Generated:** 2025-12-19 16:04:40
|
||||
|
||||
## Import Times
|
||||
|
||||
| Module | Time (ms) | Target | Status |
|
||||
|--------|-----------|--------|--------|
|
||||
| praisonai | 4.11 | <10 | ✅ |
|
||||
| message_queue | 6.87 | <50 | ✅ |
|
||||
| at_mentions | 0.58 | <50 | ✅ |
|
||||
| profiler | 0.90 | <10 | ✅ |
|
||||
|
||||
## Instantiation Times
|
||||
|
||||
| Class | Time (μs) | Target | Status |
|
||||
|-------|-----------|--------|--------|
|
||||
| MessageQueue | 0.26 | <100 | ✅ |
|
||||
| StateManager | 0.30 | <100 | ✅ |
|
||||
| FileSearchService | 8.42 | <100 | ✅ |
|
||||
|
||||
## Lazy Loading
|
||||
|
||||
| Operation | Time (ms) |
|
||||
|-----------|----------|
|
||||
| PraisonAI lazy load | 3886.47 |
|
||||
|
||||
## How to Reproduce
|
||||
|
||||
```bash
|
||||
cd praisonai
|
||||
python benchmarks/simple_benchmark.py
|
||||
```
|
||||
@@ -0,0 +1,231 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
PraisonAI CLI - Performance Benchmark
|
||||
|
||||
Measures import times and module loading performance.
|
||||
|
||||
Usage:
|
||||
python benchmarks/simple_benchmark.py
|
||||
python benchmarks/simple_benchmark.py --no-save
|
||||
"""
|
||||
|
||||
import time
|
||||
import sys
|
||||
import os
|
||||
import argparse
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
ITERATIONS = 100
|
||||
|
||||
|
||||
def measure_import(module_name: str) -> float:
|
||||
"""Measure import time in milliseconds."""
|
||||
# Clear from cache if present
|
||||
modules_to_remove = [k for k in sys.modules.keys() if k.startswith(module_name)]
|
||||
for mod in modules_to_remove:
|
||||
del sys.modules[mod]
|
||||
|
||||
start = time.perf_counter()
|
||||
__import__(module_name)
|
||||
return (time.perf_counter() - start) * 1000
|
||||
|
||||
|
||||
def measure_instantiation(create_fn, iterations=ITERATIONS) -> float:
|
||||
"""Measure average instantiation time in microseconds."""
|
||||
times = []
|
||||
for _ in range(iterations):
|
||||
start = time.perf_counter()
|
||||
create_fn()
|
||||
times.append((time.perf_counter() - start) * 1_000_000)
|
||||
return sum(times) / len(times)
|
||||
|
||||
|
||||
def run_benchmark():
|
||||
"""Run the benchmark."""
|
||||
results = {}
|
||||
|
||||
print('=' * 60)
|
||||
print('PraisonAI CLI - Performance Benchmark')
|
||||
print('=' * 60)
|
||||
print(f'\nIterations: {ITERATIONS}')
|
||||
print('Metrics: Import time (ms), Instantiation time (μs)\n')
|
||||
|
||||
# 1. Package import time
|
||||
print("1. Testing package imports...")
|
||||
|
||||
# Clear cache
|
||||
modules_to_remove = [k for k in sys.modules.keys() if 'praisonai' in k]
|
||||
for mod in modules_to_remove:
|
||||
del sys.modules[mod]
|
||||
|
||||
start = time.perf_counter()
|
||||
import praisonai
|
||||
results['praisonai_import_ms'] = (time.perf_counter() - start) * 1000
|
||||
print(f" import praisonai: {results['praisonai_import_ms']:.2f}ms")
|
||||
|
||||
# 2. CLI features import
|
||||
print("\n2. Testing CLI features imports...")
|
||||
|
||||
start = time.perf_counter()
|
||||
from praisonai.cli.features.message_queue import MessageQueue
|
||||
results['message_queue_import_ms'] = (time.perf_counter() - start) * 1000
|
||||
print(f" message_queue: {results['message_queue_import_ms']:.2f}ms")
|
||||
|
||||
start = time.perf_counter()
|
||||
from praisonai.cli.features.at_mentions import CombinedCompleter
|
||||
results['at_mentions_import_ms'] = (time.perf_counter() - start) * 1000
|
||||
print(f" at_mentions: {results['at_mentions_import_ms']:.2f}ms")
|
||||
|
||||
# 3. Profiler import (should be fast, standalone)
|
||||
print("\n3. Testing profiler import...")
|
||||
start = time.perf_counter()
|
||||
from praisonai.profiler import Profiler
|
||||
results['profiler_import_ms'] = (time.perf_counter() - start) * 1000
|
||||
print(f" profiler: {results['profiler_import_ms']:.2f}ms")
|
||||
|
||||
# 4. Instantiation benchmarks
|
||||
print(f"\n4. Testing instantiation ({ITERATIONS} iterations)...")
|
||||
|
||||
from praisonai.cli.features.message_queue import MessageQueue, StateManager
|
||||
from praisonai.cli.features.at_mentions import FileSearchService
|
||||
|
||||
results['MessageQueue_us'] = measure_instantiation(lambda: MessageQueue())
|
||||
print(f" MessageQueue: {results['MessageQueue_us']:.2f}μs")
|
||||
|
||||
results['StateManager_us'] = measure_instantiation(lambda: StateManager())
|
||||
print(f" StateManager: {results['StateManager_us']:.2f}μs")
|
||||
|
||||
results['FileSearchService_us'] = measure_instantiation(
|
||||
lambda: FileSearchService(root_dir=".")
|
||||
)
|
||||
print(f" FileSearchService: {results['FileSearchService_us']:.2f}μs")
|
||||
|
||||
# 5. PraisonAI class (lazy load)
|
||||
print("\n5. Testing PraisonAI class access (lazy load)...")
|
||||
start = time.perf_counter()
|
||||
PraisonAI = praisonai.PraisonAI
|
||||
results['PraisonAI_lazy_ms'] = (time.perf_counter() - start) * 1000
|
||||
print(f" PraisonAI lazy load: {results['PraisonAI_lazy_ms']:.2f}ms")
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def print_summary(results: dict):
|
||||
"""Print benchmark summary."""
|
||||
print('\n' + '=' * 60)
|
||||
print('SUMMARY')
|
||||
print('=' * 60)
|
||||
|
||||
print('\nImport Times (target: <50ms for features, <10ms for package):')
|
||||
print(f" {'Module':<25} {'Time':>10} {'Status':>10}")
|
||||
print(f" {'-'*25} {'-'*10} {'-'*10}")
|
||||
|
||||
import_checks = [
|
||||
('praisonai', results['praisonai_import_ms'], 10),
|
||||
('message_queue', results['message_queue_import_ms'], 50),
|
||||
('at_mentions', results['at_mentions_import_ms'], 50),
|
||||
('profiler', results['profiler_import_ms'], 10),
|
||||
]
|
||||
|
||||
all_pass = True
|
||||
for name, time_ms, threshold in import_checks:
|
||||
status = '✅ PASS' if time_ms < threshold else '❌ SLOW'
|
||||
if time_ms >= threshold:
|
||||
all_pass = False
|
||||
print(f" {name:<25} {time_ms:>8.2f}ms {status:>10}")
|
||||
|
||||
print('\nInstantiation Times (target: <100μs):')
|
||||
print(f" {'Class':<25} {'Time':>10} {'Status':>10}")
|
||||
print(f" {'-'*25} {'-'*10} {'-'*10}")
|
||||
|
||||
inst_checks = [
|
||||
('MessageQueue', results['MessageQueue_us'], 100),
|
||||
('StateManager', results['StateManager_us'], 100),
|
||||
('FileSearchService', results['FileSearchService_us'], 100),
|
||||
]
|
||||
|
||||
for name, time_us, threshold in inst_checks:
|
||||
status = '✅ PASS' if time_us < threshold else '❌ SLOW'
|
||||
if time_us >= threshold:
|
||||
all_pass = False
|
||||
print(f" {name:<25} {time_us:>8.2f}μs {status:>10}")
|
||||
|
||||
print('\n' + '=' * 60)
|
||||
if all_pass:
|
||||
print('✅ All benchmarks PASSED')
|
||||
else:
|
||||
print('❌ Some benchmarks FAILED')
|
||||
print('=' * 60)
|
||||
|
||||
return all_pass
|
||||
|
||||
|
||||
def save_results(results: dict):
|
||||
"""Save benchmark results to markdown file."""
|
||||
filepath = os.path.join(os.path.dirname(__file__), 'BENCHMARK_RESULTS.md')
|
||||
|
||||
with open(filepath, 'w') as f:
|
||||
f.write('# PraisonAI CLI - Benchmark Results\n\n')
|
||||
f.write(f'**Generated:** {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\n\n')
|
||||
|
||||
f.write('## Import Times\n\n')
|
||||
f.write('| Module | Time (ms) | Target | Status |\n')
|
||||
f.write('|--------|-----------|--------|--------|\n')
|
||||
|
||||
import_data = [
|
||||
('praisonai', results['praisonai_import_ms'], 10),
|
||||
('message_queue', results['message_queue_import_ms'], 50),
|
||||
('at_mentions', results['at_mentions_import_ms'], 50),
|
||||
('profiler', results['profiler_import_ms'], 10),
|
||||
]
|
||||
|
||||
for name, time_ms, threshold in import_data:
|
||||
status = '✅' if time_ms < threshold else '❌'
|
||||
f.write(f'| {name} | {time_ms:.2f} | <{threshold} | {status} |\n')
|
||||
|
||||
f.write('\n## Instantiation Times\n\n')
|
||||
f.write('| Class | Time (μs) | Target | Status |\n')
|
||||
f.write('|-------|-----------|--------|--------|\n')
|
||||
|
||||
inst_data = [
|
||||
('MessageQueue', results['MessageQueue_us'], 100),
|
||||
('StateManager', results['StateManager_us'], 100),
|
||||
('FileSearchService', results['FileSearchService_us'], 100),
|
||||
]
|
||||
|
||||
for name, time_us, threshold in inst_data:
|
||||
status = '✅' if time_us < threshold else '❌'
|
||||
f.write(f'| {name} | {time_us:.2f} | <{threshold} | {status} |\n')
|
||||
|
||||
f.write('\n## Lazy Loading\n\n')
|
||||
f.write(f'| Operation | Time (ms) |\n')
|
||||
f.write(f'|-----------|----------|\n')
|
||||
f.write(f'| PraisonAI lazy load | {results["PraisonAI_lazy_ms"]:.2f} |\n')
|
||||
|
||||
f.write('\n## How to Reproduce\n\n')
|
||||
f.write('```bash\n')
|
||||
f.write('cd praisonai\n')
|
||||
f.write('python benchmarks/simple_benchmark.py\n')
|
||||
f.write('```\n')
|
||||
|
||||
print(f'\nResults saved to: {filepath}')
|
||||
return filepath
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='PraisonAI CLI Performance Benchmark')
|
||||
parser.add_argument('--no-save', action='store_true', help='Do not save results to file')
|
||||
args = parser.parse_args()
|
||||
|
||||
results = run_benchmark()
|
||||
all_pass = print_summary(results)
|
||||
|
||||
if not args.no_save:
|
||||
save_results(results)
|
||||
|
||||
return 0 if all_pass else 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
@@ -3,8 +3,8 @@ class Praisonai < Formula
|
||||
|
||||
desc "AI tools for various AI applications"
|
||||
homepage "https://github.com/MervinPraison/PraisonAI"
|
||||
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.3.48.tar.gz"
|
||||
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.3.48.tar.gz | shasum -a 256`.split.first
|
||||
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.3.63.tar.gz"
|
||||
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.3.63.tar.gz | shasum -a 256`.split.first
|
||||
license "MIT"
|
||||
|
||||
depends_on "[email protected]"
|
||||
|
||||
@@ -6,30 +6,29 @@ logging.getLogger('crewai.cli.config').setLevel(logging.ERROR)
|
||||
import os
|
||||
os.environ["OTEL_SDK_DISABLED"] = "true"
|
||||
os.environ["EC_TELEMETRY"] = "false"
|
||||
from .cli import PraisonAI
|
||||
|
||||
# Version is lightweight, import directly
|
||||
from .version import __version__
|
||||
|
||||
# Re-export all classes from praisonaiagents to enable:
|
||||
# from praisonai import Agent, Task, PraisonAIAgents
|
||||
try:
|
||||
import praisonaiagents
|
||||
# Import all symbols from praisonaiagents using * import
|
||||
from praisonaiagents import *
|
||||
except ImportError:
|
||||
# If praisonaiagents is not available, these imports will fail gracefully
|
||||
pass
|
||||
|
||||
# Define __all__ to include both PraisonAI core classes and praisonaiagents exports
|
||||
# Define __all__ for lazy loading
|
||||
__all__ = [
|
||||
# Core PraisonAI classes
|
||||
'PraisonAI',
|
||||
'__version__',
|
||||
]
|
||||
|
||||
# Dynamically extend __all__ with praisonaiagents exports
|
||||
try:
|
||||
import praisonaiagents
|
||||
__all__.extend(praisonaiagents.__all__)
|
||||
except (ImportError, AttributeError):
|
||||
# If praisonaiagents is not available or doesn't have __all__, fail gracefully
|
||||
pass
|
||||
# Lazy loading for heavy imports
|
||||
def __getattr__(name):
|
||||
"""Lazy load heavy modules to improve import time."""
|
||||
if name == 'PraisonAI':
|
||||
from .cli import PraisonAI
|
||||
return PraisonAI
|
||||
|
||||
# Try praisonaiagents exports
|
||||
try:
|
||||
import praisonaiagents
|
||||
if hasattr(praisonaiagents, name):
|
||||
return getattr(praisonaiagents, name)
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
@@ -8,12 +8,19 @@ Structure:
|
||||
- features/: Feature handlers for CLI flags and commands
|
||||
"""
|
||||
|
||||
from .main import PraisonAI
|
||||
|
||||
__all__ = ["PraisonAI"]
|
||||
|
||||
|
||||
def __getattr__(name):
|
||||
"""Lazy load PraisonAI to avoid slow imports."""
|
||||
if name == "PraisonAI":
|
||||
from .main import PraisonAI
|
||||
return PraisonAI
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
|
||||
def main():
|
||||
"""CLI entry point function."""
|
||||
from .main import PraisonAI
|
||||
praison_ai = PraisonAI()
|
||||
praison_ai.main()
|
||||
@@ -25,6 +25,8 @@ Features:
|
||||
- slash_commands: Interactive slash commands (/help, /cost, /plan, etc.)
|
||||
- autonomy_mode: Autonomy levels (suggest, auto-edit, full-auto)
|
||||
- cost_tracker: Real-time cost and token tracking
|
||||
- message_queue: Message queuing while agent is processing
|
||||
- at_mentions: @ mention autocomplete for files and directories
|
||||
"""
|
||||
|
||||
# Type hints available for IDE support
|
||||
@@ -56,6 +58,11 @@ __all__ = [
|
||||
'InteractiveTUIHandler',
|
||||
'GitIntegrationHandler',
|
||||
'SandboxExecutorHandler',
|
||||
'MessageQueueHandler',
|
||||
# @ mention autocomplete
|
||||
'AtMentionCompleter',
|
||||
'CombinedCompleter',
|
||||
'FileSearchService',
|
||||
]
|
||||
|
||||
def __getattr__(name):
|
||||
@@ -133,4 +140,17 @@ def __getattr__(name):
|
||||
elif name == 'SandboxExecutorHandler':
|
||||
from .sandbox_executor import SandboxExecutorHandler
|
||||
return SandboxExecutorHandler
|
||||
elif name == 'MessageQueueHandler':
|
||||
from .message_queue import MessageQueueHandler
|
||||
return MessageQueueHandler
|
||||
# @ mention autocomplete
|
||||
elif name == 'AtMentionCompleter':
|
||||
from .at_mentions import AtMentionCompleter
|
||||
return AtMentionCompleter
|
||||
elif name == 'CombinedCompleter':
|
||||
from .at_mentions import CombinedCompleter
|
||||
return CombinedCompleter
|
||||
elif name == 'FileSearchService':
|
||||
from .at_mentions import FileSearchService
|
||||
return FileSearchService
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
@@ -0,0 +1,416 @@
|
||||
"""
|
||||
@ Mention Autocomplete for PraisonAI CLI.
|
||||
|
||||
Provides file/directory autocomplete when user types @.
|
||||
Uses only stdlib imports for zero performance impact.
|
||||
|
||||
Features:
|
||||
- Detect @ trigger in input
|
||||
- Fuzzy search files/directories
|
||||
- Cache results for performance
|
||||
- Integrate with prompt_toolkit
|
||||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
import fnmatch
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Optional, Dict, Tuple
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Data Classes
|
||||
# ============================================================================
|
||||
|
||||
@dataclass
|
||||
class AtMentionContext:
|
||||
"""Context for an @ mention in user input."""
|
||||
is_active: bool
|
||||
query: str
|
||||
start_pos: int
|
||||
mention_type: str = "file" # file, directory, web, etc.
|
||||
|
||||
|
||||
@dataclass
|
||||
class FileSuggestion:
|
||||
"""A file or directory suggestion."""
|
||||
path: str
|
||||
file_type: str # "file" or "directory"
|
||||
score: int = 100
|
||||
display: str = ""
|
||||
|
||||
def __post_init__(self):
|
||||
if not self.display:
|
||||
self.display = self.path
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# @ Mention Detection
|
||||
# ============================================================================
|
||||
|
||||
def detect_at_mention(text: str, cursor_pos: int) -> Optional[AtMentionContext]:
|
||||
"""
|
||||
Detect if cursor is within an @ mention context.
|
||||
|
||||
Scans backward from cursor to find @ symbol.
|
||||
Returns None if not in @ context.
|
||||
|
||||
Args:
|
||||
text: Full input text
|
||||
cursor_pos: Current cursor position
|
||||
|
||||
Returns:
|
||||
AtMentionContext if in @ mention, None otherwise
|
||||
"""
|
||||
if cursor_pos <= 0 or cursor_pos > len(text):
|
||||
return None
|
||||
|
||||
# Scan backward from cursor to find @
|
||||
at_pos = -1
|
||||
for i in range(cursor_pos - 1, -1, -1):
|
||||
char = text[i]
|
||||
|
||||
# Found @
|
||||
if char == '@':
|
||||
at_pos = i
|
||||
break
|
||||
|
||||
# Space/newline breaks @ context
|
||||
if char in ' \t\n\r':
|
||||
return None
|
||||
|
||||
if at_pos == -1:
|
||||
return None
|
||||
|
||||
# Extract query between @ and cursor
|
||||
query = text[at_pos + 1:cursor_pos]
|
||||
|
||||
return AtMentionContext(
|
||||
is_active=True,
|
||||
query=query,
|
||||
start_pos=at_pos
|
||||
)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# File Search Service
|
||||
# ============================================================================
|
||||
|
||||
class FileSearchService:
|
||||
"""
|
||||
Service for searching files in a directory.
|
||||
|
||||
Features:
|
||||
- Walk directory tree
|
||||
- Fuzzy match file paths
|
||||
- Cache results with TTL
|
||||
- Respect .gitignore (basic)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root_dir: str,
|
||||
cache_ttl: int = 30,
|
||||
max_depth: int = 5,
|
||||
ignore_patterns: Optional[List[str]] = None
|
||||
):
|
||||
self._root = os.path.abspath(root_dir)
|
||||
self._cache_ttl = cache_ttl
|
||||
self._max_depth = max_depth
|
||||
self._ignore_patterns = ignore_patterns or [
|
||||
'.git', '__pycache__', 'node_modules', '.venv', 'venv',
|
||||
'*.pyc', '*.pyo', '.DS_Store', '*.egg-info'
|
||||
]
|
||||
|
||||
# Cache: query -> (results, timestamp)
|
||||
self._cache: Dict[str, Tuple[List[FileSuggestion], float]] = {}
|
||||
|
||||
# File list cache
|
||||
self._file_list: Optional[List[str]] = None
|
||||
self._file_list_time: float = 0
|
||||
|
||||
def _should_ignore(self, name: str) -> bool:
|
||||
"""Check if file/dir should be ignored."""
|
||||
for pattern in self._ignore_patterns:
|
||||
if fnmatch.fnmatch(name, pattern):
|
||||
return True
|
||||
return False
|
||||
|
||||
def _get_file_list(self) -> List[str]:
|
||||
"""Get list of all files (cached)."""
|
||||
now = time.time()
|
||||
|
||||
# Return cached if fresh
|
||||
if self._file_list is not None and (now - self._file_list_time) < self._cache_ttl:
|
||||
return self._file_list
|
||||
|
||||
files = []
|
||||
|
||||
try:
|
||||
for root, dirs, filenames in os.walk(self._root):
|
||||
# Calculate depth
|
||||
rel_root = os.path.relpath(root, self._root)
|
||||
depth = 0 if rel_root == '.' else rel_root.count(os.sep) + 1
|
||||
|
||||
if depth > self._max_depth:
|
||||
dirs.clear() # Don't descend further
|
||||
continue
|
||||
|
||||
# Filter ignored directories
|
||||
dirs[:] = [d for d in dirs if not self._should_ignore(d)]
|
||||
|
||||
# Add directories
|
||||
for d in dirs:
|
||||
rel_path = os.path.relpath(os.path.join(root, d), self._root)
|
||||
files.append(rel_path + os.sep)
|
||||
|
||||
# Add files
|
||||
for f in filenames:
|
||||
if not self._should_ignore(f):
|
||||
rel_path = os.path.relpath(os.path.join(root, f), self._root)
|
||||
files.append(rel_path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
self._file_list = files
|
||||
self._file_list_time = now
|
||||
return files
|
||||
|
||||
def _fuzzy_match(self, text: str, query: str) -> int:
|
||||
"""
|
||||
Simple fuzzy match scoring.
|
||||
|
||||
Returns score 0-100, higher is better.
|
||||
"""
|
||||
if not query:
|
||||
return 50 # Default score for empty query
|
||||
|
||||
text_lower = text.lower()
|
||||
query_lower = query.lower()
|
||||
|
||||
# Exact match
|
||||
if text_lower == query_lower:
|
||||
return 100
|
||||
|
||||
# Starts with
|
||||
if text_lower.startswith(query_lower):
|
||||
return 90
|
||||
|
||||
# Contains
|
||||
if query_lower in text_lower:
|
||||
return 80
|
||||
|
||||
# Fuzzy: all query chars appear in order
|
||||
text_idx = 0
|
||||
query_idx = 0
|
||||
matches = 0
|
||||
|
||||
while text_idx < len(text_lower) and query_idx < len(query_lower):
|
||||
if text_lower[text_idx] == query_lower[query_idx]:
|
||||
matches += 1
|
||||
query_idx += 1
|
||||
text_idx += 1
|
||||
|
||||
if query_idx == len(query_lower):
|
||||
# All query chars found
|
||||
return 50 + int(30 * matches / len(text))
|
||||
|
||||
return 0
|
||||
|
||||
def search(self, query: str, max_results: int = 20) -> List[FileSuggestion]:
|
||||
"""
|
||||
Search for files matching query.
|
||||
|
||||
Args:
|
||||
query: Search query (fuzzy matched)
|
||||
max_results: Maximum results to return
|
||||
|
||||
Returns:
|
||||
List of FileSuggestion sorted by score
|
||||
"""
|
||||
# Check cache
|
||||
cache_key = f"{query}:{max_results}"
|
||||
now = time.time()
|
||||
|
||||
if cache_key in self._cache:
|
||||
results, cached_time = self._cache[cache_key]
|
||||
if (now - cached_time) < self._cache_ttl:
|
||||
return results
|
||||
|
||||
# Get all files
|
||||
all_files = self._get_file_list()
|
||||
|
||||
# Score and filter
|
||||
scored: List[Tuple[int, str]] = []
|
||||
for path in all_files:
|
||||
score = self._fuzzy_match(path, query)
|
||||
if score > 0:
|
||||
scored.append((score, path))
|
||||
|
||||
# Sort by score descending
|
||||
scored.sort(key=lambda x: (-x[0], x[1]))
|
||||
|
||||
# Build results
|
||||
results = []
|
||||
for score, path in scored[:max_results]:
|
||||
file_type = "directory" if path.endswith(os.sep) else "file"
|
||||
results.append(FileSuggestion(
|
||||
path=path,
|
||||
file_type=file_type,
|
||||
score=score
|
||||
))
|
||||
|
||||
# Cache results
|
||||
self._cache[cache_key] = (results, now)
|
||||
|
||||
return results
|
||||
|
||||
def clear_cache(self) -> None:
|
||||
"""Clear all caches."""
|
||||
self._cache.clear()
|
||||
self._file_list = None
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Completers for prompt_toolkit
|
||||
# ============================================================================
|
||||
|
||||
# Lazy import to avoid performance impact
|
||||
_Completer = None
|
||||
_Completion = None
|
||||
|
||||
def _get_prompt_toolkit():
|
||||
"""Lazy import prompt_toolkit."""
|
||||
global _Completer, _Completion
|
||||
if _Completer is None:
|
||||
from prompt_toolkit.completion import Completer, Completion
|
||||
_Completer = Completer
|
||||
_Completion = Completion
|
||||
return _Completer, _Completion
|
||||
|
||||
|
||||
class AtMentionCompleter:
|
||||
"""
|
||||
Completer for @ mentions.
|
||||
|
||||
Shows file suggestions when user types @.
|
||||
Implements prompt_toolkit Completer interface.
|
||||
"""
|
||||
|
||||
def __init__(self, root_dir: str):
|
||||
self._file_service = FileSearchService(root_dir)
|
||||
|
||||
def get_completions(self, document, complete_event):
|
||||
"""Get completions for @ mentions."""
|
||||
_, Completion = _get_prompt_toolkit()
|
||||
|
||||
text = document.text_before_cursor
|
||||
cursor_pos = len(text)
|
||||
|
||||
# Detect @ context
|
||||
context = detect_at_mention(text, cursor_pos)
|
||||
if context is None or not context.is_active:
|
||||
return
|
||||
|
||||
# Search files
|
||||
results = self._file_service.search(context.query, max_results=15)
|
||||
|
||||
# Yield completions
|
||||
for suggestion in results:
|
||||
# Icon based on type
|
||||
icon = "📁 " if suggestion.file_type == "directory" else "📄 "
|
||||
display = f"{icon}{suggestion.path}"
|
||||
|
||||
# Calculate replacement position
|
||||
# Replace from @ to cursor
|
||||
start_position = -(cursor_pos - context.start_pos)
|
||||
|
||||
yield Completion(
|
||||
text=f"@{suggestion.path}",
|
||||
start_position=start_position,
|
||||
display=display
|
||||
)
|
||||
|
||||
|
||||
def create_combined_completer(commands: List[str], root_dir: str):
|
||||
"""
|
||||
Factory function to create a CombinedCompleter.
|
||||
|
||||
Returns a proper Completer subclass that handles both / and @.
|
||||
"""
|
||||
Completer, Completion = _get_prompt_toolkit()
|
||||
file_service = FileSearchService(root_dir)
|
||||
|
||||
class CombinedCompleter(Completer):
|
||||
"""
|
||||
Combined completer for both / commands and @ mentions.
|
||||
|
||||
Properly inherits from prompt_toolkit Completer.
|
||||
"""
|
||||
|
||||
def get_completions(self, document, complete_event):
|
||||
"""Get completions for / or @."""
|
||||
text = document.text_before_cursor.lstrip()
|
||||
|
||||
# Check for slash command
|
||||
if text.startswith('/'):
|
||||
cmd_text = text[1:].lower()
|
||||
for cmd in commands:
|
||||
if cmd.lower().startswith(cmd_text):
|
||||
yield Completion(
|
||||
f'/{cmd}',
|
||||
start_position=-len(text),
|
||||
display=f'/{cmd}'
|
||||
)
|
||||
return
|
||||
|
||||
# Check for @ mention
|
||||
full_text = document.text_before_cursor
|
||||
cursor_pos = len(full_text)
|
||||
|
||||
context = detect_at_mention(full_text, cursor_pos)
|
||||
if context is None or not context.is_active:
|
||||
return
|
||||
|
||||
# Search files
|
||||
results = file_service.search(context.query, max_results=15)
|
||||
|
||||
# Yield completions
|
||||
for suggestion in results:
|
||||
icon = "📁 " if suggestion.file_type == "directory" else "📄 "
|
||||
display = f"{icon}{suggestion.path}"
|
||||
start_position = -(cursor_pos - context.start_pos)
|
||||
|
||||
yield Completion(
|
||||
text=f"@{suggestion.path}",
|
||||
start_position=start_position,
|
||||
display=display
|
||||
)
|
||||
|
||||
return CombinedCompleter()
|
||||
|
||||
|
||||
# Backward compatible alias
|
||||
class CombinedCompleter:
|
||||
"""
|
||||
Wrapper class for backward compatibility.
|
||||
|
||||
Use create_combined_completer() for proper Completer subclass.
|
||||
"""
|
||||
|
||||
def __new__(cls, commands: List[str], root_dir: str):
|
||||
return create_combined_completer(commands, root_dir)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Exports
|
||||
# ============================================================================
|
||||
|
||||
__all__ = [
|
||||
'AtMentionContext',
|
||||
'FileSuggestion',
|
||||
'detect_at_mention',
|
||||
'FileSearchService',
|
||||
'AtMentionCompleter',
|
||||
'CombinedCompleter',
|
||||
]
|
||||
@@ -0,0 +1,587 @@
|
||||
"""
|
||||
Message Queue for Interactive Mode.
|
||||
|
||||
Provides FIFO message queuing while the AI agent is processing,
|
||||
inspired by Claude Code, Windsurf Cascade, Cursor, and Gemini CLI.
|
||||
|
||||
Features:
|
||||
- Queue messages while agent is processing
|
||||
- Auto-process queue when agent becomes idle
|
||||
- Visual indicators for queue status
|
||||
- Thread-safe operations
|
||||
"""
|
||||
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Callable, List, Optional
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Enums
|
||||
# ============================================================================
|
||||
|
||||
class ProcessingState(Enum):
|
||||
"""State of the agent processing."""
|
||||
IDLE = "idle"
|
||||
PROCESSING = "processing"
|
||||
WAITING_APPROVAL = "waiting_approval"
|
||||
|
||||
|
||||
class MessagePriority(Enum):
|
||||
"""Priority levels for queued messages."""
|
||||
LOW = "low"
|
||||
NORMAL = "normal"
|
||||
HIGH = "high"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Data Classes
|
||||
# ============================================================================
|
||||
|
||||
@dataclass
|
||||
class QueuedMessage:
|
||||
"""A message in the queue with metadata."""
|
||||
content: str
|
||||
timestamp: float = field(default_factory=time.time)
|
||||
priority: MessagePriority = MessagePriority.NORMAL
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# MessageQueue - Thread-safe FIFO queue
|
||||
# ============================================================================
|
||||
|
||||
class MessageQueue:
|
||||
"""
|
||||
Thread-safe FIFO message queue.
|
||||
|
||||
Provides basic queue operations with thread safety for concurrent access.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._messages: List[str] = []
|
||||
self._lock = threading.Lock()
|
||||
|
||||
@property
|
||||
def is_empty(self) -> bool:
|
||||
"""Check if queue is empty."""
|
||||
with self._lock:
|
||||
return len(self._messages) == 0
|
||||
|
||||
@property
|
||||
def count(self) -> int:
|
||||
"""Get number of messages in queue."""
|
||||
with self._lock:
|
||||
return len(self._messages)
|
||||
|
||||
def add(self, message: str) -> bool:
|
||||
"""
|
||||
Add a message to the end of the queue.
|
||||
|
||||
Args:
|
||||
message: The message to add
|
||||
|
||||
Returns:
|
||||
True if message was added, False if empty/whitespace
|
||||
"""
|
||||
trimmed = message.strip() if message else ""
|
||||
if not trimmed:
|
||||
return False
|
||||
|
||||
with self._lock:
|
||||
self._messages.append(trimmed)
|
||||
return True
|
||||
|
||||
def pop(self) -> Optional[str]:
|
||||
"""
|
||||
Remove and return the first message (FIFO).
|
||||
|
||||
Returns:
|
||||
The first message, or None if queue is empty
|
||||
"""
|
||||
with self._lock:
|
||||
if self._messages:
|
||||
return self._messages.pop(0)
|
||||
return None
|
||||
|
||||
def peek(self) -> Optional[str]:
|
||||
"""
|
||||
View the first message without removing it.
|
||||
|
||||
Returns:
|
||||
The first message, or None if queue is empty
|
||||
"""
|
||||
with self._lock:
|
||||
if self._messages:
|
||||
return self._messages[0]
|
||||
return None
|
||||
|
||||
def clear(self) -> None:
|
||||
"""Remove all messages from the queue."""
|
||||
with self._lock:
|
||||
self._messages.clear()
|
||||
|
||||
def get_all(self) -> List[str]:
|
||||
"""
|
||||
Get all messages as a list (does not remove them).
|
||||
|
||||
Returns:
|
||||
Copy of all messages in queue order
|
||||
"""
|
||||
with self._lock:
|
||||
return self._messages.copy()
|
||||
|
||||
def remove_at(self, index: int) -> Optional[str]:
|
||||
"""
|
||||
Remove message at specific index.
|
||||
|
||||
Args:
|
||||
index: The index to remove (0-based)
|
||||
|
||||
Returns:
|
||||
The removed message, or None if index is invalid
|
||||
"""
|
||||
with self._lock:
|
||||
if 0 <= index < len(self._messages):
|
||||
return self._messages.pop(index)
|
||||
return None
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# StateManager - Processing state management
|
||||
# ============================================================================
|
||||
|
||||
class StateManager:
|
||||
"""
|
||||
Manages the processing state with optional callbacks.
|
||||
|
||||
Tracks whether the agent is idle, processing, or waiting for approval.
|
||||
"""
|
||||
|
||||
def __init__(self, on_state_change: Optional[Callable[[ProcessingState, ProcessingState], None]] = None):
|
||||
self._state = ProcessingState.IDLE
|
||||
self._lock = threading.Lock()
|
||||
self._on_state_change = on_state_change
|
||||
|
||||
@property
|
||||
def current_state(self) -> ProcessingState:
|
||||
"""Get current processing state."""
|
||||
with self._lock:
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def is_idle(self) -> bool:
|
||||
"""Check if currently idle."""
|
||||
return self.current_state == ProcessingState.IDLE
|
||||
|
||||
@property
|
||||
def is_processing(self) -> bool:
|
||||
"""Check if currently processing."""
|
||||
return self.current_state == ProcessingState.PROCESSING
|
||||
|
||||
def set_state(self, new_state: ProcessingState) -> None:
|
||||
"""
|
||||
Set the processing state.
|
||||
|
||||
Args:
|
||||
new_state: The new state to set
|
||||
"""
|
||||
with self._lock:
|
||||
old_state = self._state
|
||||
self._state = new_state
|
||||
|
||||
# Call callback outside lock to prevent deadlock
|
||||
if self._on_state_change and old_state != new_state:
|
||||
self._on_state_change(old_state, new_state)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# QueueDisplay - Visual formatting
|
||||
# ============================================================================
|
||||
|
||||
class QueueDisplay:
|
||||
"""
|
||||
Formats queue and status for display.
|
||||
|
||||
Provides visual indicators for queue status, inspired by Codex CLI.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
queue: MessageQueue,
|
||||
state_manager: Optional[StateManager] = None,
|
||||
max_message_length: int = 50
|
||||
):
|
||||
self._queue = queue
|
||||
self._state_manager = state_manager
|
||||
self._max_length = max_message_length
|
||||
|
||||
def format_queue(self) -> str:
|
||||
"""
|
||||
Format queued messages for display.
|
||||
|
||||
Returns:
|
||||
Formatted string with queued messages
|
||||
"""
|
||||
messages = self._queue.get_all()
|
||||
if not messages:
|
||||
return ""
|
||||
|
||||
lines = []
|
||||
for msg in messages:
|
||||
# Truncate long messages
|
||||
display_msg = msg
|
||||
if len(msg) > self._max_length:
|
||||
display_msg = msg[:self._max_length - 3] + "..."
|
||||
lines.append(f" ↳ {display_msg}")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
def format_status(self) -> str:
|
||||
"""
|
||||
Format current processing status.
|
||||
|
||||
Returns:
|
||||
Status string (e.g., "⏳ Processing..." or "")
|
||||
"""
|
||||
if not self._state_manager:
|
||||
return ""
|
||||
|
||||
state = self._state_manager.current_state
|
||||
if state == ProcessingState.PROCESSING:
|
||||
return "⏳ Processing..."
|
||||
elif state == ProcessingState.WAITING_APPROVAL:
|
||||
return "🔒 Waiting for approval..."
|
||||
return ""
|
||||
|
||||
def format_queue_count(self) -> str:
|
||||
"""
|
||||
Format queue count indicator.
|
||||
|
||||
Returns:
|
||||
Count string (e.g., "📋 Queued (2)")
|
||||
"""
|
||||
count = self._queue.count
|
||||
if count == 0:
|
||||
return ""
|
||||
return f"📋 Queued ({count})"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# MessageQueueHandler - Main handler class
|
||||
# ============================================================================
|
||||
|
||||
class MessageQueueHandler:
|
||||
"""
|
||||
Main handler for message queue functionality.
|
||||
|
||||
Coordinates queue, state, and processing.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
processor: Optional[Callable[[str], str]] = None,
|
||||
on_response: Optional[Callable[[str], None]] = None
|
||||
):
|
||||
self._processor = processor
|
||||
self._on_response = on_response
|
||||
self._queue = MessageQueue()
|
||||
self._state_manager = StateManager(on_state_change=self._on_state_change)
|
||||
self._processing_lock = threading.Lock()
|
||||
|
||||
@property
|
||||
def queue(self) -> MessageQueue:
|
||||
"""Get the message queue."""
|
||||
return self._queue
|
||||
|
||||
@property
|
||||
def state_manager(self) -> StateManager:
|
||||
"""Get the state manager."""
|
||||
return self._state_manager
|
||||
|
||||
def submit(self, message: str) -> bool:
|
||||
"""
|
||||
Submit a message for processing.
|
||||
|
||||
If idle, processes immediately. If processing, queues the message.
|
||||
|
||||
Args:
|
||||
message: The message to submit
|
||||
|
||||
Returns:
|
||||
True if processed/queued, False if empty message
|
||||
"""
|
||||
trimmed = message.strip() if message else ""
|
||||
if not trimmed:
|
||||
return False
|
||||
|
||||
if self._state_manager.is_idle:
|
||||
# Process immediately
|
||||
self._process_message(trimmed)
|
||||
else:
|
||||
# Queue for later
|
||||
self._queue.add(trimmed)
|
||||
|
||||
return True
|
||||
|
||||
def on_processing_complete(self) -> None:
|
||||
"""
|
||||
Called when current processing completes.
|
||||
|
||||
Sets state to idle and processes next queued message if any.
|
||||
"""
|
||||
self._state_manager.set_state(ProcessingState.IDLE)
|
||||
self._process_next_in_queue()
|
||||
|
||||
def get_status(self) -> dict:
|
||||
"""
|
||||
Get current status.
|
||||
|
||||
Returns:
|
||||
Dict with queue_count and state
|
||||
"""
|
||||
return {
|
||||
'queue_count': self._queue.count,
|
||||
'state': self._state_manager.current_state.value,
|
||||
'messages': self._queue.get_all()
|
||||
}
|
||||
|
||||
def clear_queue(self) -> None:
|
||||
"""Clear all queued messages."""
|
||||
self._queue.clear()
|
||||
|
||||
def _process_message(self, message: str) -> None:
|
||||
"""Process a single message."""
|
||||
if not self._processor:
|
||||
return
|
||||
|
||||
with self._processing_lock:
|
||||
self._state_manager.set_state(ProcessingState.PROCESSING)
|
||||
try:
|
||||
response = self._processor(message)
|
||||
if self._on_response and response:
|
||||
self._on_response(response)
|
||||
finally:
|
||||
self._state_manager.set_state(ProcessingState.IDLE)
|
||||
|
||||
def _process_next_in_queue(self) -> None:
|
||||
"""Process the next message in queue if any."""
|
||||
next_msg = self._queue.pop()
|
||||
if next_msg:
|
||||
self._process_message(next_msg)
|
||||
|
||||
def _on_state_change(self, old_state: ProcessingState, new_state: ProcessingState) -> None:
|
||||
"""Handle state changes."""
|
||||
# Auto-process queue when becoming idle
|
||||
if new_state == ProcessingState.IDLE and old_state == ProcessingState.PROCESSING:
|
||||
# Don't auto-process here to avoid recursion
|
||||
# The on_processing_complete method handles this
|
||||
pass
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# AsyncProcessor - Background processing
|
||||
# ============================================================================
|
||||
|
||||
class AsyncProcessor:
|
||||
"""
|
||||
Runs work functions in background threads.
|
||||
|
||||
Allows agent processing to happen asynchronously while
|
||||
the main thread remains responsive for user input.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
on_complete: Optional[Callable[[str], None]] = None,
|
||||
on_status: Optional[Callable[[str], None]] = None,
|
||||
on_error: Optional[Callable[[Exception], None]] = None
|
||||
):
|
||||
self._on_complete = on_complete
|
||||
self._on_status = on_status
|
||||
self._on_error = on_error
|
||||
self._thread: Optional[threading.Thread] = None
|
||||
self._running = False
|
||||
self._lock = threading.Lock()
|
||||
|
||||
@property
|
||||
def is_running(self) -> bool:
|
||||
"""Check if currently processing."""
|
||||
with self._lock:
|
||||
return self._running
|
||||
|
||||
def start(self, work_fn: Callable[[], str]) -> None:
|
||||
"""
|
||||
Start processing work in background thread.
|
||||
|
||||
Args:
|
||||
work_fn: Function to execute (should return result string)
|
||||
"""
|
||||
def _worker():
|
||||
try:
|
||||
with self._lock:
|
||||
self._running = True
|
||||
|
||||
result = work_fn()
|
||||
|
||||
with self._lock:
|
||||
self._running = False
|
||||
|
||||
if self._on_complete:
|
||||
self._on_complete(result)
|
||||
except Exception as e:
|
||||
with self._lock:
|
||||
self._running = False
|
||||
if self._on_error:
|
||||
self._on_error(e)
|
||||
|
||||
self._thread = threading.Thread(target=_worker, daemon=True)
|
||||
self._thread.start()
|
||||
|
||||
def update_status(self, status: str) -> None:
|
||||
"""Update status (can be called from work function)."""
|
||||
if self._on_status:
|
||||
self._on_status(status)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# LiveStatusDisplay - Real-time status display
|
||||
# ============================================================================
|
||||
|
||||
class LiveStatusDisplay:
|
||||
"""
|
||||
Tracks and displays real-time status during processing.
|
||||
|
||||
Shows tool calls, command executions, and current status.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._status = ""
|
||||
self._tool_calls: List[dict] = []
|
||||
self._commands: List[str] = []
|
||||
self._lock = threading.Lock()
|
||||
|
||||
@property
|
||||
def current_status(self) -> str:
|
||||
"""Get current status message."""
|
||||
with self._lock:
|
||||
return self._status
|
||||
|
||||
@property
|
||||
def tool_calls(self) -> List[dict]:
|
||||
"""Get list of tool calls."""
|
||||
with self._lock:
|
||||
return self._tool_calls.copy()
|
||||
|
||||
@property
|
||||
def commands(self) -> List[str]:
|
||||
"""Get list of commands executed."""
|
||||
with self._lock:
|
||||
return self._commands.copy()
|
||||
|
||||
def update_status(self, status: str) -> None:
|
||||
"""Update current status message."""
|
||||
with self._lock:
|
||||
self._status = status
|
||||
|
||||
def add_tool_call(self, name: str, args: dict) -> None:
|
||||
"""Add a tool call to the display."""
|
||||
with self._lock:
|
||||
self._tool_calls.append({
|
||||
'name': name,
|
||||
'args': args,
|
||||
'timestamp': time.time()
|
||||
})
|
||||
|
||||
def add_command(self, command: str) -> None:
|
||||
"""Add a command execution to the display."""
|
||||
with self._lock:
|
||||
self._commands.append(command)
|
||||
|
||||
def format(self) -> str:
|
||||
"""Format current status for display."""
|
||||
lines = []
|
||||
|
||||
with self._lock:
|
||||
if self._status:
|
||||
lines.append(f"⏳ {self._status}")
|
||||
|
||||
for tool in self._tool_calls[-3:]: # Show last 3 tools
|
||||
lines.append(f" 🔧 {tool['name']}")
|
||||
|
||||
for cmd in self._commands[-2:]: # Show last 2 commands
|
||||
display_cmd = cmd[:40] + "..." if len(cmd) > 40 else cmd
|
||||
lines.append(f" 💻 {display_cmd}")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
def clear(self) -> None:
|
||||
"""Clear all status."""
|
||||
with self._lock:
|
||||
self._status = ""
|
||||
self._tool_calls.clear()
|
||||
self._commands.clear()
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# NonBlockingInput - Async input handling
|
||||
# ============================================================================
|
||||
|
||||
class NonBlockingInput:
|
||||
"""
|
||||
Handles user input asynchronously.
|
||||
|
||||
Allows users to type new messages while processing is ongoing.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._queue = MessageQueue()
|
||||
|
||||
@property
|
||||
def has_pending(self) -> bool:
|
||||
"""Check if there are pending inputs."""
|
||||
return not self._queue.is_empty
|
||||
|
||||
@property
|
||||
def pending_count(self) -> int:
|
||||
"""Get number of pending inputs."""
|
||||
return self._queue.count
|
||||
|
||||
def submit(self, message: str) -> bool:
|
||||
"""Submit a new input message."""
|
||||
return self._queue.add(message)
|
||||
|
||||
def pop(self) -> Optional[str]:
|
||||
"""Get next pending input."""
|
||||
return self._queue.pop()
|
||||
|
||||
def peek(self) -> Optional[str]:
|
||||
"""View next pending input without removing."""
|
||||
return self._queue.peek()
|
||||
|
||||
def get_all(self) -> List[str]:
|
||||
"""Get all pending inputs."""
|
||||
return self._queue.get_all()
|
||||
|
||||
def clear(self) -> None:
|
||||
"""Clear all pending inputs."""
|
||||
self._queue.clear()
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Exports
|
||||
# ============================================================================
|
||||
|
||||
__all__ = [
|
||||
'ProcessingState',
|
||||
'MessagePriority',
|
||||
'QueuedMessage',
|
||||
'MessageQueue',
|
||||
'StateManager',
|
||||
'QueueDisplay',
|
||||
'MessageQueueHandler',
|
||||
'AsyncProcessor',
|
||||
'LiveStatusDisplay',
|
||||
'NonBlockingInput',
|
||||
]
|
||||
+1112
-95
File diff suppressed because it is too large.
Load diff
@@ -57,7 +57,7 @@ class CloudDeployer:
|
||||
file.write("FROM python:3.11-slim\n")
|
||||
file.write("WORKDIR /app\n")
|
||||
file.write("COPY . .\n")
|
||||
file.write("RUN pip install flask praisonai==2.3.48 gunicorn markdown\n")
|
||||
file.write("RUN pip install flask praisonai==2.3.63 gunicorn markdown\n")
|
||||
file.write("EXPOSE 8080\n")
|
||||
file.write('CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"]\n')
|
||||
|
||||
|
||||
@@ -135,7 +135,8 @@ class BaseCLIIntegration(ABC):
|
||||
*cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
cwd=self.workspace
|
||||
cwd=self.workspace,
|
||||
env=self.get_env()
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -173,7 +174,8 @@ class BaseCLIIntegration(ABC):
|
||||
*cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
cwd=self.workspace
|
||||
cwd=self.workspace,
|
||||
env=self.get_env()
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -208,7 +210,8 @@ class BaseCLIIntegration(ABC):
|
||||
*cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
cwd=self.workspace
|
||||
cwd=self.workspace,
|
||||
env=self.get_env()
|
||||
)
|
||||
|
||||
try:
|
||||
|
||||
@@ -127,23 +127,16 @@ class ClaudeCodeIntegration(BaseCLIIntegration):
|
||||
"""
|
||||
cmd = ["claude"]
|
||||
|
||||
# Add continue flag if needed
|
||||
if continue_session or self._session_active:
|
||||
cmd.append("--continue")
|
||||
|
||||
# Add skip permissions flag
|
||||
if self.skip_permissions:
|
||||
cmd.append("--dangerously-skip-permissions")
|
||||
|
||||
# Add print mode flag
|
||||
# Add print mode flag for non-interactive output
|
||||
cmd.append("-p")
|
||||
|
||||
# Add prompt
|
||||
cmd.append(prompt)
|
||||
|
||||
# Add output format
|
||||
cmd.extend(["--output-format", self.output_format])
|
||||
|
||||
# Add continue flag if needed
|
||||
if continue_session or self._session_active:
|
||||
cmd.append("--continue")
|
||||
|
||||
# Add model if specified
|
||||
if self.model:
|
||||
cmd.extend(["--model", self.model])
|
||||
@@ -160,6 +153,13 @@ class ClaudeCodeIntegration(BaseCLIIntegration):
|
||||
if self.disallowed_tools:
|
||||
cmd.extend(["--disallowedTools", ",".join(self.disallowed_tools)])
|
||||
|
||||
# Add verbose if needed
|
||||
if options.get('verbose'):
|
||||
cmd.append("--verbose")
|
||||
|
||||
# Add prompt last
|
||||
cmd.append(prompt)
|
||||
|
||||
return cmd
|
||||
|
||||
async def execute(self, prompt: str, **options) -> str:
|
||||
|
||||
@@ -93,6 +93,9 @@ class CodexCLIIntegration(BaseCLIIntegration):
|
||||
"""
|
||||
cmd = ["codex", "exec"]
|
||||
|
||||
# Add working directory
|
||||
cmd.extend(["-C", self.workspace])
|
||||
|
||||
# Add task
|
||||
cmd.append(task)
|
||||
|
||||
|
||||
@@ -97,6 +97,9 @@ class CursorCLIIntegration(BaseCLIIntegration):
|
||||
# Add print mode flag
|
||||
cmd.append("-p")
|
||||
|
||||
# Add workspace
|
||||
cmd.extend(["--workspace", self.workspace])
|
||||
|
||||
# Add force flag if enabled
|
||||
if self.force:
|
||||
cmd.append("--force")
|
||||
|
||||
@@ -92,12 +92,6 @@ class GeminiCLIIntegration(BaseCLIIntegration):
|
||||
"""
|
||||
cmd = ["gemini"]
|
||||
|
||||
# Add print mode flag
|
||||
cmd.append("-p")
|
||||
|
||||
# Add prompt
|
||||
cmd.append(prompt)
|
||||
|
||||
# Add model
|
||||
cmd.extend(["-m", self.model])
|
||||
|
||||
@@ -112,6 +106,12 @@ class GeminiCLIIntegration(BaseCLIIntegration):
|
||||
if self.sandbox:
|
||||
cmd.append("--sandbox")
|
||||
|
||||
# Add YOLO mode for non-interactive execution
|
||||
cmd.append("--yolo")
|
||||
|
||||
# Add prompt as positional argument (must be last)
|
||||
cmd.append(prompt)
|
||||
|
||||
return cmd
|
||||
|
||||
async def execute(self, prompt: str, **options) -> str:
|
||||
|
||||
@@ -0,0 +1,465 @@
|
||||
"""
|
||||
PraisonAI Profiler Module
|
||||
|
||||
Standardized profiling for performance monitoring across praisonai and praisonai-agents.
|
||||
|
||||
Features:
|
||||
- Import timing
|
||||
- Function execution timing
|
||||
- Flow tracking
|
||||
- File/module usage tracking
|
||||
- Memory usage (optional)
|
||||
|
||||
Usage:
|
||||
from praisonai.profiler import Profiler, profile, profile_imports
|
||||
|
||||
# Profile a function
|
||||
@profile
|
||||
def my_function():
|
||||
pass
|
||||
|
||||
# Profile a block
|
||||
with Profiler.block("my_operation"):
|
||||
do_something()
|
||||
|
||||
# Profile imports
|
||||
with profile_imports():
|
||||
import heavy_module
|
||||
|
||||
# Get report
|
||||
Profiler.report()
|
||||
"""
|
||||
|
||||
import time
|
||||
import functools
|
||||
import threading
|
||||
import sys
|
||||
import os
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict, List, Optional, Callable, Any
|
||||
from contextlib import contextmanager
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Data Classes
|
||||
# ============================================================================
|
||||
|
||||
@dataclass
|
||||
class TimingRecord:
|
||||
"""Record of a single timing measurement."""
|
||||
name: str
|
||||
duration_ms: float
|
||||
category: str = "function"
|
||||
file: str = ""
|
||||
line: int = 0
|
||||
timestamp: float = field(default_factory=time.time)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ImportRecord:
|
||||
"""Record of a module import."""
|
||||
module: str
|
||||
duration_ms: float
|
||||
parent: str = ""
|
||||
timestamp: float = field(default_factory=time.time)
|
||||
|
||||
|
||||
@dataclass
|
||||
class FlowRecord:
|
||||
"""Record of execution flow."""
|
||||
step: int
|
||||
name: str
|
||||
file: str
|
||||
line: int
|
||||
duration_ms: float
|
||||
timestamp: float = field(default_factory=time.time)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Profiler Class
|
||||
# ============================================================================
|
||||
|
||||
class Profiler:
|
||||
"""
|
||||
Centralized profiler for performance monitoring.
|
||||
|
||||
Thread-safe singleton pattern for global access.
|
||||
"""
|
||||
|
||||
_instance: Optional['Profiler'] = None
|
||||
_lock = threading.Lock()
|
||||
|
||||
# Class-level storage
|
||||
_timings: List[TimingRecord] = []
|
||||
_imports: List[ImportRecord] = []
|
||||
_flow: List[FlowRecord] = []
|
||||
_enabled: bool = False
|
||||
_flow_step: int = 0
|
||||
_files_accessed: Dict[str, int] = {}
|
||||
|
||||
def __new__(cls):
|
||||
if cls._instance is None:
|
||||
with cls._lock:
|
||||
if cls._instance is None:
|
||||
cls._instance = super().__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
@classmethod
|
||||
def enable(cls) -> None:
|
||||
"""Enable profiling."""
|
||||
cls._enabled = True
|
||||
|
||||
@classmethod
|
||||
def disable(cls) -> None:
|
||||
"""Disable profiling."""
|
||||
cls._enabled = False
|
||||
|
||||
@classmethod
|
||||
def is_enabled(cls) -> bool:
|
||||
"""Check if profiling is enabled."""
|
||||
return cls._enabled or os.environ.get('PRAISONAI_PROFILE', '').lower() in ('1', 'true', 'yes')
|
||||
|
||||
@classmethod
|
||||
def clear(cls) -> None:
|
||||
"""Clear all profiling data."""
|
||||
with cls._lock:
|
||||
cls._timings.clear()
|
||||
cls._imports.clear()
|
||||
cls._flow.clear()
|
||||
cls._flow_step = 0
|
||||
cls._files_accessed.clear()
|
||||
|
||||
@classmethod
|
||||
def record_timing(cls, name: str, duration_ms: float, category: str = "function",
|
||||
file: str = "", line: int = 0) -> None:
|
||||
"""Record a timing measurement."""
|
||||
if not cls.is_enabled():
|
||||
return
|
||||
|
||||
with cls._lock:
|
||||
cls._timings.append(TimingRecord(
|
||||
name=name,
|
||||
duration_ms=duration_ms,
|
||||
category=category,
|
||||
file=file,
|
||||
line=line
|
||||
))
|
||||
|
||||
# Track file access
|
||||
if file:
|
||||
cls._files_accessed[file] = cls._files_accessed.get(file, 0) + 1
|
||||
|
||||
@classmethod
|
||||
def record_import(cls, module: str, duration_ms: float, parent: str = "") -> None:
|
||||
"""Record an import timing."""
|
||||
if not cls.is_enabled():
|
||||
return
|
||||
|
||||
with cls._lock:
|
||||
cls._imports.append(ImportRecord(
|
||||
module=module,
|
||||
duration_ms=duration_ms,
|
||||
parent=parent
|
||||
))
|
||||
|
||||
@classmethod
|
||||
def record_flow(cls, name: str, duration_ms: float, file: str = "", line: int = 0) -> None:
|
||||
"""Record a flow step."""
|
||||
if not cls.is_enabled():
|
||||
return
|
||||
|
||||
with cls._lock:
|
||||
cls._flow_step += 1
|
||||
cls._flow.append(FlowRecord(
|
||||
step=cls._flow_step,
|
||||
name=name,
|
||||
file=file,
|
||||
line=line,
|
||||
duration_ms=duration_ms
|
||||
))
|
||||
|
||||
@classmethod
|
||||
@contextmanager
|
||||
def block(cls, name: str, category: str = "block"):
|
||||
"""Context manager for profiling a block of code."""
|
||||
start = time.time()
|
||||
frame = sys._getframe(2) if hasattr(sys, '_getframe') else None
|
||||
file = frame.f_code.co_filename if frame else ""
|
||||
line = frame.f_lineno if frame else 0
|
||||
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
duration_ms = (time.time() - start) * 1000
|
||||
cls.record_timing(name, duration_ms, category, file, line)
|
||||
cls.record_flow(name, duration_ms, file, line)
|
||||
|
||||
@classmethod
|
||||
def get_timings(cls, category: Optional[str] = None) -> List[TimingRecord]:
|
||||
"""Get timing records, optionally filtered by category."""
|
||||
with cls._lock:
|
||||
if category:
|
||||
return [t for t in cls._timings if t.category == category]
|
||||
return cls._timings.copy()
|
||||
|
||||
@classmethod
|
||||
def get_imports(cls, min_duration_ms: float = 0) -> List[ImportRecord]:
|
||||
"""Get import records, optionally filtered by minimum duration."""
|
||||
with cls._lock:
|
||||
if min_duration_ms > 0:
|
||||
return [i for i in cls._imports if i.duration_ms >= min_duration_ms]
|
||||
return cls._imports.copy()
|
||||
|
||||
@classmethod
|
||||
def get_flow(cls) -> List[FlowRecord]:
|
||||
"""Get flow records."""
|
||||
with cls._lock:
|
||||
return cls._flow.copy()
|
||||
|
||||
@classmethod
|
||||
def get_files_accessed(cls) -> Dict[str, int]:
|
||||
"""Get files accessed with counts."""
|
||||
with cls._lock:
|
||||
return cls._files_accessed.copy()
|
||||
|
||||
@classmethod
|
||||
def get_summary(cls) -> Dict[str, Any]:
|
||||
"""Get profiling summary."""
|
||||
with cls._lock:
|
||||
total_time = sum(t.duration_ms for t in cls._timings)
|
||||
import_time = sum(i.duration_ms for i in cls._imports)
|
||||
|
||||
# Group by category
|
||||
by_category: Dict[str, float] = {}
|
||||
for t in cls._timings:
|
||||
by_category[t.category] = by_category.get(t.category, 0) + t.duration_ms
|
||||
|
||||
# Top slowest
|
||||
slowest = sorted(cls._timings, key=lambda x: x.duration_ms, reverse=True)[:10]
|
||||
slowest_imports = sorted(cls._imports, key=lambda x: x.duration_ms, reverse=True)[:10]
|
||||
|
||||
return {
|
||||
'total_time_ms': total_time,
|
||||
'import_time_ms': import_time,
|
||||
'timing_count': len(cls._timings),
|
||||
'import_count': len(cls._imports),
|
||||
'flow_steps': len(cls._flow),
|
||||
'files_accessed': len(cls._files_accessed),
|
||||
'by_category': by_category,
|
||||
'slowest_operations': [(s.name, s.duration_ms) for s in slowest],
|
||||
'slowest_imports': [(s.module, s.duration_ms) for s in slowest_imports],
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def report(cls, output: str = "console") -> str:
|
||||
"""Generate and output profiling report."""
|
||||
summary = cls.get_summary()
|
||||
|
||||
lines = [
|
||||
"=" * 60,
|
||||
"PraisonAI Profiling Report",
|
||||
"=" * 60,
|
||||
"",
|
||||
f"Total Time: {summary['total_time_ms']:.2f}ms",
|
||||
f"Import Time: {summary['import_time_ms']:.2f}ms",
|
||||
f"Timing Records: {summary['timing_count']}",
|
||||
f"Import Records: {summary['import_count']}",
|
||||
f"Flow Steps: {summary['flow_steps']}",
|
||||
f"Files Accessed: {summary['files_accessed']}",
|
||||
"",
|
||||
"By Category:",
|
||||
]
|
||||
|
||||
for cat, time_ms in summary['by_category'].items():
|
||||
lines.append(f" {cat}: {time_ms:.2f}ms")
|
||||
|
||||
lines.extend([
|
||||
"",
|
||||
"Slowest Operations:",
|
||||
])
|
||||
for name, time_ms in summary['slowest_operations']:
|
||||
lines.append(f" {name}: {time_ms:.2f}ms")
|
||||
|
||||
lines.extend([
|
||||
"",
|
||||
"Slowest Imports:",
|
||||
])
|
||||
for module, time_ms in summary['slowest_imports']:
|
||||
lines.append(f" {module}: {time_ms:.2f}ms")
|
||||
|
||||
lines.append("=" * 60)
|
||||
|
||||
report_text = "\n".join(lines)
|
||||
|
||||
if output == "console":
|
||||
print(report_text)
|
||||
|
||||
return report_text
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Decorators
|
||||
# ============================================================================
|
||||
|
||||
def profile(func: Optional[Callable] = None, *, category: str = "function"):
|
||||
"""
|
||||
Decorator to profile a function.
|
||||
|
||||
Usage:
|
||||
@profile
|
||||
def my_function():
|
||||
pass
|
||||
|
||||
@profile(category="api")
|
||||
def api_call():
|
||||
pass
|
||||
"""
|
||||
def decorator(fn: Callable) -> Callable:
|
||||
@functools.wraps(fn)
|
||||
def wrapper(*args, **kwargs):
|
||||
if not Profiler.is_enabled():
|
||||
return fn(*args, **kwargs)
|
||||
|
||||
start = time.time()
|
||||
try:
|
||||
return fn(*args, **kwargs)
|
||||
finally:
|
||||
duration_ms = (time.time() - start) * 1000
|
||||
file = fn.__code__.co_filename if hasattr(fn, '__code__') else ""
|
||||
line = fn.__code__.co_firstlineno if hasattr(fn, '__code__') else 0
|
||||
Profiler.record_timing(fn.__name__, duration_ms, category, file, line)
|
||||
Profiler.record_flow(fn.__name__, duration_ms, file, line)
|
||||
|
||||
return wrapper
|
||||
|
||||
if func is not None:
|
||||
return decorator(func)
|
||||
return decorator
|
||||
|
||||
|
||||
def profile_async(func: Optional[Callable] = None, *, category: str = "async"):
|
||||
"""
|
||||
Decorator to profile an async function.
|
||||
"""
|
||||
def decorator(fn: Callable) -> Callable:
|
||||
@functools.wraps(fn)
|
||||
async def wrapper(*args, **kwargs):
|
||||
if not Profiler.is_enabled():
|
||||
return await fn(*args, **kwargs)
|
||||
|
||||
start = time.time()
|
||||
try:
|
||||
return await fn(*args, **kwargs)
|
||||
finally:
|
||||
duration_ms = (time.time() - start) * 1000
|
||||
file = fn.__code__.co_filename if hasattr(fn, '__code__') else ""
|
||||
line = fn.__code__.co_firstlineno if hasattr(fn, '__code__') else 0
|
||||
Profiler.record_timing(fn.__name__, duration_ms, category, file, line)
|
||||
Profiler.record_flow(fn.__name__, duration_ms, file, line)
|
||||
|
||||
return wrapper
|
||||
|
||||
if func is not None:
|
||||
return decorator(func)
|
||||
return decorator
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Import Profiling
|
||||
# ============================================================================
|
||||
|
||||
class ImportProfiler:
|
||||
"""
|
||||
Context manager to profile imports.
|
||||
|
||||
Usage:
|
||||
with profile_imports() as profiler:
|
||||
import heavy_module
|
||||
|
||||
print(profiler.get_imports())
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._original_import = None
|
||||
self._imports: List[ImportRecord] = []
|
||||
|
||||
def __enter__(self):
|
||||
import builtins
|
||||
self._original_import = builtins.__import__
|
||||
|
||||
def profiled_import(name, globals=None, locals=None, fromlist=(), level=0):
|
||||
start = time.time()
|
||||
try:
|
||||
return self._original_import(name, globals, locals, fromlist, level)
|
||||
finally:
|
||||
duration_ms = (time.time() - start) * 1000
|
||||
if duration_ms > 1: # Only record imports > 1ms
|
||||
record = ImportRecord(module=name, duration_ms=duration_ms)
|
||||
self._imports.append(record)
|
||||
Profiler.record_import(name, duration_ms)
|
||||
|
||||
builtins.__import__ = profiled_import
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
import builtins
|
||||
builtins.__import__ = self._original_import
|
||||
return False
|
||||
|
||||
def get_imports(self, min_duration_ms: float = 0) -> List[ImportRecord]:
|
||||
"""Get recorded imports."""
|
||||
if min_duration_ms > 0:
|
||||
return [i for i in self._imports if i.duration_ms >= min_duration_ms]
|
||||
return self._imports.copy()
|
||||
|
||||
def get_slowest(self, n: int = 10) -> List[ImportRecord]:
|
||||
"""Get N slowest imports."""
|
||||
return sorted(self._imports, key=lambda x: x.duration_ms, reverse=True)[:n]
|
||||
|
||||
|
||||
def profile_imports():
|
||||
"""Create an import profiler context manager."""
|
||||
return ImportProfiler()
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Quick Profiling Functions
|
||||
# ============================================================================
|
||||
|
||||
def time_import(module_name: str) -> float:
|
||||
"""
|
||||
Time how long it takes to import a module.
|
||||
|
||||
Returns duration in milliseconds.
|
||||
"""
|
||||
start = time.time()
|
||||
__import__(module_name)
|
||||
return (time.time() - start) * 1000
|
||||
|
||||
|
||||
def check_module_available(module_name: str) -> bool:
|
||||
"""
|
||||
Check if a module is available without importing it.
|
||||
|
||||
Uses importlib.util.find_spec which is fast.
|
||||
"""
|
||||
import importlib.util
|
||||
return importlib.util.find_spec(module_name) is not None
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Exports
|
||||
# ============================================================================
|
||||
|
||||
__all__ = [
|
||||
'Profiler',
|
||||
'profile',
|
||||
'profile_async',
|
||||
'profile_imports',
|
||||
'ImportProfiler',
|
||||
'time_import',
|
||||
'check_module_available',
|
||||
'TimingRecord',
|
||||
'ImportRecord',
|
||||
'FlowRecord',
|
||||
]
|
||||
@@ -1 +1 @@
|
||||
__version__ = "2.3.48"
|
||||
__version__ = "2.3.63"
|
||||
@@ -12,7 +12,7 @@ dependencies = [
|
||||
"rich>=13.7",
|
||||
"markdown>=3.5",
|
||||
"pyparsing>=3.0.0",
|
||||
"praisonaiagents>=0.1.17",
|
||||
"praisonaiagents>=0.1.21",
|
||||
"python-dotenv>=0.19.0",
|
||||
"instructor>=1.3.3",
|
||||
"PyYAML>=6.0",
|
||||
|
||||
@@ -83,13 +83,19 @@ def setup_test_environment(request):
|
||||
# Real tests (marked with @pytest.mark.real) should use actual environment variables
|
||||
is_real_test = False
|
||||
|
||||
# Check if this test is marked as a real test
|
||||
# Check if this test is marked as a real test or integration test
|
||||
if hasattr(request, 'node') and hasattr(request.node, 'iter_markers'):
|
||||
for marker in request.node.iter_markers():
|
||||
if marker.name == 'real':
|
||||
if marker.name in ('real', 'integration'):
|
||||
is_real_test = True
|
||||
break
|
||||
|
||||
# Also check if the test file is in the integration directory
|
||||
if hasattr(request, 'fspath') and request.fspath:
|
||||
test_path = str(request.fspath)
|
||||
if '/integration/' in test_path or '\\integration\\' in test_path:
|
||||
is_real_test = True
|
||||
|
||||
# Store original values to restore later
|
||||
original_values = {}
|
||||
|
||||
|
||||
@@ -14,9 +14,17 @@ import yaml
|
||||
# Add the source path for imports
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
|
||||
|
||||
# Skip if no API key
|
||||
if not os.environ.get('OPENAI_API_KEY'):
|
||||
pytest.skip("OPENAI_API_KEY not set", allow_module_level=True)
|
||||
# Skip if no API key or using test/invalid key
|
||||
# Valid OpenAI keys start with 'sk-' and are typically 51+ characters
|
||||
api_key = os.environ.get('OPENAI_API_KEY', '')
|
||||
is_test_key = (
|
||||
not api_key or
|
||||
'test' in api_key.lower() or
|
||||
not api_key.startswith('sk-') or
|
||||
len(api_key) < 40
|
||||
)
|
||||
if is_test_key:
|
||||
pytest.skip("OPENAI_API_KEY not set or using test/invalid key", allow_module_level=True)
|
||||
|
||||
try:
|
||||
from praisonai.auto import AutoGenerator, WorkflowAutoGenerator
|
||||
|
||||
@@ -19,10 +19,15 @@ import sys
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
|
||||
|
||||
|
||||
# Skip all tests if no API keys are set
|
||||
# Skip all tests if no API keys are set or using test/invalid key
|
||||
# Valid OpenAI keys start with 'sk-' and are typically 51+ characters
|
||||
_api_key = os.environ.get("OPENAI_API_KEY", "")
|
||||
pytestmark = pytest.mark.skipif(
|
||||
not os.environ.get("OPENAI_API_KEY"),
|
||||
reason="OPENAI_API_KEY not set"
|
||||
not _api_key or
|
||||
'test' in _api_key.lower() or
|
||||
not _api_key.startswith('sk-') or
|
||||
len(_api_key) < 40,
|
||||
reason="OPENAI_API_KEY not set or using test/invalid key"
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -220,8 +220,11 @@ class TestRealAPIIntegration:
|
||||
"""Tests that make real API calls - requires API keys."""
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not os.getenv('OPENAI_API_KEY'),
|
||||
reason="OPENAI_API_KEY not set"
|
||||
not os.getenv('OPENAI_API_KEY') or
|
||||
'test' in os.getenv('OPENAI_API_KEY', '').lower() or
|
||||
not os.getenv('OPENAI_API_KEY', '').startswith('sk-') or
|
||||
len(os.getenv('OPENAI_API_KEY', '')) < 40,
|
||||
reason="OPENAI_API_KEY not set or using test/invalid key"
|
||||
)
|
||||
def test_real_openai_call(self):
|
||||
"""Test real OpenAI API call with routed model."""
|
||||
|
||||
@@ -0,0 +1,221 @@
|
||||
"""
|
||||
Tests for the CLI approval system.
|
||||
|
||||
Tests:
|
||||
1. --trust flag auto-approves all tools
|
||||
2. --approve-level flag with different risk levels
|
||||
3. Approval callback integration
|
||||
"""
|
||||
|
||||
import pytest
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the src directory to the path
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
||||
|
||||
|
||||
class TestApprovalFlags:
|
||||
"""Test CLI approval flags."""
|
||||
|
||||
def test_trust_flag_exists(self):
|
||||
"""Test that --trust flag is defined in argparse."""
|
||||
import argparse
|
||||
|
||||
# Check that the args object has the trust attribute after parsing
|
||||
# Parse with --help to avoid actually running anything
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--trust", action="store_true")
|
||||
parser.add_argument("--approve-level", type=str, choices=["low", "medium", "high", "critical"])
|
||||
|
||||
# Test that the arguments can be parsed
|
||||
args = parser.parse_args(['--trust'])
|
||||
assert args.trust is True
|
||||
|
||||
args = parser.parse_args([])
|
||||
assert args.trust is False
|
||||
|
||||
def test_approve_level_flag_exists(self):
|
||||
"""Test that --approve-level flag is defined in argparse."""
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--approve-level", type=str, choices=["low", "medium", "high", "critical"])
|
||||
|
||||
# Test that the arguments can be parsed
|
||||
args = parser.parse_args(['--approve-level', 'high'])
|
||||
assert args.approve_level == 'high'
|
||||
|
||||
args = parser.parse_args(['--approve-level', 'critical'])
|
||||
assert args.approve_level == 'critical'
|
||||
|
||||
def test_trust_flag_in_praisonai_parser(self):
|
||||
"""Test that --trust flag is in PraisonAI's actual parser."""
|
||||
from praisonai.cli.main import PraisonAI
|
||||
|
||||
# Create instance - parse_args is called in __init__
|
||||
praison = PraisonAI()
|
||||
|
||||
# Call parse_args to get the args
|
||||
args = praison.parse_args()
|
||||
|
||||
# The args should have trust attribute after parse_args
|
||||
assert hasattr(args, 'trust'), "PraisonAI.args should have 'trust' attribute"
|
||||
# Cleanup
|
||||
del praison
|
||||
|
||||
def test_approve_level_flag_in_praisonai_parser(self):
|
||||
"""Test that --approve-level flag is in PraisonAI's actual parser."""
|
||||
from praisonai.cli.main import PraisonAI
|
||||
|
||||
# Create instance - parse_args is called in __init__
|
||||
praison = PraisonAI()
|
||||
|
||||
# Call parse_args to get the args
|
||||
args = praison.parse_args()
|
||||
|
||||
# The args should have approve_level attribute after parse_args
|
||||
assert hasattr(args, 'approve_level'), "PraisonAI.args should have 'approve_level' attribute"
|
||||
|
||||
|
||||
class TestApprovalCallback:
|
||||
"""Test approval callback system."""
|
||||
|
||||
def test_set_approval_callback(self):
|
||||
"""Test that we can set a custom approval callback."""
|
||||
from praisonaiagents.approval import set_approval_callback, ApprovalDecision
|
||||
|
||||
callback_called = []
|
||||
|
||||
def custom_callback(function_name, arguments, risk_level):
|
||||
callback_called.append((function_name, arguments, risk_level))
|
||||
return ApprovalDecision(approved=True, reason="Test approved")
|
||||
|
||||
set_approval_callback(custom_callback)
|
||||
|
||||
# Verify callback was set
|
||||
from praisonaiagents.approval import approval_callback
|
||||
assert approval_callback == custom_callback
|
||||
|
||||
# Reset callback
|
||||
set_approval_callback(None)
|
||||
|
||||
def test_auto_approve_callback(self):
|
||||
"""Test auto-approve callback returns approved decision."""
|
||||
from praisonaiagents.approval import ApprovalDecision
|
||||
|
||||
def auto_approve_callback(function_name, arguments, risk_level):
|
||||
return ApprovalDecision(approved=True, reason="Auto-approved")
|
||||
|
||||
decision = auto_approve_callback("execute_command", {"command": "ls"}, "critical")
|
||||
assert decision.approved is True
|
||||
assert decision.reason == "Auto-approved"
|
||||
|
||||
def test_level_based_callback(self):
|
||||
"""Test level-based approval callback."""
|
||||
from praisonaiagents.approval import ApprovalDecision
|
||||
|
||||
def level_based_callback(function_name, arguments, risk_level, max_level="high"):
|
||||
"""Approve based on risk level threshold."""
|
||||
levels = {"low": 1, "medium": 2, "high": 3, "critical": 4}
|
||||
tool_level = levels.get(risk_level, 4)
|
||||
max_allowed = levels.get(max_level, 3)
|
||||
|
||||
if tool_level <= max_allowed:
|
||||
return ApprovalDecision(approved=True, reason=f"Auto-approved (level {risk_level} <= {max_level})")
|
||||
else:
|
||||
return ApprovalDecision(approved=False, reason=f"Denied (level {risk_level} > {max_level})")
|
||||
|
||||
# Test with max_level="high"
|
||||
# Should approve low, medium, high but deny critical
|
||||
assert level_based_callback("test", {}, "low", "high").approved is True
|
||||
assert level_based_callback("test", {}, "medium", "high").approved is True
|
||||
assert level_based_callback("test", {}, "high", "high").approved is True
|
||||
assert level_based_callback("test", {}, "critical", "high").approved is False
|
||||
|
||||
# Test with max_level="critical"
|
||||
# Should approve everything
|
||||
assert level_based_callback("test", {}, "critical", "critical").approved is True
|
||||
|
||||
|
||||
class TestApprovalRequirements:
|
||||
"""Test approval requirement configuration."""
|
||||
|
||||
def test_default_dangerous_tools(self):
|
||||
"""Test that default dangerous tools are configured."""
|
||||
from praisonaiagents.approval import APPROVAL_REQUIRED_TOOLS, TOOL_RISK_LEVELS
|
||||
|
||||
# Check critical tools
|
||||
assert "execute_command" in APPROVAL_REQUIRED_TOOLS
|
||||
assert TOOL_RISK_LEVELS.get("execute_command") == "critical"
|
||||
|
||||
# Check high risk tools
|
||||
assert "write_file" in APPROVAL_REQUIRED_TOOLS
|
||||
assert TOOL_RISK_LEVELS.get("write_file") == "high"
|
||||
|
||||
def test_remove_approval_requirement(self):
|
||||
"""Test removing approval requirement."""
|
||||
from praisonaiagents.approval import (
|
||||
add_approval_requirement,
|
||||
remove_approval_requirement,
|
||||
is_approval_required,
|
||||
APPROVAL_REQUIRED_TOOLS
|
||||
)
|
||||
|
||||
# Add a test tool
|
||||
add_approval_requirement("test_tool", "medium")
|
||||
assert is_approval_required("test_tool") is True
|
||||
|
||||
# Remove it
|
||||
remove_approval_requirement("test_tool")
|
||||
assert is_approval_required("test_tool") is False
|
||||
|
||||
def test_is_approval_required(self):
|
||||
"""Test checking if approval is required."""
|
||||
from praisonaiagents.approval import is_approval_required
|
||||
|
||||
# execute_command should require approval
|
||||
assert is_approval_required("execute_command") is True
|
||||
|
||||
# read_file should not require approval
|
||||
assert is_approval_required("read_file") is False
|
||||
|
||||
# list_files should not require approval
|
||||
assert is_approval_required("list_files") is False
|
||||
|
||||
|
||||
class TestApprovalDecision:
|
||||
"""Test ApprovalDecision class."""
|
||||
|
||||
def test_approval_decision_approved(self):
|
||||
"""Test approved decision."""
|
||||
from praisonaiagents.approval import ApprovalDecision
|
||||
|
||||
decision = ApprovalDecision(approved=True, reason="User approved")
|
||||
assert decision.approved is True
|
||||
assert decision.reason == "User approved"
|
||||
assert decision.modified_args == {}
|
||||
|
||||
def test_approval_decision_denied(self):
|
||||
"""Test denied decision."""
|
||||
from praisonaiagents.approval import ApprovalDecision
|
||||
|
||||
decision = ApprovalDecision(approved=False, reason="User denied")
|
||||
assert decision.approved is False
|
||||
assert decision.reason == "User denied"
|
||||
|
||||
def test_approval_decision_with_modified_args(self):
|
||||
"""Test decision with modified arguments."""
|
||||
from praisonaiagents.approval import ApprovalDecision
|
||||
|
||||
decision = ApprovalDecision(
|
||||
approved=True,
|
||||
reason="Approved with modifications",
|
||||
modified_args={"command": "ls -la"}
|
||||
)
|
||||
assert decision.approved is True
|
||||
assert decision.modified_args == {"command": "ls -la"}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
@@ -0,0 +1,289 @@
|
||||
"""
|
||||
Unit tests for @ mention autocomplete feature.
|
||||
|
||||
Tests for:
|
||||
1. AtMentionContext - Context detection
|
||||
2. FileSearchService - File search with fuzzy matching
|
||||
3. AtMentionCompleter - Autocomplete integration
|
||||
"""
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 1. AtMentionContext Tests
|
||||
# ============================================================================
|
||||
|
||||
class TestAtMentionContext:
|
||||
"""Test @ mention context detection."""
|
||||
|
||||
def test_detect_at_start(self):
|
||||
"""Should detect @ at start of input."""
|
||||
from praisonai.cli.features.at_mentions import detect_at_mention
|
||||
|
||||
result = detect_at_mention("@", cursor_pos=1)
|
||||
assert result is not None
|
||||
assert result.is_active
|
||||
assert result.query == ""
|
||||
assert result.start_pos == 0
|
||||
|
||||
def test_detect_at_with_query(self):
|
||||
"""Should detect @ with partial query."""
|
||||
from praisonai.cli.features.at_mentions import detect_at_mention
|
||||
|
||||
result = detect_at_mention("@main", cursor_pos=5)
|
||||
assert result is not None
|
||||
assert result.is_active
|
||||
assert result.query == "main"
|
||||
assert result.start_pos == 0
|
||||
|
||||
def test_detect_at_mid_text(self):
|
||||
"""Should detect @ in middle of text."""
|
||||
from praisonai.cli.features.at_mentions import detect_at_mention
|
||||
|
||||
result = detect_at_mention("read @src/", cursor_pos=10)
|
||||
assert result is not None
|
||||
assert result.is_active
|
||||
assert result.query == "src/"
|
||||
assert result.start_pos == 5
|
||||
|
||||
def test_no_at_symbol(self):
|
||||
"""Should return None when no @ present."""
|
||||
from praisonai.cli.features.at_mentions import detect_at_mention
|
||||
|
||||
result = detect_at_mention("hello world", cursor_pos=11)
|
||||
assert result is None
|
||||
|
||||
def test_at_after_space_breaks_context(self):
|
||||
"""Should not detect @ if space after query."""
|
||||
from praisonai.cli.features.at_mentions import detect_at_mention
|
||||
|
||||
result = detect_at_mention("@file.txt ", cursor_pos=10)
|
||||
assert result is None # Space breaks the @ context
|
||||
|
||||
def test_cursor_before_at(self):
|
||||
"""Should not detect if cursor is before @."""
|
||||
from praisonai.cli.features.at_mentions import detect_at_mention
|
||||
|
||||
result = detect_at_mention("hello @file", cursor_pos=3)
|
||||
assert result is None
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 2. FileSuggestion Tests
|
||||
# ============================================================================
|
||||
|
||||
class TestFileSuggestion:
|
||||
"""Test FileSuggestion dataclass."""
|
||||
|
||||
def test_create_file_suggestion(self):
|
||||
"""Should create file suggestion."""
|
||||
from praisonai.cli.features.at_mentions import FileSuggestion
|
||||
|
||||
suggestion = FileSuggestion(
|
||||
path="src/main.py",
|
||||
file_type="file",
|
||||
score=100
|
||||
)
|
||||
assert suggestion.path == "src/main.py"
|
||||
assert suggestion.file_type == "file"
|
||||
assert suggestion.score == 100
|
||||
|
||||
def test_directory_suggestion(self):
|
||||
"""Should create directory suggestion."""
|
||||
from praisonai.cli.features.at_mentions import FileSuggestion
|
||||
|
||||
suggestion = FileSuggestion(
|
||||
path="src/",
|
||||
file_type="directory",
|
||||
score=90
|
||||
)
|
||||
assert suggestion.file_type == "directory"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 3. FileSearchService Tests
|
||||
# ============================================================================
|
||||
|
||||
class TestFileSearchService:
|
||||
"""Test FileSearchService for file discovery."""
|
||||
|
||||
def test_service_creation(self):
|
||||
"""Should create service."""
|
||||
from praisonai.cli.features.at_mentions import FileSearchService
|
||||
|
||||
service = FileSearchService(root_dir=".")
|
||||
assert service is not None
|
||||
|
||||
def test_search_files(self):
|
||||
"""Should search files in directory."""
|
||||
from praisonai.cli.features.at_mentions import FileSearchService
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
# Create test files
|
||||
open(os.path.join(tmpdir, "main.py"), "w").close()
|
||||
open(os.path.join(tmpdir, "test.py"), "w").close()
|
||||
os.makedirs(os.path.join(tmpdir, "src"))
|
||||
open(os.path.join(tmpdir, "src", "app.py"), "w").close()
|
||||
|
||||
service = FileSearchService(root_dir=tmpdir)
|
||||
results = service.search("main")
|
||||
|
||||
assert len(results) >= 1
|
||||
assert any("main.py" in r.path for r in results)
|
||||
|
||||
def test_fuzzy_match(self):
|
||||
"""Should fuzzy match file names."""
|
||||
from praisonai.cli.features.at_mentions import FileSearchService
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
open(os.path.join(tmpdir, "my_long_filename.py"), "w").close()
|
||||
|
||||
service = FileSearchService(root_dir=tmpdir)
|
||||
results = service.search("mlf") # Fuzzy match
|
||||
|
||||
assert len(results) >= 1
|
||||
|
||||
def test_empty_query_returns_all(self):
|
||||
"""Should return files when query is empty."""
|
||||
from praisonai.cli.features.at_mentions import FileSearchService
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
open(os.path.join(tmpdir, "a.py"), "w").close()
|
||||
open(os.path.join(tmpdir, "b.py"), "w").close()
|
||||
|
||||
service = FileSearchService(root_dir=tmpdir)
|
||||
results = service.search("")
|
||||
|
||||
assert len(results) >= 2
|
||||
|
||||
def test_respects_max_results(self):
|
||||
"""Should limit results."""
|
||||
from praisonai.cli.features.at_mentions import FileSearchService
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
for i in range(20):
|
||||
open(os.path.join(tmpdir, f"file{i}.py"), "w").close()
|
||||
|
||||
service = FileSearchService(root_dir=tmpdir)
|
||||
results = service.search("", max_results=5)
|
||||
|
||||
assert len(results) <= 5
|
||||
|
||||
def test_cache_works(self):
|
||||
"""Should cache results."""
|
||||
from praisonai.cli.features.at_mentions import FileSearchService
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
open(os.path.join(tmpdir, "test.py"), "w").close()
|
||||
|
||||
service = FileSearchService(root_dir=tmpdir, cache_ttl=10)
|
||||
|
||||
# First search
|
||||
start = time.time()
|
||||
results1 = service.search("test")
|
||||
first_time = time.time() - start
|
||||
|
||||
# Second search (cached)
|
||||
start = time.time()
|
||||
results2 = service.search("test")
|
||||
second_time = time.time() - start
|
||||
|
||||
assert len(results1) == len(results2)
|
||||
# Cache should be faster (or at least not slower)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 4. AtMentionCompleter Tests
|
||||
# ============================================================================
|
||||
|
||||
class TestAtMentionCompleter:
|
||||
"""Test AtMentionCompleter for prompt_toolkit integration."""
|
||||
|
||||
def test_completer_creation(self):
|
||||
"""Should create completer."""
|
||||
from praisonai.cli.features.at_mentions import AtMentionCompleter
|
||||
|
||||
completer = AtMentionCompleter(root_dir=".")
|
||||
assert completer is not None
|
||||
|
||||
def test_no_completions_without_at(self):
|
||||
"""Should not complete without @."""
|
||||
from praisonai.cli.features.at_mentions import AtMentionCompleter
|
||||
from prompt_toolkit.document import Document
|
||||
|
||||
completer = AtMentionCompleter(root_dir=".")
|
||||
doc = Document("hello world")
|
||||
completions = list(completer.get_completions(doc, None))
|
||||
|
||||
assert len(completions) == 0
|
||||
|
||||
def test_completions_with_at(self):
|
||||
"""Should complete with @."""
|
||||
from praisonai.cli.features.at_mentions import AtMentionCompleter
|
||||
from prompt_toolkit.document import Document
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
open(os.path.join(tmpdir, "main.py"), "w").close()
|
||||
|
||||
completer = AtMentionCompleter(root_dir=tmpdir)
|
||||
doc = Document("@main")
|
||||
completions = list(completer.get_completions(doc, None))
|
||||
|
||||
assert len(completions) >= 1
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 5. CombinedCompleter Tests
|
||||
# ============================================================================
|
||||
|
||||
class TestCombinedCompleter:
|
||||
"""Test CombinedCompleter that handles both / and @."""
|
||||
|
||||
def test_slash_commands(self):
|
||||
"""Should complete slash commands."""
|
||||
from praisonai.cli.features.at_mentions import CombinedCompleter
|
||||
from prompt_toolkit.document import Document
|
||||
|
||||
completer = CombinedCompleter(
|
||||
commands=["help", "exit", "queue"],
|
||||
root_dir="."
|
||||
)
|
||||
doc = Document("/he")
|
||||
completions = list(completer.get_completions(doc, None))
|
||||
|
||||
assert len(completions) >= 1
|
||||
assert any("/help" in c.text for c in completions)
|
||||
|
||||
def test_at_mentions(self):
|
||||
"""Should complete @ mentions."""
|
||||
from praisonai.cli.features.at_mentions import CombinedCompleter
|
||||
from prompt_toolkit.document import Document
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
open(os.path.join(tmpdir, "test.py"), "w").close()
|
||||
|
||||
completer = CombinedCompleter(
|
||||
commands=["help"],
|
||||
root_dir=tmpdir
|
||||
)
|
||||
doc = Document("@test")
|
||||
completions = list(completer.get_completions(doc, None))
|
||||
|
||||
assert len(completions) >= 1
|
||||
|
||||
def test_no_completions_for_regular_text(self):
|
||||
"""Should not complete regular text."""
|
||||
from praisonai.cli.features.at_mentions import CombinedCompleter
|
||||
from prompt_toolkit.document import Document
|
||||
|
||||
completer = CombinedCompleter(
|
||||
commands=["help"],
|
||||
root_dir="."
|
||||
)
|
||||
doc = Document("hello world")
|
||||
completions = list(completer.get_completions(doc, None))
|
||||
|
||||
assert len(completions) == 0
|
||||
@@ -0,0 +1,151 @@
|
||||
"""
|
||||
Unit tests for --chat flag (alias for --chat-mode).
|
||||
|
||||
Tests verify that:
|
||||
1. --chat flag works as an alias for --chat-mode
|
||||
2. --chat flag does not conflict with 'chat' subcommand
|
||||
3. Both flags set chat_mode=True correctly
|
||||
4. Backward compatibility with --chat-mode is maintained
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import argparse
|
||||
|
||||
# Add parent directory to path for imports
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))))
|
||||
|
||||
|
||||
class TestChatFlagParsing:
|
||||
"""Test argument parsing for --chat and --chat-mode flags."""
|
||||
|
||||
def test_chat_mode_flag_sets_chat_mode_true(self):
|
||||
"""--chat-mode should set chat_mode=True."""
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--chat-mode", "--chat", action="store_true", dest="chat_mode")
|
||||
parser.add_argument("command", nargs="?")
|
||||
|
||||
args, _ = parser.parse_known_args(["--chat-mode", "test prompt"])
|
||||
assert args.chat_mode is True
|
||||
assert args.command == "test prompt"
|
||||
|
||||
def test_chat_flag_sets_chat_mode_true(self):
|
||||
"""--chat should set chat_mode=True (alias for --chat-mode)."""
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--chat-mode", "--chat", action="store_true", dest="chat_mode")
|
||||
parser.add_argument("command", nargs="?")
|
||||
|
||||
args, _ = parser.parse_known_args(["--chat", "test prompt"])
|
||||
assert args.chat_mode is True
|
||||
assert args.command == "test prompt"
|
||||
|
||||
def test_chat_subcommand_does_not_set_chat_mode(self):
|
||||
"""'chat' as positional command should NOT set chat_mode."""
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--chat-mode", "--chat", action="store_true", dest="chat_mode")
|
||||
parser.add_argument("command", nargs="?")
|
||||
|
||||
args, _ = parser.parse_known_args(["chat"])
|
||||
assert args.chat_mode is False
|
||||
assert args.command == "chat"
|
||||
|
||||
def test_chat_flag_after_prompt(self):
|
||||
"""--chat after prompt should work."""
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--chat-mode", "--chat", action="store_true", dest="chat_mode")
|
||||
parser.add_argument("command", nargs="?")
|
||||
|
||||
args, _ = parser.parse_known_args(["What is 2+2?", "--chat"])
|
||||
assert args.chat_mode is True
|
||||
assert args.command == "What is 2+2?"
|
||||
|
||||
def test_chat_mode_flag_after_prompt(self):
|
||||
"""--chat-mode after prompt should work (backward compat)."""
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--chat-mode", "--chat", action="store_true", dest="chat_mode")
|
||||
parser.add_argument("command", nargs="?")
|
||||
|
||||
args, _ = parser.parse_known_args(["What is 2+2?", "--chat-mode"])
|
||||
assert args.chat_mode is True
|
||||
assert args.command == "What is 2+2?"
|
||||
|
||||
def test_no_flag_defaults_to_false(self):
|
||||
"""Without --chat or --chat-mode, chat_mode should be False."""
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--chat-mode", "--chat", action="store_true", dest="chat_mode")
|
||||
parser.add_argument("command", nargs="?")
|
||||
|
||||
args, _ = parser.parse_known_args(["some prompt"])
|
||||
assert args.chat_mode is False
|
||||
assert args.command == "some prompt"
|
||||
|
||||
|
||||
class TestChatFlagNoConflict:
|
||||
"""Test that --chat flag does not conflict with other features."""
|
||||
|
||||
def test_chat_flag_with_other_flags(self):
|
||||
"""--chat should work alongside other flags."""
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--chat-mode", "--chat", action="store_true", dest="chat_mode")
|
||||
parser.add_argument("--verbose", "-v", action="store_true")
|
||||
parser.add_argument("--model", type=str)
|
||||
parser.add_argument("command", nargs="?")
|
||||
|
||||
args, _ = parser.parse_known_args(["--chat", "--verbose", "--model", "gpt-4o", "test"])
|
||||
assert args.chat_mode is True
|
||||
assert args.verbose is True
|
||||
assert args.model == "gpt-4o"
|
||||
assert args.command == "test"
|
||||
|
||||
def test_chat_subcommand_with_other_flags(self):
|
||||
"""'chat' subcommand should work with other flags."""
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--chat-mode", "--chat", action="store_true", dest="chat_mode")
|
||||
parser.add_argument("--verbose", "-v", action="store_true")
|
||||
parser.add_argument("command", nargs="?")
|
||||
|
||||
args, _ = parser.parse_known_args(["chat", "--verbose"])
|
||||
assert args.chat_mode is False # subcommand, not flag
|
||||
assert args.command == "chat"
|
||||
assert args.verbose is True
|
||||
|
||||
|
||||
class TestChatFlagIntegration:
|
||||
"""Integration tests with actual PraisonAI CLI parser."""
|
||||
|
||||
def test_praisonai_parser_has_chat_flag(self):
|
||||
"""PraisonAI CLI should have --chat as alias for --chat-mode."""
|
||||
# This test will fail until we implement the change
|
||||
from praisonai.cli.main import PraisonAI
|
||||
|
||||
# Create instance and get parser
|
||||
pai = PraisonAI()
|
||||
|
||||
# Mock sys.argv to avoid test environment issues
|
||||
import sys
|
||||
original_argv = sys.argv
|
||||
try:
|
||||
sys.argv = ["praisonai", "--chat", "test prompt"]
|
||||
args = pai.parse_args()
|
||||
assert hasattr(args, 'chat_mode')
|
||||
# After implementation, this should be True
|
||||
# For now, it may work due to argparse prefix matching
|
||||
finally:
|
||||
sys.argv = original_argv
|
||||
|
||||
def test_praisonai_chat_subcommand_still_works(self):
|
||||
"""'praisonai chat' subcommand should still launch Chainlit.
|
||||
|
||||
Note: In test environment, parse_args returns default args to avoid
|
||||
pytest interference. This test verifies the parser structure instead.
|
||||
"""
|
||||
# Test the parser structure directly without going through PraisonAI
|
||||
# which has test environment detection
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--chat-mode", "--chat", action="store_true", dest="chat_mode")
|
||||
parser.add_argument("command", nargs="?")
|
||||
|
||||
# Simulate 'praisonai chat' - should set command='chat', not chat_mode
|
||||
args, _ = parser.parse_known_args(["chat"])
|
||||
assert args.command == "chat"
|
||||
assert args.chat_mode is False # subcommand, not flag
|
||||
@@ -0,0 +1,661 @@
|
||||
"""
|
||||
Unit tests for Message Queue feature.
|
||||
|
||||
Tests for:
|
||||
1. MessageQueue - FIFO queue for messages
|
||||
2. ProcessingState - State management
|
||||
3. QueuedMessage - Message with metadata
|
||||
4. MessageQueueHandler - Main handler class
|
||||
"""
|
||||
|
||||
import time
|
||||
import threading
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 1. MessageQueue Tests - FIFO operations
|
||||
# ============================================================================
|
||||
|
||||
class TestMessageQueue:
|
||||
"""Test MessageQueue FIFO operations."""
|
||||
|
||||
def test_create_empty_queue(self):
|
||||
"""Queue should start empty."""
|
||||
from praisonai.cli.features.message_queue import MessageQueue
|
||||
queue = MessageQueue()
|
||||
assert queue.is_empty
|
||||
assert queue.count == 0
|
||||
|
||||
def test_add_message(self):
|
||||
"""Should add message to queue."""
|
||||
from praisonai.cli.features.message_queue import MessageQueue
|
||||
queue = MessageQueue()
|
||||
queue.add("Hello")
|
||||
assert not queue.is_empty
|
||||
assert queue.count == 1
|
||||
|
||||
def test_add_multiple_messages(self):
|
||||
"""Should add multiple messages in order."""
|
||||
from praisonai.cli.features.message_queue import MessageQueue
|
||||
queue = MessageQueue()
|
||||
queue.add("First")
|
||||
queue.add("Second")
|
||||
queue.add("Third")
|
||||
assert queue.count == 3
|
||||
|
||||
def test_pop_message_fifo(self):
|
||||
"""Should pop messages in FIFO order."""
|
||||
from praisonai.cli.features.message_queue import MessageQueue
|
||||
queue = MessageQueue()
|
||||
queue.add("First")
|
||||
queue.add("Second")
|
||||
msg = queue.pop()
|
||||
assert msg == "First"
|
||||
assert queue.count == 1
|
||||
|
||||
def test_pop_empty_queue(self):
|
||||
"""Should return None when popping empty queue."""
|
||||
from praisonai.cli.features.message_queue import MessageQueue
|
||||
queue = MessageQueue()
|
||||
msg = queue.pop()
|
||||
assert msg is None
|
||||
|
||||
def test_peek_message(self):
|
||||
"""Should peek without removing."""
|
||||
from praisonai.cli.features.message_queue import MessageQueue
|
||||
queue = MessageQueue()
|
||||
queue.add("First")
|
||||
queue.add("Second")
|
||||
msg = queue.peek()
|
||||
assert msg == "First"
|
||||
assert queue.count == 2 # Not removed
|
||||
|
||||
def test_peek_empty_queue(self):
|
||||
"""Should return None when peeking empty queue."""
|
||||
from praisonai.cli.features.message_queue import MessageQueue
|
||||
queue = MessageQueue()
|
||||
msg = queue.peek()
|
||||
assert msg is None
|
||||
|
||||
def test_clear_queue(self):
|
||||
"""Should clear all messages."""
|
||||
from praisonai.cli.features.message_queue import MessageQueue
|
||||
queue = MessageQueue()
|
||||
queue.add("First")
|
||||
queue.add("Second")
|
||||
queue.clear()
|
||||
assert queue.is_empty
|
||||
assert queue.count == 0
|
||||
|
||||
def test_get_all_messages(self):
|
||||
"""Should get all messages as list."""
|
||||
from praisonai.cli.features.message_queue import MessageQueue
|
||||
queue = MessageQueue()
|
||||
queue.add("First")
|
||||
queue.add("Second")
|
||||
messages = queue.get_all()
|
||||
assert messages == ["First", "Second"]
|
||||
assert queue.count == 2 # Not removed
|
||||
|
||||
def test_remove_at_index(self):
|
||||
"""Should remove message at specific index."""
|
||||
from praisonai.cli.features.message_queue import MessageQueue
|
||||
queue = MessageQueue()
|
||||
queue.add("First")
|
||||
queue.add("Second")
|
||||
queue.add("Third")
|
||||
removed = queue.remove_at(1)
|
||||
assert removed == "Second"
|
||||
assert queue.count == 2
|
||||
assert queue.get_all() == ["First", "Third"]
|
||||
|
||||
def test_remove_at_invalid_index(self):
|
||||
"""Should return None for invalid index."""
|
||||
from praisonai.cli.features.message_queue import MessageQueue
|
||||
queue = MessageQueue()
|
||||
queue.add("First")
|
||||
removed = queue.remove_at(5)
|
||||
assert removed is None
|
||||
assert queue.count == 1
|
||||
|
||||
def test_add_empty_message_ignored(self):
|
||||
"""Should ignore empty messages."""
|
||||
from praisonai.cli.features.message_queue import MessageQueue
|
||||
queue = MessageQueue()
|
||||
queue.add("")
|
||||
queue.add(" ")
|
||||
assert queue.is_empty
|
||||
|
||||
def test_thread_safety(self):
|
||||
"""Queue should be thread-safe."""
|
||||
from praisonai.cli.features.message_queue import MessageQueue
|
||||
queue = MessageQueue()
|
||||
results = []
|
||||
|
||||
def add_messages():
|
||||
for i in range(100):
|
||||
queue.add(f"msg-{i}")
|
||||
|
||||
def pop_messages():
|
||||
for _ in range(100):
|
||||
msg = queue.pop()
|
||||
if msg:
|
||||
results.append(msg)
|
||||
|
||||
t1 = threading.Thread(target=add_messages)
|
||||
t2 = threading.Thread(target=pop_messages)
|
||||
t1.start()
|
||||
t2.start()
|
||||
t1.join()
|
||||
t2.join()
|
||||
|
||||
# All messages should be accounted for
|
||||
total = len(results) + queue.count
|
||||
assert total == 100
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 2. ProcessingState Tests
|
||||
# ============================================================================
|
||||
|
||||
class TestProcessingState:
|
||||
"""Test ProcessingState enum and state management."""
|
||||
|
||||
def test_state_enum_values(self):
|
||||
"""Should have correct state values."""
|
||||
from praisonai.cli.features.message_queue import ProcessingState
|
||||
assert ProcessingState.IDLE.value == "idle"
|
||||
assert ProcessingState.PROCESSING.value == "processing"
|
||||
assert ProcessingState.WAITING_APPROVAL.value == "waiting_approval"
|
||||
|
||||
def test_state_manager_initial_state(self):
|
||||
"""Should start in IDLE state."""
|
||||
from praisonai.cli.features.message_queue import StateManager, ProcessingState
|
||||
manager = StateManager()
|
||||
assert manager.current_state == ProcessingState.IDLE
|
||||
|
||||
def test_state_transition(self):
|
||||
"""Should transition between states."""
|
||||
from praisonai.cli.features.message_queue import StateManager, ProcessingState
|
||||
manager = StateManager()
|
||||
manager.set_state(ProcessingState.PROCESSING)
|
||||
assert manager.current_state == ProcessingState.PROCESSING
|
||||
|
||||
def test_is_idle(self):
|
||||
"""Should correctly report idle state."""
|
||||
from praisonai.cli.features.message_queue import StateManager, ProcessingState
|
||||
manager = StateManager()
|
||||
assert manager.is_idle
|
||||
manager.set_state(ProcessingState.PROCESSING)
|
||||
assert not manager.is_idle
|
||||
|
||||
def test_is_processing(self):
|
||||
"""Should correctly report processing state."""
|
||||
from praisonai.cli.features.message_queue import StateManager, ProcessingState
|
||||
manager = StateManager()
|
||||
assert not manager.is_processing
|
||||
manager.set_state(ProcessingState.PROCESSING)
|
||||
assert manager.is_processing
|
||||
|
||||
def test_state_callback(self):
|
||||
"""Should call callback on state change."""
|
||||
from praisonai.cli.features.message_queue import StateManager, ProcessingState
|
||||
callback_called = []
|
||||
|
||||
def on_state_change(old_state, new_state):
|
||||
callback_called.append((old_state, new_state))
|
||||
|
||||
manager = StateManager(on_state_change=on_state_change)
|
||||
manager.set_state(ProcessingState.PROCESSING)
|
||||
|
||||
assert len(callback_called) == 1
|
||||
assert callback_called[0] == (ProcessingState.IDLE, ProcessingState.PROCESSING)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 3. QueuedMessage Tests
|
||||
# ============================================================================
|
||||
|
||||
class TestQueuedMessage:
|
||||
"""Test QueuedMessage dataclass."""
|
||||
|
||||
def test_create_queued_message(self):
|
||||
"""Should create message with content."""
|
||||
from praisonai.cli.features.message_queue import QueuedMessage
|
||||
msg = QueuedMessage(content="Hello")
|
||||
assert msg.content == "Hello"
|
||||
assert msg.timestamp is not None
|
||||
|
||||
def test_message_has_timestamp(self):
|
||||
"""Should have creation timestamp."""
|
||||
from praisonai.cli.features.message_queue import QueuedMessage
|
||||
before = time.time()
|
||||
msg = QueuedMessage(content="Hello")
|
||||
after = time.time()
|
||||
assert before <= msg.timestamp <= after
|
||||
|
||||
def test_message_priority(self):
|
||||
"""Should support priority levels."""
|
||||
from praisonai.cli.features.message_queue import QueuedMessage, MessagePriority
|
||||
msg = QueuedMessage(content="Hello", priority=MessagePriority.HIGH)
|
||||
assert msg.priority == MessagePriority.HIGH
|
||||
|
||||
def test_default_priority_normal(self):
|
||||
"""Should default to NORMAL priority."""
|
||||
from praisonai.cli.features.message_queue import QueuedMessage, MessagePriority
|
||||
msg = QueuedMessage(content="Hello")
|
||||
assert msg.priority == MessagePriority.NORMAL
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 4. MessageQueueHandler Tests
|
||||
# ============================================================================
|
||||
|
||||
class TestMessageQueueHandler:
|
||||
"""Test MessageQueueHandler main class."""
|
||||
|
||||
def test_handler_creation(self):
|
||||
"""Should create handler with default settings."""
|
||||
from praisonai.cli.features.message_queue import MessageQueueHandler
|
||||
handler = MessageQueueHandler()
|
||||
assert handler.queue.is_empty
|
||||
assert handler.state_manager.is_idle
|
||||
|
||||
def test_submit_when_idle(self):
|
||||
"""Should process immediately when idle."""
|
||||
from praisonai.cli.features.message_queue import MessageQueueHandler
|
||||
processed = []
|
||||
|
||||
def processor(msg):
|
||||
processed.append(msg)
|
||||
return f"Response to: {msg}"
|
||||
|
||||
handler = MessageQueueHandler(processor=processor)
|
||||
handler.submit("Hello")
|
||||
|
||||
# Should process immediately
|
||||
assert "Hello" in processed
|
||||
|
||||
def test_queue_when_processing(self):
|
||||
"""Should queue message when already processing."""
|
||||
from praisonai.cli.features.message_queue import MessageQueueHandler, ProcessingState
|
||||
|
||||
handler = MessageQueueHandler()
|
||||
handler.state_manager.set_state(ProcessingState.PROCESSING)
|
||||
handler.submit("Hello")
|
||||
|
||||
# Should be queued, not processed
|
||||
assert handler.queue.count == 1
|
||||
|
||||
def test_auto_process_queue_on_idle(self):
|
||||
"""Should auto-process queue when becoming idle."""
|
||||
from praisonai.cli.features.message_queue import MessageQueueHandler, ProcessingState
|
||||
processed = []
|
||||
|
||||
def processor(msg):
|
||||
processed.append(msg)
|
||||
return f"Response: {msg}"
|
||||
|
||||
handler = MessageQueueHandler(processor=processor)
|
||||
|
||||
# Simulate processing state
|
||||
handler.state_manager.set_state(ProcessingState.PROCESSING)
|
||||
handler.submit("Queued1")
|
||||
handler.submit("Queued2")
|
||||
|
||||
assert handler.queue.count == 2
|
||||
|
||||
# Simulate becoming idle - should trigger queue processing
|
||||
handler.on_processing_complete()
|
||||
|
||||
# Queue should be processed
|
||||
assert "Queued1" in processed or handler.queue.count < 2
|
||||
|
||||
def test_get_queue_status(self):
|
||||
"""Should return queue status."""
|
||||
from praisonai.cli.features.message_queue import MessageQueueHandler, ProcessingState
|
||||
handler = MessageQueueHandler()
|
||||
handler.state_manager.set_state(ProcessingState.PROCESSING)
|
||||
handler.submit("Hello")
|
||||
handler.submit("World")
|
||||
|
||||
status = handler.get_status()
|
||||
assert status['queue_count'] == 2
|
||||
assert status['state'] == 'processing'
|
||||
|
||||
def test_clear_queue(self):
|
||||
"""Should clear queue via handler."""
|
||||
from praisonai.cli.features.message_queue import MessageQueueHandler, ProcessingState
|
||||
handler = MessageQueueHandler()
|
||||
handler.state_manager.set_state(ProcessingState.PROCESSING)
|
||||
handler.submit("Hello")
|
||||
handler.clear_queue()
|
||||
assert handler.queue.is_empty
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 5. QueueDisplay Tests
|
||||
# ============================================================================
|
||||
|
||||
class TestQueueDisplay:
|
||||
"""Test QueueDisplay for visual indicators."""
|
||||
|
||||
def test_format_empty_queue(self):
|
||||
"""Should return empty string for empty queue."""
|
||||
from praisonai.cli.features.message_queue import QueueDisplay, MessageQueue
|
||||
queue = MessageQueue()
|
||||
display = QueueDisplay(queue)
|
||||
assert display.format_queue() == ""
|
||||
|
||||
def test_format_single_message(self):
|
||||
"""Should format single message."""
|
||||
from praisonai.cli.features.message_queue import QueueDisplay, MessageQueue
|
||||
queue = MessageQueue()
|
||||
queue.add("Hello world")
|
||||
display = QueueDisplay(queue)
|
||||
formatted = display.format_queue()
|
||||
assert "↳" in formatted
|
||||
assert "Hello world" in formatted
|
||||
|
||||
def test_format_multiple_messages(self):
|
||||
"""Should format multiple messages."""
|
||||
from praisonai.cli.features.message_queue import QueueDisplay, MessageQueue
|
||||
queue = MessageQueue()
|
||||
queue.add("First")
|
||||
queue.add("Second")
|
||||
display = QueueDisplay(queue)
|
||||
formatted = display.format_queue()
|
||||
assert "First" in formatted
|
||||
assert "Second" in formatted
|
||||
|
||||
def test_truncate_long_message(self):
|
||||
"""Should truncate long messages."""
|
||||
from praisonai.cli.features.message_queue import QueueDisplay, MessageQueue
|
||||
queue = MessageQueue()
|
||||
queue.add("A" * 100)
|
||||
display = QueueDisplay(queue, max_message_length=20)
|
||||
formatted = display.format_queue()
|
||||
assert "..." in formatted
|
||||
|
||||
def test_format_status_idle(self):
|
||||
"""Should show idle status."""
|
||||
from praisonai.cli.features.message_queue import QueueDisplay, MessageQueue, StateManager
|
||||
queue = MessageQueue()
|
||||
state = StateManager()
|
||||
display = QueueDisplay(queue, state_manager=state)
|
||||
status = display.format_status()
|
||||
assert "idle" in status.lower() or status == ""
|
||||
|
||||
def test_format_status_processing(self):
|
||||
"""Should show processing status."""
|
||||
from praisonai.cli.features.message_queue import (
|
||||
QueueDisplay, MessageQueue, StateManager, ProcessingState
|
||||
)
|
||||
queue = MessageQueue()
|
||||
state = StateManager()
|
||||
state.set_state(ProcessingState.PROCESSING)
|
||||
display = QueueDisplay(queue, state_manager=state)
|
||||
status = display.format_status()
|
||||
assert "processing" in status.lower() or "⏳" in status
|
||||
|
||||
def test_format_queue_count(self):
|
||||
"""Should show queue count."""
|
||||
from praisonai.cli.features.message_queue import QueueDisplay, MessageQueue, ProcessingState, StateManager
|
||||
queue = MessageQueue()
|
||||
queue.add("First")
|
||||
queue.add("Second")
|
||||
state = StateManager()
|
||||
state.set_state(ProcessingState.PROCESSING)
|
||||
display = QueueDisplay(queue, state_manager=state)
|
||||
count_str = display.format_queue_count()
|
||||
assert "2" in count_str
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 6. Integration Tests
|
||||
# ============================================================================
|
||||
|
||||
class TestMessageQueueIntegration:
|
||||
"""Integration tests for message queue system."""
|
||||
|
||||
def test_full_workflow(self):
|
||||
"""Test complete queue workflow."""
|
||||
from praisonai.cli.features.message_queue import (
|
||||
MessageQueueHandler, ProcessingState
|
||||
)
|
||||
|
||||
results = []
|
||||
|
||||
def processor(msg):
|
||||
results.append(msg)
|
||||
return f"Processed: {msg}"
|
||||
|
||||
handler = MessageQueueHandler(processor=processor)
|
||||
|
||||
# 1. Submit when idle - processes immediately
|
||||
handler.submit("First")
|
||||
assert "First" in results
|
||||
|
||||
# 2. Simulate processing
|
||||
handler.state_manager.set_state(ProcessingState.PROCESSING)
|
||||
|
||||
# 3. Queue messages while processing
|
||||
handler.submit("Second")
|
||||
handler.submit("Third")
|
||||
assert handler.queue.count == 2
|
||||
|
||||
# 4. Complete processing - should auto-process queue
|
||||
handler.on_processing_complete()
|
||||
|
||||
# Queue should be processed
|
||||
assert handler.queue.count < 2 or "Second" in results
|
||||
|
||||
def test_queue_with_display(self):
|
||||
"""Test queue with display formatting."""
|
||||
from praisonai.cli.features.message_queue import (
|
||||
MessageQueueHandler, QueueDisplay, ProcessingState
|
||||
)
|
||||
|
||||
handler = MessageQueueHandler()
|
||||
handler.state_manager.set_state(ProcessingState.PROCESSING)
|
||||
handler.submit("Task 1")
|
||||
handler.submit("Task 2")
|
||||
|
||||
display = QueueDisplay(handler.queue, state_manager=handler.state_manager)
|
||||
|
||||
# Should show processing status
|
||||
status = display.format_status()
|
||||
assert "processing" in status.lower() or "⏳" in status
|
||||
|
||||
# Should show queued messages
|
||||
queue_str = display.format_queue()
|
||||
assert "Task 1" in queue_str
|
||||
assert "Task 2" in queue_str
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 9. AsyncProcessor Tests - Background processing
|
||||
# ============================================================================
|
||||
|
||||
class TestAsyncProcessor:
|
||||
"""Test AsyncProcessor for background agent execution."""
|
||||
|
||||
def test_processor_creation(self):
|
||||
"""Should create processor with callback."""
|
||||
from praisonai.cli.features.message_queue import AsyncProcessor
|
||||
|
||||
results = []
|
||||
def on_complete(result):
|
||||
results.append(result)
|
||||
|
||||
processor = AsyncProcessor(on_complete=on_complete)
|
||||
assert processor is not None
|
||||
|
||||
def test_start_processing(self):
|
||||
"""Should start processing in background thread."""
|
||||
from praisonai.cli.features.message_queue import AsyncProcessor
|
||||
import time
|
||||
|
||||
results = []
|
||||
def work_fn():
|
||||
time.sleep(0.05)
|
||||
return "done"
|
||||
|
||||
def on_complete(result):
|
||||
results.append(result)
|
||||
|
||||
processor = AsyncProcessor(on_complete=on_complete)
|
||||
processor.start(work_fn)
|
||||
|
||||
# Should be processing
|
||||
assert processor.is_running
|
||||
|
||||
# Wait for completion
|
||||
time.sleep(0.15)
|
||||
assert not processor.is_running
|
||||
assert "done" in results
|
||||
|
||||
def test_is_running_property(self):
|
||||
"""Should track running state."""
|
||||
from praisonai.cli.features.message_queue import AsyncProcessor
|
||||
import time
|
||||
|
||||
def slow_work():
|
||||
time.sleep(0.1)
|
||||
return "result"
|
||||
|
||||
processor = AsyncProcessor()
|
||||
assert not processor.is_running
|
||||
|
||||
processor.start(slow_work)
|
||||
assert processor.is_running
|
||||
|
||||
time.sleep(0.15)
|
||||
assert not processor.is_running
|
||||
|
||||
def test_on_status_callback(self):
|
||||
"""Should call status callback during processing."""
|
||||
from praisonai.cli.features.message_queue import AsyncProcessor
|
||||
import time
|
||||
|
||||
statuses = []
|
||||
def on_status(status):
|
||||
statuses.append(status)
|
||||
|
||||
def work_with_status(status_callback):
|
||||
status_callback("Starting...")
|
||||
time.sleep(0.02)
|
||||
status_callback("Working...")
|
||||
return "done"
|
||||
|
||||
processor = AsyncProcessor(on_status=on_status)
|
||||
processor.start(lambda: work_with_status(on_status))
|
||||
|
||||
time.sleep(0.1)
|
||||
assert len(statuses) >= 1
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 10. LiveStatusDisplay Tests - Real-time status
|
||||
# ============================================================================
|
||||
|
||||
class TestLiveStatusDisplay:
|
||||
"""Test LiveStatusDisplay for real-time tool/command status."""
|
||||
|
||||
def test_display_creation(self):
|
||||
"""Should create display."""
|
||||
from praisonai.cli.features.message_queue import LiveStatusDisplay
|
||||
|
||||
display = LiveStatusDisplay()
|
||||
assert display is not None
|
||||
|
||||
def test_update_status(self):
|
||||
"""Should update current status."""
|
||||
from praisonai.cli.features.message_queue import LiveStatusDisplay
|
||||
|
||||
display = LiveStatusDisplay()
|
||||
display.update_status("Processing...")
|
||||
assert display.current_status == "Processing..."
|
||||
|
||||
def test_add_tool_call(self):
|
||||
"""Should track tool calls."""
|
||||
from praisonai.cli.features.message_queue import LiveStatusDisplay
|
||||
|
||||
display = LiveStatusDisplay()
|
||||
display.add_tool_call("read_file", {"path": "test.py"})
|
||||
|
||||
assert len(display.tool_calls) == 1
|
||||
assert display.tool_calls[0]['name'] == "read_file"
|
||||
|
||||
def test_add_command_execution(self):
|
||||
"""Should track command executions."""
|
||||
from praisonai.cli.features.message_queue import LiveStatusDisplay
|
||||
|
||||
display = LiveStatusDisplay()
|
||||
display.add_command("ls -la")
|
||||
|
||||
assert len(display.commands) == 1
|
||||
assert display.commands[0] == "ls -la"
|
||||
|
||||
def test_format_live_status(self):
|
||||
"""Should format live status for display."""
|
||||
from praisonai.cli.features.message_queue import LiveStatusDisplay
|
||||
|
||||
display = LiveStatusDisplay()
|
||||
display.update_status("Thinking...")
|
||||
display.add_tool_call("read_file", {"path": "main.py"})
|
||||
|
||||
formatted = display.format()
|
||||
assert "Thinking" in formatted or "read_file" in formatted
|
||||
|
||||
def test_clear_status(self):
|
||||
"""Should clear all status."""
|
||||
from praisonai.cli.features.message_queue import LiveStatusDisplay
|
||||
|
||||
display = LiveStatusDisplay()
|
||||
display.update_status("Working")
|
||||
display.add_tool_call("test", {})
|
||||
display.add_command("echo hi")
|
||||
|
||||
display.clear()
|
||||
|
||||
assert display.current_status == ""
|
||||
assert len(display.tool_calls) == 0
|
||||
assert len(display.commands) == 0
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 11. NonBlockingInput Tests - Async input handling
|
||||
# ============================================================================
|
||||
|
||||
class TestNonBlockingInput:
|
||||
"""Test NonBlockingInput for async user input."""
|
||||
|
||||
def test_input_handler_creation(self):
|
||||
"""Should create input handler."""
|
||||
from praisonai.cli.features.message_queue import NonBlockingInput
|
||||
|
||||
handler = NonBlockingInput()
|
||||
assert handler is not None
|
||||
|
||||
def test_submit_input(self):
|
||||
"""Should accept input while processing."""
|
||||
from praisonai.cli.features.message_queue import NonBlockingInput
|
||||
|
||||
handler = NonBlockingInput()
|
||||
handler.submit("new message")
|
||||
|
||||
assert handler.has_pending
|
||||
assert handler.pop() == "new message"
|
||||
|
||||
def test_multiple_inputs(self):
|
||||
"""Should queue multiple inputs."""
|
||||
from praisonai.cli.features.message_queue import NonBlockingInput
|
||||
|
||||
handler = NonBlockingInput()
|
||||
handler.submit("first")
|
||||
handler.submit("second")
|
||||
|
||||
assert handler.pending_count == 2
|
||||
assert handler.pop() == "first"
|
||||
assert handler.pop() == "second"
|
||||
@@ -163,11 +163,16 @@ class TestClaudeCodeSDKIntegration:
|
||||
assert isinstance(integration.sdk_available, bool)
|
||||
|
||||
def test_use_sdk_option(self):
|
||||
"""Test use_sdk option."""
|
||||
from praisonai.integrations.claude_code import ClaudeCodeIntegration
|
||||
"""Test use_sdk option.
|
||||
|
||||
Note: use_sdk is only True if both use_sdk=True AND SDK is available.
|
||||
Since SDK is not installed in test environment, use_sdk will be False.
|
||||
"""
|
||||
from praisonai.integrations.claude_code import ClaudeCodeIntegration, CLAUDE_SDK_AVAILABLE
|
||||
|
||||
integration = ClaudeCodeIntegration(use_sdk=True)
|
||||
assert integration.use_sdk is True
|
||||
# use_sdk is True only if SDK is available
|
||||
assert integration.use_sdk == (True and CLAUDE_SDK_AVAILABLE)
|
||||
|
||||
integration2 = ClaudeCodeIntegration(use_sdk=False)
|
||||
assert integration2.use_sdk is False
|
||||
@@ -0,0 +1,259 @@
|
||||
"""
|
||||
Unit tests for PraisonAI Profiler module.
|
||||
"""
|
||||
|
||||
import time
|
||||
|
||||
|
||||
class TestProfiler:
|
||||
"""Test Profiler class."""
|
||||
|
||||
def test_profiler_singleton(self):
|
||||
"""Should return same instance."""
|
||||
from praisonai.profiler import Profiler
|
||||
|
||||
p1 = Profiler()
|
||||
p2 = Profiler()
|
||||
assert p1 is p2
|
||||
|
||||
def test_enable_disable(self):
|
||||
"""Should enable and disable profiling."""
|
||||
from praisonai.profiler import Profiler
|
||||
|
||||
Profiler.disable()
|
||||
assert not Profiler.is_enabled()
|
||||
|
||||
Profiler.enable()
|
||||
assert Profiler.is_enabled()
|
||||
|
||||
Profiler.disable()
|
||||
|
||||
def test_record_timing(self):
|
||||
"""Should record timing."""
|
||||
from praisonai.profiler import Profiler
|
||||
|
||||
Profiler.enable()
|
||||
Profiler.clear()
|
||||
|
||||
Profiler.record_timing("test_op", 100.5, "test")
|
||||
|
||||
timings = Profiler.get_timings()
|
||||
assert len(timings) >= 1
|
||||
assert timings[-1].name == "test_op"
|
||||
assert timings[-1].duration_ms == 100.5
|
||||
|
||||
Profiler.disable()
|
||||
Profiler.clear()
|
||||
|
||||
def test_record_import(self):
|
||||
"""Should record import timing."""
|
||||
from praisonai.profiler import Profiler
|
||||
|
||||
Profiler.enable()
|
||||
Profiler.clear()
|
||||
|
||||
Profiler.record_import("test_module", 50.0)
|
||||
|
||||
imports = Profiler.get_imports()
|
||||
assert len(imports) >= 1
|
||||
assert imports[-1].module == "test_module"
|
||||
|
||||
Profiler.disable()
|
||||
Profiler.clear()
|
||||
|
||||
def test_block_context_manager(self):
|
||||
"""Should profile a block of code."""
|
||||
from praisonai.profiler import Profiler
|
||||
|
||||
Profiler.enable()
|
||||
Profiler.clear()
|
||||
|
||||
with Profiler.block("test_block"):
|
||||
time.sleep(0.01)
|
||||
|
||||
timings = Profiler.get_timings(category="block")
|
||||
assert len(timings) >= 1
|
||||
assert timings[-1].name == "test_block"
|
||||
assert timings[-1].duration_ms >= 10
|
||||
|
||||
Profiler.disable()
|
||||
Profiler.clear()
|
||||
|
||||
def test_get_summary(self):
|
||||
"""Should return summary."""
|
||||
from praisonai.profiler import Profiler
|
||||
|
||||
Profiler.enable()
|
||||
Profiler.clear()
|
||||
|
||||
Profiler.record_timing("op1", 100, "function")
|
||||
Profiler.record_timing("op2", 200, "function")
|
||||
Profiler.record_import("mod1", 50)
|
||||
|
||||
summary = Profiler.get_summary()
|
||||
|
||||
assert summary['total_time_ms'] == 300
|
||||
assert summary['import_time_ms'] == 50
|
||||
assert summary['timing_count'] == 2
|
||||
assert summary['import_count'] == 1
|
||||
|
||||
Profiler.disable()
|
||||
Profiler.clear()
|
||||
|
||||
|
||||
class TestProfileDecorator:
|
||||
"""Test @profile decorator."""
|
||||
|
||||
def test_profile_function(self):
|
||||
"""Should profile a function."""
|
||||
from praisonai.profiler import Profiler, profile
|
||||
|
||||
Profiler.enable()
|
||||
Profiler.clear()
|
||||
|
||||
@profile
|
||||
def test_func():
|
||||
time.sleep(0.01)
|
||||
return "result"
|
||||
|
||||
result = test_func()
|
||||
|
||||
assert result == "result"
|
||||
|
||||
timings = Profiler.get_timings(category="function")
|
||||
assert len(timings) >= 1
|
||||
assert timings[-1].name == "test_func"
|
||||
assert timings[-1].duration_ms >= 10
|
||||
|
||||
Profiler.disable()
|
||||
Profiler.clear()
|
||||
|
||||
def test_profile_with_category(self):
|
||||
"""Should profile with custom category."""
|
||||
from praisonai.profiler import Profiler, profile
|
||||
|
||||
Profiler.enable()
|
||||
Profiler.clear()
|
||||
|
||||
@profile(category="api")
|
||||
def api_call():
|
||||
return "response"
|
||||
|
||||
api_call()
|
||||
|
||||
timings = Profiler.get_timings(category="api")
|
||||
assert len(timings) >= 1
|
||||
assert timings[-1].name == "api_call"
|
||||
|
||||
Profiler.disable()
|
||||
Profiler.clear()
|
||||
|
||||
def test_profile_disabled(self):
|
||||
"""Should not record when disabled."""
|
||||
from praisonai.profiler import Profiler, profile
|
||||
|
||||
Profiler.disable()
|
||||
Profiler.clear()
|
||||
|
||||
@profile
|
||||
def test_func():
|
||||
return "result"
|
||||
|
||||
test_func()
|
||||
|
||||
timings = Profiler.get_timings()
|
||||
assert len(timings) == 0
|
||||
|
||||
|
||||
class TestImportProfiler:
|
||||
"""Test ImportProfiler."""
|
||||
|
||||
def test_profile_imports(self):
|
||||
"""Should profile imports."""
|
||||
from praisonai.profiler import profile_imports
|
||||
|
||||
with profile_imports() as profiler:
|
||||
import json
|
||||
import re
|
||||
|
||||
imports = profiler.get_imports()
|
||||
# May or may not have imports depending on cache
|
||||
assert isinstance(imports, list)
|
||||
|
||||
def test_get_slowest(self):
|
||||
"""Should get slowest imports."""
|
||||
from praisonai.profiler import profile_imports
|
||||
|
||||
with profile_imports() as profiler:
|
||||
import collections
|
||||
|
||||
slowest = profiler.get_slowest(5)
|
||||
assert isinstance(slowest, list)
|
||||
assert len(slowest) <= 5
|
||||
|
||||
|
||||
class TestUtilityFunctions:
|
||||
"""Test utility functions."""
|
||||
|
||||
def test_check_module_available(self):
|
||||
"""Should check module availability."""
|
||||
from praisonai.profiler import check_module_available
|
||||
|
||||
assert check_module_available("os") is True
|
||||
assert check_module_available("nonexistent_module_xyz") is False
|
||||
|
||||
def test_time_import(self):
|
||||
"""Should time import."""
|
||||
from praisonai.profiler import time_import
|
||||
|
||||
duration = time_import("json")
|
||||
assert isinstance(duration, float)
|
||||
assert duration >= 0
|
||||
|
||||
|
||||
class TestDataClasses:
|
||||
"""Test data classes."""
|
||||
|
||||
def test_timing_record(self):
|
||||
"""Should create TimingRecord."""
|
||||
from praisonai.profiler import TimingRecord
|
||||
|
||||
record = TimingRecord(
|
||||
name="test",
|
||||
duration_ms=100.0,
|
||||
category="function",
|
||||
file="test.py",
|
||||
line=10
|
||||
)
|
||||
|
||||
assert record.name == "test"
|
||||
assert record.duration_ms == 100.0
|
||||
assert record.category == "function"
|
||||
|
||||
def test_import_record(self):
|
||||
"""Should create ImportRecord."""
|
||||
from praisonai.profiler import ImportRecord
|
||||
|
||||
record = ImportRecord(
|
||||
module="test_module",
|
||||
duration_ms=50.0,
|
||||
parent="parent_module"
|
||||
)
|
||||
|
||||
assert record.module == "test_module"
|
||||
assert record.duration_ms == 50.0
|
||||
|
||||
def test_flow_record(self):
|
||||
"""Should create FlowRecord."""
|
||||
from praisonai.profiler import FlowRecord
|
||||
|
||||
record = FlowRecord(
|
||||
step=1,
|
||||
name="step1",
|
||||
file="test.py",
|
||||
line=20,
|
||||
duration_ms=30.0
|
||||
)
|
||||
|
||||
assert record.step == 1
|
||||
assert record.name == "step1"
|
||||
@@ -161,20 +161,26 @@ class TestHandleDirectPromptWithRewrite:
|
||||
llm=None
|
||||
)
|
||||
|
||||
# Mock the agent and its start method
|
||||
mock_agent = MagicMock()
|
||||
mock_agent.start.return_value = "result"
|
||||
|
||||
with patch.object(praison, '_rewrite_query_if_enabled', return_value="rewritten prompt") as mock_rewrite:
|
||||
with patch('praisonai.cli.main.PRAISONAI_AVAILABLE', True):
|
||||
with patch('praisonai.cli.main.PraisonAgent') as mock_agent_class:
|
||||
mock_agent = MagicMock()
|
||||
mock_agent.start.return_value = "result"
|
||||
mock_agent_class.return_value = mock_agent
|
||||
with patch.object(praison, '_run_with_status_display', return_value="result") as mock_run:
|
||||
with patch('praisonai.cli.main.PRAISONAI_AVAILABLE', True):
|
||||
# Mock praisonaiagents and all its submodules
|
||||
mock_praisonaiagents = MagicMock()
|
||||
mock_praisonaiagents.Agent = MagicMock(return_value=mock_agent)
|
||||
mock_praisonaiagents.approval = MagicMock()
|
||||
|
||||
praison.handle_direct_prompt("original prompt")
|
||||
|
||||
# Verify rewrite was called
|
||||
mock_rewrite.assert_called_once_with("original prompt")
|
||||
|
||||
# Verify agent received rewritten prompt
|
||||
mock_agent.start.assert_called_once_with("rewritten prompt")
|
||||
with patch.dict('sys.modules', {
|
||||
'praisonaiagents': mock_praisonaiagents,
|
||||
'praisonaiagents.approval': mock_praisonaiagents.approval
|
||||
}):
|
||||
praison.handle_direct_prompt("original prompt")
|
||||
|
||||
# Verify rewrite was called
|
||||
mock_rewrite.assert_called_once_with("original prompt")
|
||||
|
||||
|
||||
class TestToolLoading:
|
||||
|
||||
Generated
+4
-4
@@ -4197,7 +4197,7 @@ requires-dist = [
|
||||
{ name = "praisonai-tools", marker = "extra == 'autogen'", specifier = ">=0.0.22" },
|
||||
{ name = "praisonai-tools", marker = "extra == 'autogen-v4'", specifier = ">=0.0.22" },
|
||||
{ name = "praisonai-tools", marker = "extra == 'crewai'", specifier = ">=0.0.22" },
|
||||
{ name = "praisonaiagents", specifier = ">=0.1.17" },
|
||||
{ name = "praisonaiagents", specifier = ">=0.1.21" },
|
||||
{ name = "pyautogen", marker = "extra == 'autogen'", specifier = "==0.2.29" },
|
||||
{ name = "pydantic", marker = "extra == 'chat'", specifier = "<=2.10.1" },
|
||||
{ name = "pydantic", marker = "extra == 'code'", specifier = "<=2.10.1" },
|
||||
@@ -4252,7 +4252,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "praisonaiagents"
|
||||
version = "0.1.17"
|
||||
version = "0.1.21"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "aiohttp" },
|
||||
@@ -4261,9 +4261,9 @@ dependencies = [
|
||||
{ name = "pydantic" },
|
||||
{ name = "rich" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/3c/ed/fd26940dd2ee4506f2d6e178841b36d09f90f6a817ea052e0c1a9473f132/praisonaiagents-0.1.17.tar.gz", hash = "sha256:fa3bb9e77c6dbd16ed1fdb933d3669fac2208b1e58a2a5d5986fd630fef975c1", size = 647705, upload-time = "2025-12-18T21:52:30.244Z" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/44/9a/c5b74fdfafc3557107f5c5dc35978d818537f35047f76e2a50142a2401bd/praisonaiagents-0.1.21.tar.gz", hash = "sha256:09b9448f4aa8c88d2ff3cab6514b5746ff62cbe07db97541a467ac1eac38777f", size = 658730, upload-time = "2025-12-20T11:57:22.359Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/94/40/261bbe4e0597442bd8d3aef9450c315063735970bb8f32f95cc4f0267b7b/praisonaiagents-0.1.17-py3-none-any.whl", hash = "sha256:61b98ede790e14fd7a21f4fc51729ba0a4b32ab1165f3ad9ee966757fa581c0f", size = 583244, upload-time = "2025-12-18T21:52:28.004Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/89/0a/c408a92c1721bc7a15e1c6dffc099381411fc87c241fba09d60c91a74d47/praisonaiagents-0.1.21-py3-none-any.whl", hash = "sha256:9d77c484013a1ea7c52ad6a9a0750c0c336133f6e913da6b9bd3da73f2bb5b11", size = 589189, upload-time = "2025-12-20T11:57:20.651Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
Reference in new issue
Block a user