mirror of
https://github.com/MervinPraison/PraisonAI.git
synced 2026-11-09 11:51:37 -10:00
Compare commits
305
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92542b7c3c | ||
|
|
041c3d5897 | ||
|
|
e890aade27 | ||
|
|
4d262fc832 | ||
|
|
c31c5b9fc7 | ||
|
|
45419c40d8 | ||
|
|
9df792b218 | ||
|
|
0526cdc528 | ||
|
|
e6f3438cfa | ||
|
|
c87dbf6330 | ||
|
|
0a9b7cd137 | ||
|
|
3f69c77577 | ||
|
|
f30b2b78c8 | ||
|
|
08581d68d4 | ||
|
|
5bdf515ac3 | ||
|
|
b02204e711 | ||
|
|
10e25b550d | ||
|
|
03d7b6ad11 | ||
|
|
3dd0bc877c | ||
|
|
037b2197f9 | ||
|
|
17968d7cc1 | ||
|
|
17288679cc | ||
|
|
01859f40da | ||
|
|
dda4822e4c | ||
|
|
a14ca673da | ||
|
|
02d9d1e04f | ||
|
|
d3ee549ccc | ||
|
|
63fbd04af3 | ||
|
|
51f95ad904 | ||
|
|
44b800df0e | ||
|
|
6e3a99b8b6 | ||
|
|
b7d53b8f11 | ||
|
|
643fac5762 | ||
|
|
92b71cbc8f | ||
|
|
d7555ba1b2 | ||
|
|
22dd13ec66 | ||
|
|
ba40e818e4 | ||
|
|
12f829321c | ||
|
|
149ed5bd94 | ||
|
|
15f2937dd5 | ||
|
|
d28199a18d | ||
|
|
0d3d6bb852 | ||
|
|
5f93f83c9f | ||
|
|
c9786fbd1e | ||
|
|
95e8e38998 | ||
|
|
73f8961e61 | ||
|
|
4caf10a982 | ||
|
|
fb42a1010a | ||
|
|
fe60dd40af | ||
|
|
7f37d754a7 | ||
|
|
26f60fb056 | ||
|
|
0f0b640184 | ||
|
|
d857dfc6b7 | ||
|
|
0135a9e09f | ||
|
|
847b5e6372 | ||
|
|
05a7265c4e | ||
|
|
de1734c29a | ||
|
|
f0e91b11ae | ||
|
|
6e1a9143e2 | ||
|
|
10d76d3a85 | ||
|
|
a38a9c06c6 | ||
|
|
9c90416ab5 | ||
|
|
efa5247344 | ||
|
|
720213d82d | ||
|
|
f6af16ef5e | ||
|
|
8439b98ec2 | ||
|
|
92271fc118 | ||
|
|
1b43dc6a4b | ||
|
|
929cefa55e | ||
|
|
225843e39b | ||
|
|
77913183a9 | ||
|
|
e5b5d747f4 | ||
|
|
8cb779c51c | ||
|
|
020ca46c90 | ||
|
|
557e243eed | ||
|
|
0628204fce | ||
|
|
2a11bf5221 | ||
|
|
ecc2182d24 | ||
|
|
c6b91b2d57 | ||
|
|
a5ed17945f | ||
|
|
ff3ca3052d | ||
|
|
6f1f4a5363 | ||
|
|
dd9dfa5970 | ||
|
|
5f4fc8fa2f | ||
|
|
dd9dd5d5d9 | ||
|
|
d2c629b82c | ||
|
|
609ac5f7a6 | ||
|
|
75c72e909e | ||
|
|
9492ff8de1 | ||
|
|
a0a3aa1a53 | ||
|
|
6a20001551 | ||
|
|
577295c147 | ||
|
|
06f64a551c | ||
|
|
928b824180 | ||
|
|
1631973256 | ||
|
|
a9d7f6fd76 | ||
|
|
cc5360e60e | ||
|
|
863dd9bc34 | ||
|
|
8c39769971 | ||
|
|
93904f8530 | ||
|
|
ffc3775446 | ||
|
|
86e76b7a3e | ||
|
|
b18d6007c2 | ||
|
|
c656af3166 | ||
|
|
3c2fff05dd | ||
|
|
092097faa6 | ||
|
|
068ddecb91 | ||
|
|
444d14cb54 | ||
|
|
090316dce3 | ||
|
|
a40cddd402 | ||
|
|
62472a2342 | ||
|
|
35186a0e66 | ||
|
|
d08d98ca12 | ||
|
|
2c9e8b46cb | ||
|
|
ce3b25c085 | ||
|
|
a928467188 | ||
|
|
8285afe789 | ||
|
|
b529110246 | ||
|
|
2a004bc382 | ||
|
|
b3fecacb2b | ||
|
|
6924077e36 | ||
|
|
63101d7323 | ||
|
|
e9540b2a92 | ||
|
|
75c1c62cec | ||
|
|
de4f251a02 | ||
|
|
d4858b4d5c | ||
|
|
03ca92a7d5 | ||
|
|
cca65dcdab | ||
|
|
93844d9509 | ||
|
|
0849db227d | ||
|
|
914d34e57b | ||
|
|
be66a5e4e1 | ||
|
|
20a19f7958 | ||
|
|
4128a67e10 | ||
|
|
9d29c796c7 | ||
|
|
f058ce018f | ||
|
|
09671d4bd3 | ||
|
|
c1dead194c | ||
|
|
5d67f0fdcf | ||
|
|
fb93b7d6d5 | ||
|
|
657fd93743 | ||
|
|
f0a2936b7f | ||
|
|
58778492ca | ||
|
|
08c444f3a8 | ||
|
|
34c1b39e95 | ||
|
|
5e4c58a9ac | ||
|
|
19a30ff3d9 | ||
|
|
50f12c7682 | ||
|
|
5a70b8abb9 | ||
|
|
b7d4d219b9 | ||
|
|
ff2d83ffdf | ||
|
|
2d58caa741 | ||
|
|
d31cf379e5 | ||
|
|
719ef04e7c | ||
|
|
4fb25a99d5 | ||
|
|
0fdacff2cf | ||
|
|
6a1fa19c6e | ||
|
|
a4725a027a | ||
|
|
dc9a8f3443 | ||
|
|
9421e28c8a | ||
|
|
c3d60c82ca | ||
|
|
8c509b439f | ||
|
|
3fbf1a353e | ||
|
|
e0f3a49e41 | ||
|
|
987b43877f | ||
|
|
ebb383208e | ||
|
|
c9e4f0738a | ||
|
|
db0a24555d | ||
|
|
e3fe35e2b6 | ||
|
|
74a7322b83 | ||
|
|
7ceb7f8cb9 | ||
|
|
84e5b6c693 | ||
|
|
5f00ddbc7c | ||
|
|
db551c5d65 | ||
|
|
aed6027256 | ||
|
|
a9d709645a | ||
|
|
a430e1b873 | ||
|
|
8e0a40db56 | ||
|
|
8d1e4637d4 | ||
|
|
e11d62dc89 | ||
|
|
13a9a3e07d | ||
|
|
b95f6b0665 | ||
|
|
e8c4410014 | ||
|
|
6c2d852ed1 | ||
|
|
6f95ae8d9d | ||
|
|
8e2dd09b3b | ||
|
|
8ecfad5c5a | ||
|
|
35a88a0913 | ||
|
|
6df3e32640 | ||
|
|
fb36e1c50c | ||
|
|
ac5454cb00 | ||
|
|
bbe95795d1 | ||
|
|
bdaf5340f9 | ||
|
|
5579614001 | ||
|
|
2c4dd0e2d4 | ||
|
|
fd23d62841 | ||
|
|
5f3bcb78ac | ||
|
|
3c40c2278e | ||
|
|
1921b03aab | ||
|
|
f33bf57662 | ||
|
|
df21c419ea | ||
|
|
fe997d525c | ||
|
|
ca26988774 | ||
|
|
3600594316 | ||
|
|
097cd42890 | ||
|
|
595add2991 | ||
|
|
03a3aa420b | ||
|
|
9533abd8a4 | ||
|
|
39c9e50875 | ||
|
|
585b68d02c | ||
|
|
66481cc51c | ||
|
|
8e637daecd | ||
|
|
ba482895f7 | ||
|
|
ba09407981 | ||
|
|
b04bfaf5be | ||
|
|
abea33d2fb | ||
|
|
6f8f6bcc86 | ||
|
|
e823dad0c3 | ||
|
|
8f6cd62a9f | ||
|
|
f5850c3ff9 | ||
|
|
31e2f03e07 | ||
|
|
c53c33738d | ||
|
|
efed927736 | ||
|
|
cf6a86854e | ||
|
|
ba054700d5 | ||
|
|
83fb3f4c62 | ||
|
|
b009a0c49d | ||
|
|
91c33594f4 | ||
|
|
c54a9e12f5 | ||
|
|
74ceda77ef | ||
|
|
8aa45391af | ||
|
|
722056fd76 | ||
|
|
8860b31282 | ||
|
|
f5e17c697b | ||
|
|
d3f6eb4096 | ||
|
|
c691b0a4c0 | ||
|
|
b59675eeb4 | ||
|
|
7e0739c6c4 | ||
|
|
7519bc85d2 | ||
|
|
c7471092d0 | ||
|
|
c344ee1bad | ||
|
|
77bbf1ae66 | ||
|
|
695d4d8843 | ||
|
|
08f0736db5 | ||
|
|
1e46093edf | ||
|
|
1a71aac6b8 | ||
|
|
287f42dfee | ||
|
|
57b637948f | ||
|
|
757d402cae | ||
|
|
412783c5ed | ||
|
|
edbd122414 | ||
|
|
0ac055cba5 | ||
|
|
134327a9cc | ||
|
|
17c5942a02 | ||
|
|
951eb6b497 | ||
|
|
1d0f08b20a | ||
|
|
07ff93ed7f | ||
|
|
2d5c982893 | ||
|
|
f5d4db4262 | ||
|
|
87d691c616 | ||
|
|
8ea15dee0f | ||
|
|
4f56842e08 | ||
|
|
a24836aaee | ||
|
|
be9ce27615 | ||
|
|
0159a1e082 | ||
|
|
71a5076c07 | ||
|
|
9815a9f8a8 | ||
|
|
2ae44a05b0 | ||
|
|
3386d25022 | ||
|
|
6dca2ace8a | ||
|
|
63548739e8 | ||
|
|
3e046537f4 | ||
|
|
4a34677567 | ||
|
|
19e91cb669 | ||
|
|
5a6125021a | ||
|
|
da26887f24 | ||
|
|
1b2f7139f9 | ||
|
|
aafbc86180 | ||
|
|
3d4ca1b7c0 | ||
|
|
1180aa38bd | ||
|
|
65f65e5c7b | ||
|
|
f81a12d5e8 | ||
|
|
cf6f5fbefe | ||
|
|
908d7710db | ||
|
|
7d5b41e31f | ||
|
|
e46a0bc9f3 | ||
|
|
911483c92c | ||
|
|
4ffb9d6e3f | ||
|
|
b686e5b00f | ||
|
|
d8d84ee3e3 | ||
|
|
9f2c4d10b5 | ||
|
|
42a86c831f | ||
|
|
48443b11d6 | ||
|
|
71745aa14a | ||
|
|
4f309ec16d | ||
|
|
e48afc8993 | ||
|
|
90950f8889 | ||
|
|
56f36e2593 | ||
|
|
7aa67ad27c | ||
|
|
307df443ef | ||
|
|
6434a87b60 | ||
|
|
97d5b6db0d | ||
|
|
4996a9f9fa | ||
|
|
54f4a96cba | ||
|
|
e59be8f0fd |
No files matched your search
@@ -1,58 +0,0 @@
|
||||
name: Update Feature Parity Tracker
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "main" ]
|
||||
paths:
|
||||
- 'src/praisonai-agents/praisonaiagents/**/*.py'
|
||||
- 'src/praisonai-ts/src/**/*.ts'
|
||||
- 'src/praisonai-rust/src/**/*.rs'
|
||||
- '.github/workflows/update-parity-tracker.yml'
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
update-parity-tracker:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: main
|
||||
persist-credentials: true
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.11'
|
||||
|
||||
- name: Generate parity trackers
|
||||
run: |
|
||||
cd $GITHUB_WORKSPACE
|
||||
python3 src/praisonai/scripts/generate_parity_tracker.py
|
||||
|
||||
- name: Check for changes
|
||||
id: check_changes
|
||||
run: |
|
||||
if git diff --quiet src/praisonai-ts/FEATURE_PARITY_TRACKER.json src/praisonai-ts/PARITY.md src/praisonai-rust/FEATURE_PARITY_TRACKER.json src/praisonai-rust/PARITY.md 2>/dev/null; then
|
||||
echo "changes=false" >> $GITHUB_OUTPUT
|
||||
echo "No changes to parity trackers"
|
||||
else
|
||||
echo "changes=true" >> $GITHUB_OUTPUT
|
||||
echo "Parity trackers have been updated"
|
||||
git diff src/praisonai-ts/FEATURE_PARITY_TRACKER.json | head -50
|
||||
fi
|
||||
|
||||
- name: Commit and push changes
|
||||
if: steps.check_changes.outputs.changes == 'true'
|
||||
uses: stefanzweifel/git-auto-commit-action@v5
|
||||
with:
|
||||
commit_message: "docs: auto-update feature parity trackers [skip ci]"
|
||||
file_pattern: 'src/praisonai-ts/FEATURE_PARITY_TRACKER.json src/praisonai-ts/PARITY.md src/praisonai-rust/FEATURE_PARITY_TRACKER.json src/praisonai-rust/PARITY.md'
|
||||
commit_user_name: "github-actions[bot]"
|
||||
commit_user_email: "41898282+github-actions[bot]@users.noreply.github.com"
|
||||
commit_author: "github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"
|
||||
|
||||
+1
-8
@@ -84,11 +84,4 @@ issue_details.json
|
||||
.mcpregistry_*
|
||||
|
||||
*.log
|
||||
.internal
|
||||
.praisonai
|
||||
|
||||
# Rust / Cargo
|
||||
target/
|
||||
Cargo.lock
|
||||
**/*.rs.bk
|
||||
*.pdb
|
||||
.internal
|
||||
@@ -58,7 +58,6 @@ Methods:
|
||||
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">generate_task</a>() -> 'Task'</code>
|
||||
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">get_available_tools</a>() -> List[Any]</code>
|
||||
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">get_history_size</a>() -> int</code>
|
||||
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">get_learn_context</a>() -> str</code>
|
||||
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">get_memory_context</a>(query: Optional[str] = None) -> str</code>
|
||||
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">get_recommended_stage</a>(prompt: str) -> str</code>
|
||||
* <code title="class Agent">Agent.<a href="./src/praisonai-agents/praisonaiagents/agent/agent.py">get_rules_context</a>(file_path: Optional[str] = None, include_manual: Optional[List[str]] = None) -> str</code>
|
||||
@@ -576,7 +575,7 @@ Methods:
|
||||
|
||||
Types:
|
||||
```python
|
||||
from praisonaiagents import ContextManager
|
||||
from praisonaiagents import ContextManager, ManagerConfig
|
||||
```
|
||||
|
||||
Methods:
|
||||
@@ -594,16 +593,20 @@ Methods:
|
||||
* <code title="class ContextManager">ContextManager.<a href="./src/praisonai-agents/praisonaiagents/context/manager.py">reset</a>() -> None</code>
|
||||
* <code title="class ContextManager">ContextManager.<a href="./src/praisonai-agents/praisonaiagents/context/manager.py">set_tool_budget</a>(tool_name: str, max_tokens: int, protected: bool = False) -> None</code>
|
||||
* <code title="class ContextManager">ContextManager.<a href="./src/praisonai-agents/praisonaiagents/context/manager.py">truncate_tool_output</a>(tool_name: str, output: str) -> str</code>
|
||||
* <code title="class ManagerConfig">ManagerConfig.<a href="./src/praisonai-agents/praisonaiagents/context/manager.py">from_env</a>() -> 'ManagerConfig'</code>
|
||||
* <code title="class ManagerConfig">ManagerConfig.<a href="./src/praisonai-agents/praisonaiagents/context/manager.py">merge</a>(**overrides) -> 'ManagerConfig'</code>
|
||||
* <code title="class ManagerConfig">ManagerConfig.<a href="./src/praisonai-agents/praisonaiagents/context/manager.py">to_dict</a>() -> Dict[str, Any]</code>
|
||||
|
||||
# Config
|
||||
|
||||
Types:
|
||||
```python
|
||||
from praisonaiagents import AutonomyLevel, CachingConfig, ExecutionConfig, ExecutionPreset, GuardrailConfig, HooksConfig, KnowledgeConfig, MemoryConfig, MultiAgentExecutionConfig, MultiAgentHooksConfig, MultiAgentMemoryConfig, MultiAgentOutputConfig, MultiAgentPlanningConfig, OutputConfig, OutputPreset, PlanningConfig, ReflectionConfig, SkillsConfig, TemplateConfig, WebConfig, WebSearchProvider
|
||||
from praisonaiagents import AutonomyConfig, AutonomyLevel, CachingConfig, ExecutionConfig, ExecutionPreset, GuardrailConfig, HooksConfig, KnowledgeConfig, MemoryConfig, MultiAgentExecutionConfig, MultiAgentHooksConfig, MultiAgentMemoryConfig, MultiAgentOutputConfig, MultiAgentPlanningConfig, OutputConfig, OutputPreset, PlanningConfig, ReflectionConfig, SkillsConfig, TemplateConfig, WebConfig, WebSearchProvider
|
||||
```
|
||||
|
||||
Methods:
|
||||
|
||||
* <code title="class AutonomyConfig">AutonomyConfig.<a href="./src/praisonai-agents/praisonaiagents/config/feature_configs.py">to_dict</a>() -> Dict[str, Any]</code>
|
||||
* <code title="class CachingConfig">CachingConfig.<a href="./src/praisonai-agents/praisonaiagents/config/feature_configs.py">to_dict</a>() -> Dict[str, Any]</code>
|
||||
* <code title="class ExecutionConfig">ExecutionConfig.<a href="./src/praisonai-agents/praisonaiagents/config/feature_configs.py">to_dict</a>() -> Dict[str, Any]</code>
|
||||
* <code title="class GuardrailConfig">GuardrailConfig.<a href="./src/praisonai-agents/praisonaiagents/config/feature_configs.py">to_dict</a>() -> Dict[str, Any]</code>
|
||||
|
||||
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=4.4.4" \
|
||||
"praisonai>=4.4.1" \
|
||||
"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>=4.4.4" \
|
||||
"praisonai>=4.4.1" \
|
||||
"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>=4.4.4" \
|
||||
"praisonai>=4.4.1" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[crewai]"
|
||||
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
# EvaluationLoop Examples
|
||||
|
||||
Examples demonstrating iterative evaluation and improvement patterns.
|
||||
|
||||
## Quick Start
|
||||
|
||||
```bash
|
||||
pip install praisonaiagents
|
||||
export OPENAI_API_KEY=your-key
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
| File | Description |
|
||||
|------|-------------|
|
||||
| `basic_evaluation_loop.py` | Simple loop: Run → Judge → Improve |
|
||||
| `multi_step_task_loop.py` | Sequential tasks with context passing |
|
||||
| `recipe_optimization_loop.py` | CLI-based recipe optimization |
|
||||
|
||||
## Pattern
|
||||
|
||||
```
|
||||
┌─────────┐ ┌─────────┐ ┌──────────┐
|
||||
│ Agent │ ──► │ Judge │ ──► │ Feedback │
|
||||
│ runs │ │ scores │ │ improves │
|
||||
└─────────┘ └─────────┘ └──────────┘
|
||||
▲ │
|
||||
└───────────────────────────────┘
|
||||
(repeat until 8.0+)
|
||||
```
|
||||
|
||||
## Run Examples
|
||||
|
||||
```bash
|
||||
# Basic loop
|
||||
python basic_evaluation_loop.py
|
||||
|
||||
# Multi-step with context
|
||||
python multi_step_task_loop.py
|
||||
|
||||
# CLI recipe loop
|
||||
python recipe_optimization_loop.py "research AI trends"
|
||||
```
|
||||
@@ -1,73 +0,0 @@
|
||||
"""
|
||||
Basic EvaluationLoop Example
|
||||
|
||||
Demonstrates the iterative evaluation loop pattern:
|
||||
Run agent → Judge output → Improve → Repeat until threshold met.
|
||||
|
||||
Usage:
|
||||
pip install praisonaiagents
|
||||
export OPENAI_API_KEY=your-key
|
||||
python basic_evaluation_loop.py
|
||||
"""
|
||||
from praisonaiagents import Agent
|
||||
from praisonaiagents.eval import EvaluationLoop
|
||||
|
||||
|
||||
def on_iteration(result):
|
||||
"""Callback showing progress after each iteration."""
|
||||
status = "✅" if result.score >= 8.0 else "⚠️" if result.score >= 6.0 else "❌"
|
||||
print(f"{status} Iteration {result.iteration}: {result.score:.1f}/10")
|
||||
if result.findings:
|
||||
print(f" → {result.findings[0][:60]}...")
|
||||
|
||||
|
||||
def main():
|
||||
# Create an agent
|
||||
agent = Agent(
|
||||
name="writer",
|
||||
instructions="You are a technical content writer. Write clear, accurate explanations."
|
||||
)
|
||||
|
||||
# Run evaluation loop
|
||||
loop = EvaluationLoop(
|
||||
agent=agent,
|
||||
criteria="""
|
||||
Evaluate the response on:
|
||||
1. CLARITY: Is the explanation easy to understand?
|
||||
2. ACCURACY: Is the technical content correct?
|
||||
3. COMPLETENESS: Does it cover the key aspects?
|
||||
4. EXAMPLES: Does it include practical examples?
|
||||
|
||||
Score 1-10 based on all criteria.
|
||||
""",
|
||||
threshold=8.0,
|
||||
max_iterations=5,
|
||||
on_iteration=on_iteration,
|
||||
verbose=False,
|
||||
)
|
||||
|
||||
print("╔════════════════════════════════════════════════════════════╗")
|
||||
print("║ Basic EvaluationLoop Example ║")
|
||||
print("║ Pattern: Run → Judge → Improve → Repeat ║")
|
||||
print("║ Target: 8.0/10 score ║")
|
||||
print("╚════════════════════════════════════════════════════════════╝\n")
|
||||
|
||||
result = loop.run("Explain how API rate limiting works and why it matters")
|
||||
|
||||
print("\n" + "─" * 60)
|
||||
print("RESULTS:")
|
||||
print("─" * 60)
|
||||
print(f"Success: {result.success}")
|
||||
print(f"Final Score: {result.final_score:.1f}/10")
|
||||
print(f"Iterations: {result.num_iterations}")
|
||||
print(f"Score History: {[f'{s:.1f}' for s in result.score_history]}")
|
||||
print(f"Duration: {result.total_duration_seconds:.2f}s")
|
||||
|
||||
print("\n" + "─" * 60)
|
||||
print("FINAL OUTPUT (first 500 chars):")
|
||||
print("─" * 60)
|
||||
print(result.final_output[:500] + "..." if len(result.final_output) > 500 else result.final_output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,144 +0,0 @@
|
||||
"""
|
||||
Multi-Step Task Loop Example
|
||||
|
||||
Demonstrates evaluating sequential task steps where each step can
|
||||
reference output from previous steps.
|
||||
|
||||
Pattern:
|
||||
1. Research → Evaluate → Improve
|
||||
2. Analyze (using research) → Evaluate → Improve
|
||||
3. Report (using analysis) → Evaluate → Improve
|
||||
|
||||
Usage:
|
||||
pip install praisonaiagents
|
||||
export OPENAI_API_KEY=your-key
|
||||
python multi_step_task_loop.py
|
||||
"""
|
||||
from praisonaiagents import Agent
|
||||
from praisonaiagents.eval import EvaluationLoop
|
||||
|
||||
|
||||
def on_iteration(result):
|
||||
"""Show iteration progress."""
|
||||
status = "✅" if result.score >= 8.0 else "⚠️" if result.score >= 6.0 else "❌"
|
||||
print(f" {status} Iteration {result.iteration}: {result.score:.1f}/10")
|
||||
|
||||
|
||||
def run_step(step_name: str, agent: Agent, task: str, criteria: str, context: str = "") -> dict:
|
||||
"""Run a single step through an evaluation loop."""
|
||||
print(f"\n{'─'*60}")
|
||||
print(f"STEP: {step_name}")
|
||||
print(f"{'─'*60}")
|
||||
|
||||
# Include context from previous step if provided
|
||||
full_task = task
|
||||
if context:
|
||||
full_task = f"{task}\n\nContext from previous step:\n{context[:1000]}"
|
||||
|
||||
loop = EvaluationLoop(
|
||||
agent=agent,
|
||||
criteria=criteria,
|
||||
threshold=8.0,
|
||||
max_iterations=3,
|
||||
on_iteration=on_iteration,
|
||||
verbose=False,
|
||||
)
|
||||
|
||||
result = loop.run(full_task)
|
||||
|
||||
print(f" → Final: {result.final_score:.1f}/10 ({result.num_iterations} iterations)")
|
||||
|
||||
return {
|
||||
"name": step_name,
|
||||
"output": result.final_output,
|
||||
"score": result.final_score,
|
||||
"success": result.success,
|
||||
"iterations": result.num_iterations,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
print("╔════════════════════════════════════════════════════════════╗")
|
||||
print("║ Multi-Step Task Loop Example ║")
|
||||
print("║ Pattern: Step1 → Step2 → Step3 (with context passing) ║")
|
||||
print("║ Each step: Run → Judge → Improve until 8.0+ ║")
|
||||
print("╚════════════════════════════════════════════════════════════╝")
|
||||
|
||||
# Create specialized agents
|
||||
researcher = Agent(
|
||||
name="researcher",
|
||||
instructions="You are a research specialist. Find and summarize key information."
|
||||
)
|
||||
|
||||
analyst = Agent(
|
||||
name="analyst",
|
||||
instructions="You are an analyst. Identify patterns, insights, and implications."
|
||||
)
|
||||
|
||||
writer = Agent(
|
||||
name="writer",
|
||||
instructions="You are a report writer. Create clear, actionable summaries."
|
||||
)
|
||||
|
||||
# Define task steps
|
||||
steps = [
|
||||
{
|
||||
"name": "Research",
|
||||
"agent": researcher,
|
||||
"task": "Research the top 3 trends in AI for 2024-2025. Provide brief descriptions.",
|
||||
"criteria": "Covers 3 distinct trends with accurate descriptions.",
|
||||
},
|
||||
{
|
||||
"name": "Analysis",
|
||||
"agent": analyst,
|
||||
"task": "Analyze the implications of these AI trends for software development.",
|
||||
"criteria": "Identifies actionable insights and potential challenges.",
|
||||
},
|
||||
{
|
||||
"name": "Report",
|
||||
"agent": writer,
|
||||
"task": "Create a brief executive summary (3-4 bullet points) for a CTO.",
|
||||
"criteria": "Concise, actionable, appropriate for executive audience.",
|
||||
},
|
||||
]
|
||||
|
||||
# Run all steps with context passing
|
||||
results = []
|
||||
context = ""
|
||||
|
||||
for step in steps:
|
||||
result = run_step(
|
||||
step_name=step["name"],
|
||||
agent=step["agent"],
|
||||
task=step["task"],
|
||||
criteria=step["criteria"],
|
||||
context=context,
|
||||
)
|
||||
results.append(result)
|
||||
context = result["output"] # Pass output as context to next step
|
||||
|
||||
# Summary
|
||||
print("\n" + "═"*60)
|
||||
print("SUMMARY")
|
||||
print("═"*60)
|
||||
|
||||
total_iterations = sum(r["iterations"] for r in results)
|
||||
avg_score = sum(r["score"] for r in results) / len(results)
|
||||
all_success = all(r["success"] for r in results)
|
||||
|
||||
for r in results:
|
||||
status = "✅" if r["success"] else "❌"
|
||||
print(f" {status} {r['name']}: {r['score']:.1f}/10 ({r['iterations']} iterations)")
|
||||
|
||||
print(f"\n Total Iterations: {total_iterations}")
|
||||
print(f" Average Score: {avg_score:.1f}/10")
|
||||
print(f" All Steps Passed: {'Yes' if all_success else 'No'}")
|
||||
|
||||
print("\n" + "─"*60)
|
||||
print("FINAL REPORT OUTPUT:")
|
||||
print("─"*60)
|
||||
print(results[-1]["output"][:600])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,183 +0,0 @@
|
||||
"""
|
||||
Recipe Optimization Loop (CLI-based)
|
||||
|
||||
Demonstrates the 5-cycle recipe improvement pattern using CLI commands:
|
||||
Create (once) → Run → Judge → Optimize (loop 5x)
|
||||
|
||||
Usage:
|
||||
pip install praisonai
|
||||
export OPENAI_API_KEY=your-key
|
||||
export TAVILY_API_KEY=your-key
|
||||
python recipe_optimization_loop.py "research AI trends in healthcare"
|
||||
"""
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def run_cli(cmd: str, cwd: str = None) -> tuple[int, str, str]:
|
||||
"""Run a CLI command and return (exit_code, stdout, stderr)."""
|
||||
result = subprocess.run(
|
||||
cmd,
|
||||
shell=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
cwd=cwd,
|
||||
)
|
||||
return result.returncode, result.stdout, result.stderr
|
||||
|
||||
|
||||
def create_recipe(goal: str, work_dir: str) -> str:
|
||||
"""Create a recipe once. Returns recipe name."""
|
||||
print(f"\n{'═'*60}")
|
||||
print("STEP 1: CREATE RECIPE (once)")
|
||||
print(f"{'═'*60}")
|
||||
print(f"Goal: {goal}")
|
||||
|
||||
cmd = f'praisonai recipe create "{goal}" --no-optimize'
|
||||
code, stdout, stderr = run_cli(cmd, cwd=work_dir)
|
||||
|
||||
if code != 0:
|
||||
print(f"❌ Failed to create recipe: {stderr}")
|
||||
return None
|
||||
|
||||
# Extract recipe name from output
|
||||
for line in stdout.split('\n'):
|
||||
if 'Created recipe:' in line:
|
||||
recipe_name = line.split('Created recipe:')[1].strip()
|
||||
print(f"✅ Created: {recipe_name}")
|
||||
return recipe_name
|
||||
|
||||
print(f"✅ Recipe created")
|
||||
return None
|
||||
|
||||
|
||||
def run_recipe(recipe_name: str, trace_name: str, work_dir: str) -> tuple[bool, str]:
|
||||
"""Run the recipe and save trace. Returns (success, output)."""
|
||||
cmd = f'praisonai recipe run {recipe_name} --save --name {trace_name}'
|
||||
code, stdout, stderr = run_cli(cmd, cwd=work_dir)
|
||||
|
||||
success = code == 0 and 'status: completed' in stdout.lower()
|
||||
return success, stdout
|
||||
|
||||
|
||||
def judge_recipe(trace_name: str, work_dir: str) -> tuple[float, str]:
|
||||
"""Judge the trace. Returns (score, feedback)."""
|
||||
cmd = f'praisonai recipe judge {trace_name}'
|
||||
code, stdout, stderr = run_cli(cmd, cwd=work_dir)
|
||||
|
||||
# Extract score from output
|
||||
score = 0.0
|
||||
for line in stdout.split('\n'):
|
||||
if 'Overall Score:' in line:
|
||||
try:
|
||||
score = float(line.split(':')[1].strip().split('/')[0])
|
||||
except:
|
||||
pass
|
||||
|
||||
return score, stdout
|
||||
|
||||
|
||||
def optimize_recipe(recipe_name: str, iterations: int, work_dir: str) -> tuple[float, str]:
|
||||
"""Optimize the recipe. Returns (final_score, output)."""
|
||||
cmd = f'praisonai recipe optimize {recipe_name} --iterations {iterations}'
|
||||
code, stdout, stderr = run_cli(cmd, cwd=work_dir)
|
||||
|
||||
# Extract final score
|
||||
score = 0.0
|
||||
for line in stdout.split('\n'):
|
||||
if 'Final score:' in line or 'score:' in line.lower():
|
||||
try:
|
||||
score = float(line.split(':')[1].strip().split('/')[0])
|
||||
except:
|
||||
pass
|
||||
|
||||
return score, stdout
|
||||
|
||||
|
||||
def run_optimization_loop(goal: str, num_cycles: int = 5, threshold: float = 8.0):
|
||||
"""Run the full Create → Run → Judge → Optimize loop."""
|
||||
|
||||
print("╔════════════════════════════════════════════════════════════╗")
|
||||
print("║ Recipe Optimization Loop (CLI-based) ║")
|
||||
print("║ Pattern: Create (once) → Run → Judge → Optimize (loop) ║")
|
||||
print(f"║ Cycles: {num_cycles} | Threshold: {threshold}/10 ║")
|
||||
print("╚════════════════════════════════════════════════════════════╝")
|
||||
|
||||
# Create work directory
|
||||
work_dir = Path("/tmp/recipe-optimization-loop")
|
||||
work_dir.mkdir(parents=True, exist_ok=True)
|
||||
os.chdir(work_dir)
|
||||
|
||||
# Step 1: Create recipe (once)
|
||||
recipe_name = create_recipe(goal, str(work_dir))
|
||||
if not recipe_name:
|
||||
print("Failed to create recipe. Exiting.")
|
||||
return
|
||||
|
||||
# Step 2-N: Run → Judge → Optimize loop
|
||||
results = []
|
||||
|
||||
for cycle in range(1, num_cycles + 1):
|
||||
print(f"\n{'─'*60}")
|
||||
print(f"CYCLE {cycle}/{num_cycles}")
|
||||
print(f"{'─'*60}")
|
||||
|
||||
trace_name = f"cycle-{cycle}"
|
||||
|
||||
# Run
|
||||
print(f" 📝 Running recipe...")
|
||||
success, run_output = run_recipe(recipe_name, trace_name, str(work_dir))
|
||||
run_status = "✅" if success else "⚠️"
|
||||
print(f" {run_status} Run complete")
|
||||
|
||||
# Judge
|
||||
print(f" ⚖️ Judging output...")
|
||||
score, judge_output = judge_recipe(trace_name, str(work_dir))
|
||||
score_status = "✅" if score >= threshold else "⚠️" if score >= 6.0 else "❌"
|
||||
print(f" {score_status} Score: {score}/10")
|
||||
|
||||
# Check if threshold met
|
||||
if score >= threshold:
|
||||
print(f" 🎉 Threshold {threshold}/10 reached!")
|
||||
results.append({"cycle": cycle, "score": score, "status": "passed"})
|
||||
break
|
||||
|
||||
# Optimize
|
||||
print(f" 🔧 Optimizing recipe...")
|
||||
opt_score, opt_output = optimize_recipe(recipe_name, 1, str(work_dir))
|
||||
print(f" → Optimized score: {opt_score}/10")
|
||||
|
||||
results.append({
|
||||
"cycle": cycle,
|
||||
"initial_score": score,
|
||||
"optimized_score": opt_score,
|
||||
"status": "optimized" if opt_score > score else "no_change"
|
||||
})
|
||||
|
||||
# Summary
|
||||
print(f"\n{'═'*60}")
|
||||
print("SUMMARY")
|
||||
print(f"{'═'*60}")
|
||||
|
||||
for r in results:
|
||||
if "initial_score" in r:
|
||||
print(f" Cycle {r['cycle']}: {r['initial_score']} → {r['optimized_score']}/10")
|
||||
else:
|
||||
print(f" Cycle {r['cycle']}: {r['score']}/10 ✅ PASSED")
|
||||
|
||||
final_score = results[-1].get("optimized_score", results[-1].get("score", 0))
|
||||
success = final_score >= threshold
|
||||
|
||||
print(f"\n Final Score: {final_score}/10")
|
||||
print(f" Success: {'Yes ✅' if success else 'No ❌'}")
|
||||
print(f" Recipe: {work_dir}/{recipe_name}")
|
||||
|
||||
return results
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
goal = sys.argv[1] if len(sys.argv) > 1 else "research latest AI trends and create a summary report"
|
||||
run_optimization_loop(goal, num_cycles=5, threshold=8.0)
|
||||
@@ -457,19 +457,6 @@ _LAZY_IMPORTS = {
|
||||
'get_default_plugin_dirs': ('praisonaiagents.plugins', 'get_default_plugin_dirs'),
|
||||
'get_plugin_template': ('praisonaiagents.plugins', 'get_plugin_template'),
|
||||
'ensure_plugin_dir': ('praisonaiagents.plugins', 'ensure_plugin_dir'),
|
||||
|
||||
# Config loader - for config-driven defaults
|
||||
'get_config': ('praisonaiagents.config.loader', 'get_config'),
|
||||
'get_default': ('praisonaiagents.config.loader', 'get_default'),
|
||||
'get_plugins_config': ('praisonaiagents.config.loader', 'get_plugins_config'),
|
||||
'get_defaults_config': ('praisonaiagents.config.loader', 'get_defaults_config'),
|
||||
'apply_config_defaults': ('praisonaiagents.config.loader', 'apply_config_defaults'),
|
||||
'validate_config': ('praisonaiagents.config.loader', 'validate_config'),
|
||||
'get_config_path': ('praisonaiagents.config.loader', 'get_config_path'),
|
||||
'ConfigValidationError': ('praisonaiagents.config.loader', 'ConfigValidationError'),
|
||||
'PraisonConfig': ('praisonaiagents.config.loader', 'PraisonConfig'),
|
||||
'PluginsConfig': ('praisonaiagents.config.loader', 'PluginsConfig'),
|
||||
'DefaultsConfig': ('praisonaiagents.config.loader', 'DefaultsConfig'),
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -9,10 +9,6 @@ Environment Variables:
|
||||
PRAISONAI_TELEMETRY_ENABLED: Enable telemetry (default: false, opt-in)
|
||||
PRAISONAI_TELEMETRY_DISABLED: Disable telemetry (takes precedence)
|
||||
DO_NOT_TRACK: Standard opt-out flag (takes precedence)
|
||||
PRAISONAI_PLUGINS: Enable plugins (default: false)
|
||||
- "true" or "1": Enable all discovered plugins
|
||||
- "logging,metrics": Enable specific plugins (comma-separated)
|
||||
- "false" or "0": Disable plugins
|
||||
"""
|
||||
import os
|
||||
from typing import Optional
|
||||
@@ -81,38 +77,6 @@ AUTO_INSTRUMENT = _str_to_bool(
|
||||
)
|
||||
|
||||
|
||||
# Plugins: Enable background plugins (hooks, metrics, logging)
|
||||
# Note: Tools and guardrails work WITHOUT this flag - they are explicit
|
||||
def _get_plugins_enabled() -> bool:
|
||||
"""Determine if plugins should be auto-enabled based on env var."""
|
||||
env_value = os.environ.get('PRAISONAI_PLUGINS', '').lower()
|
||||
if not env_value:
|
||||
return False
|
||||
if env_value in ('true', '1', 'yes', 'on'):
|
||||
return True
|
||||
if env_value in ('false', '0', 'no', 'off'):
|
||||
return False
|
||||
# Treat as comma-separated list of plugin names (implies enabled)
|
||||
return True
|
||||
|
||||
|
||||
def _get_plugins_list() -> list:
|
||||
"""Get list of specific plugins to enable from env var.
|
||||
|
||||
Returns:
|
||||
List of plugin names, or empty list if all plugins enabled.
|
||||
"""
|
||||
env_value = os.environ.get('PRAISONAI_PLUGINS', '').lower()
|
||||
if not env_value or env_value in ('true', '1', 'yes', 'on', 'false', '0', 'no', 'off'):
|
||||
return []
|
||||
# Parse comma-separated list
|
||||
return [p.strip() for p in env_value.split(',') if p.strip()]
|
||||
|
||||
|
||||
PLUGINS_ENABLED = _get_plugins_enabled()
|
||||
PLUGINS_LIST = _get_plugins_list()
|
||||
|
||||
|
||||
def missing_dependency_error(
|
||||
package_name: str,
|
||||
extra_name: Optional[str] = None,
|
||||
|
||||
@@ -541,49 +541,6 @@ class Agent:
|
||||
Agent._configure_logging()
|
||||
Agent._logging_configured = True
|
||||
|
||||
# ============================================================
|
||||
# CONFIG-DRIVEN DEFAULTS (apply before parameter resolution)
|
||||
# Precedence: Explicit params > Config file > Built-in defaults
|
||||
# Only applies when param is None (not explicitly set)
|
||||
# ============================================================
|
||||
from ..config.loader import apply_config_defaults, get_default
|
||||
|
||||
# Apply config defaults for LLM if not explicitly set
|
||||
if llm is None and model is None:
|
||||
config_model = get_default("model")
|
||||
if config_model:
|
||||
llm = config_model
|
||||
|
||||
# Apply config defaults for base_url if not explicitly set
|
||||
if base_url is None:
|
||||
config_base_url = get_default("base_url")
|
||||
if config_base_url:
|
||||
base_url = config_base_url
|
||||
|
||||
# Apply config defaults for feature params (memory, knowledge, etc.)
|
||||
# These use apply_config_defaults which handles enabled=True/False logic
|
||||
if memory is None:
|
||||
memory = apply_config_defaults("memory", memory, MemoryConfig)
|
||||
if knowledge is None:
|
||||
knowledge = apply_config_defaults("knowledge", knowledge, KnowledgeConfig)
|
||||
if planning is False: # Only override if explicitly False (default)
|
||||
config_planning = apply_config_defaults("planning", None, PlanningConfig)
|
||||
if config_planning:
|
||||
planning = config_planning
|
||||
if reflection is None:
|
||||
reflection = apply_config_defaults("reflection", reflection, ReflectionConfig)
|
||||
if web is None:
|
||||
web = apply_config_defaults("web", web, WebConfig)
|
||||
if output is None:
|
||||
output = apply_config_defaults("output", output, OutputConfig)
|
||||
if execution is None:
|
||||
execution = apply_config_defaults("execution", execution, ExecutionConfig)
|
||||
if caching is None:
|
||||
caching = apply_config_defaults("caching", caching, CachingConfig)
|
||||
if autonomy is None:
|
||||
# AutonomyConfig is in agent/autonomy.py - use dict for config defaults
|
||||
autonomy = apply_config_defaults("autonomy", autonomy, None)
|
||||
|
||||
# ============================================================
|
||||
# CONSOLIDATED PARAMS EXTRACTION (agent-centric API)
|
||||
# Uses unified resolver: Instance > Config > Array > String > Bool > Default
|
||||
|
||||
@@ -141,19 +141,8 @@ class SubagentDelegator:
|
||||
self._total_count: int = 0
|
||||
self._task_counter: int = 0
|
||||
|
||||
# Note: asyncio.Semaphore() is created lazily via _get_semaphore() to support
|
||||
# Python 3.9 where Semaphore() requires an event loop at creation time.
|
||||
self.__semaphore: Optional[asyncio.Semaphore] = None
|
||||
|
||||
def _get_semaphore(self) -> asyncio.Semaphore:
|
||||
"""Get the semaphore, creating it lazily if needed.
|
||||
|
||||
This deferred creation is required for Python 3.9 compatibility
|
||||
where asyncio.Semaphore() calls get_event_loop() at creation time.
|
||||
"""
|
||||
if self.__semaphore is None:
|
||||
self.__semaphore = asyncio.Semaphore(self.config.max_concurrent_subagents)
|
||||
return self.__semaphore
|
||||
# Semaphore for concurrency control
|
||||
self._semaphore = asyncio.Semaphore(self.config.max_concurrent_subagents)
|
||||
|
||||
def get_available_agents(self) -> List[str]:
|
||||
"""Get list of available agent names for delegation."""
|
||||
@@ -215,7 +204,7 @@ class SubagentDelegator:
|
||||
self._total_count += 1
|
||||
|
||||
# Execute with concurrency control
|
||||
async with self._get_semaphore():
|
||||
async with self._semaphore:
|
||||
return await self._execute_task(task)
|
||||
|
||||
async def delegate_parallel(
|
||||
|
||||
@@ -33,9 +33,6 @@ from dataclasses import dataclass, field
|
||||
from typing import Dict, List, Any, Optional, Callable, Tuple, Union
|
||||
from enum import Enum
|
||||
|
||||
# Import AutonomyConfig from canonical location (no circular dep)
|
||||
from ..agent.autonomy import AutonomyConfig
|
||||
|
||||
|
||||
class MemoryBackend(str, Enum):
|
||||
"""Memory storage backends."""
|
||||
@@ -956,6 +953,54 @@ class AutonomyLevel(str, Enum):
|
||||
AUTO_EDIT = "auto_edit"
|
||||
FULL_AUTO = "full_auto"
|
||||
|
||||
|
||||
@dataclass
|
||||
class AutonomyConfig:
|
||||
"""
|
||||
Configuration for agent autonomy behavior.
|
||||
|
||||
Controls escalation, doom-loop detection, and approval policies.
|
||||
|
||||
Usage:
|
||||
# Simple enable
|
||||
Agent(autonomy=True)
|
||||
|
||||
# With config
|
||||
Agent(autonomy=AutonomyConfig(
|
||||
level="auto_edit",
|
||||
escalation_enabled=True,
|
||||
doom_loop_detection=True,
|
||||
max_consecutive_failures=3,
|
||||
))
|
||||
"""
|
||||
# Autonomy level
|
||||
level: Union[str, AutonomyLevel] = AutonomyLevel.SUGGEST
|
||||
|
||||
# Escalation pipeline
|
||||
escalation_enabled: bool = True
|
||||
escalation_threshold: int = 3
|
||||
|
||||
# Doom loop detection
|
||||
doom_loop_detection: bool = True
|
||||
max_consecutive_failures: int = 3
|
||||
|
||||
# Approval policies
|
||||
require_approval_for_writes: bool = True
|
||||
require_approval_for_shell: bool = True
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Convert to dictionary."""
|
||||
return {
|
||||
"level": self.level.value if isinstance(self.level, AutonomyLevel) else self.level,
|
||||
"escalation_enabled": self.escalation_enabled,
|
||||
"escalation_threshold": self.escalation_threshold,
|
||||
"doom_loop_detection": self.doom_loop_detection,
|
||||
"max_consecutive_failures": self.max_consecutive_failures,
|
||||
"require_approval_for_writes": self.require_approval_for_writes,
|
||||
"require_approval_for_shell": self.require_approval_for_shell,
|
||||
}
|
||||
|
||||
|
||||
# Type aliases for Union types used in Agent.__init__
|
||||
MemoryParam = Union[bool, MemoryConfig, Any] # Any = MemoryManager instance
|
||||
KnowledgeParam = Union[bool, List[str], KnowledgeConfig, Any] # Any = KnowledgeBase instance
|
||||
@@ -969,7 +1014,7 @@ TemplateParam = TemplateConfig
|
||||
CachingParam = Union[bool, CachingConfig]
|
||||
HooksParam = Union[List[Any], HooksConfig]
|
||||
SkillsParam = Union[List[str], SkillsConfig]
|
||||
AutonomyParam = Union[bool, Dict[str, Any], "AutonomyConfig"]
|
||||
AutonomyParam = Union[bool, Dict[str, Any], AutonomyConfig]
|
||||
|
||||
|
||||
# =============================================================================
|
||||
|
||||
@@ -1,609 +0,0 @@
|
||||
"""
|
||||
Configuration Loader for PraisonAI Agents.
|
||||
|
||||
Loads configuration from multiple sources with precedence:
|
||||
1. Explicit parameters (highest)
|
||||
2. Environment variables
|
||||
3. Config file (.praisonai/config.toml or praisonai.toml)
|
||||
4. Defaults (lowest)
|
||||
|
||||
Zero Performance Impact:
|
||||
- Config is loaded lazily on first access
|
||||
- No file I/O at import time
|
||||
- Cached after first load
|
||||
|
||||
Usage:
|
||||
from praisonaiagents.config.loader import get_config, get_default
|
||||
|
||||
# Get entire config
|
||||
config = get_config()
|
||||
|
||||
# Get specific default with fallback
|
||||
model = get_default("model", "gpt-4o-mini")
|
||||
memory_config = get_default("memory", {})
|
||||
|
||||
# Validate config file
|
||||
errors = validate_config(config.to_dict())
|
||||
"""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# CONFIG VALIDATION SCHEMA
|
||||
# ============================================================================
|
||||
# Defines valid keys and types for config file validation
|
||||
|
||||
VALID_PLUGINS_KEYS = {
|
||||
"enabled": (bool, list, str), # bool, list of plugin names, or "true"/"false"
|
||||
"auto_discover": (bool,),
|
||||
"directories": (list,),
|
||||
}
|
||||
|
||||
VALID_DEFAULTS_KEYS = {
|
||||
# LLM settings
|
||||
"model": (str,),
|
||||
"base_url": (str,),
|
||||
"api_key": (str,),
|
||||
# Feature flags
|
||||
"allow_delegation": (bool,),
|
||||
"allow_code_execution": (bool,),
|
||||
"code_execution_mode": (str,),
|
||||
# Feature configs (nested dicts with 'enabled' key)
|
||||
"memory": (dict, bool),
|
||||
"knowledge": (dict, bool),
|
||||
"planning": (dict, bool),
|
||||
"reflection": (dict, bool),
|
||||
"guardrails": (dict, bool),
|
||||
"web": (dict, bool),
|
||||
"output": (dict, str), # dict or preset string
|
||||
"execution": (dict, str), # dict or preset string
|
||||
"caching": (dict, bool),
|
||||
"autonomy": (dict, bool),
|
||||
"skills": (dict, list),
|
||||
"context": (dict, bool),
|
||||
"hooks": (dict, list),
|
||||
"templates": (dict,),
|
||||
}
|
||||
|
||||
VALID_ROOT_KEYS = {"plugins", "defaults"}
|
||||
|
||||
|
||||
class ConfigValidationError(Exception):
|
||||
"""Raised when config file has validation errors."""
|
||||
|
||||
def __init__(self, errors: List[str]):
|
||||
self.errors = errors
|
||||
message = "Config validation failed:\n" + "\n".join(f" - {e}" for e in errors)
|
||||
super().__init__(message)
|
||||
|
||||
# Global config cache (lazy loaded)
|
||||
_config_cache: Optional[Dict[str, Any]] = None
|
||||
_config_loaded: bool = False
|
||||
|
||||
|
||||
@dataclass
|
||||
class PluginsConfig:
|
||||
"""Configuration for the plugin system."""
|
||||
enabled: Union[bool, List[str]] = False # True, False, or list of plugin names
|
||||
auto_discover: bool = True
|
||||
directories: List[str] = field(default_factory=lambda: [
|
||||
"./.praisonai/plugins/",
|
||||
"~/.praisonai/plugins/"
|
||||
])
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"enabled": self.enabled,
|
||||
"auto_discover": self.auto_discover,
|
||||
"directories": self.directories,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class DefaultsConfig:
|
||||
"""Default configuration for Agent parameters."""
|
||||
# LLM defaults
|
||||
model: Optional[str] = None
|
||||
base_url: Optional[str] = None
|
||||
|
||||
# Feature flags
|
||||
allow_delegation: bool = False
|
||||
allow_code_execution: bool = False
|
||||
code_execution_mode: str = "safe"
|
||||
|
||||
# Feature configs (nested dicts)
|
||||
memory: Optional[Dict[str, Any]] = None
|
||||
knowledge: Optional[Dict[str, Any]] = None
|
||||
planning: Optional[Dict[str, Any]] = None
|
||||
reflection: Optional[Dict[str, Any]] = None
|
||||
web: Optional[Dict[str, Any]] = None
|
||||
output: Optional[Dict[str, Any]] = None
|
||||
execution: Optional[Dict[str, Any]] = None
|
||||
caching: Optional[Dict[str, Any]] = None
|
||||
autonomy: Optional[Dict[str, Any]] = None
|
||||
skills: Optional[Dict[str, Any]] = None
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"model": self.model,
|
||||
"base_url": self.base_url,
|
||||
"allow_delegation": self.allow_delegation,
|
||||
"allow_code_execution": self.allow_code_execution,
|
||||
"code_execution_mode": self.code_execution_mode,
|
||||
"memory": self.memory,
|
||||
"knowledge": self.knowledge,
|
||||
"planning": self.planning,
|
||||
"reflection": self.reflection,
|
||||
"web": self.web,
|
||||
"output": self.output,
|
||||
"execution": self.execution,
|
||||
"caching": self.caching,
|
||||
"autonomy": self.autonomy,
|
||||
"skills": self.skills,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class PraisonConfig:
|
||||
"""Root configuration for PraisonAI Agents."""
|
||||
plugins: PluginsConfig = field(default_factory=PluginsConfig)
|
||||
defaults: DefaultsConfig = field(default_factory=DefaultsConfig)
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"plugins": self.plugins.to_dict(),
|
||||
"defaults": self.defaults.to_dict(),
|
||||
}
|
||||
|
||||
|
||||
def _find_config_file() -> Optional[Path]:
|
||||
"""Find config file in standard locations.
|
||||
|
||||
Search order:
|
||||
1. .praisonai/config.toml (project-local)
|
||||
2. praisonai.toml (project root)
|
||||
3. ~/.praisonai/config.toml (user global)
|
||||
|
||||
Returns:
|
||||
Path to config file if found, None otherwise
|
||||
"""
|
||||
# Project-local locations
|
||||
cwd = Path.cwd()
|
||||
local_paths = [
|
||||
cwd / ".praisonai" / "config.toml",
|
||||
cwd / "praisonai.toml",
|
||||
]
|
||||
|
||||
for path in local_paths:
|
||||
if path.exists():
|
||||
return path
|
||||
|
||||
# User global location
|
||||
home = Path.home()
|
||||
global_path = home / ".praisonai" / "config.toml"
|
||||
if global_path.exists():
|
||||
return global_path
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _parse_toml(path: Path) -> Dict[str, Any]:
|
||||
"""Parse TOML file with fallback for Python < 3.11.
|
||||
|
||||
Args:
|
||||
path: Path to TOML file
|
||||
|
||||
Returns:
|
||||
Parsed config dict
|
||||
"""
|
||||
try:
|
||||
# Python 3.11+ has built-in tomllib
|
||||
import tomllib
|
||||
with open(path, "rb") as f:
|
||||
return tomllib.load(f)
|
||||
except ImportError:
|
||||
# Fallback for Python < 3.11
|
||||
try:
|
||||
import tomli
|
||||
with open(path, "rb") as f:
|
||||
return tomli.load(f)
|
||||
except ImportError:
|
||||
# No TOML parser available - return empty config
|
||||
import logging
|
||||
logging.debug(
|
||||
"TOML parser not available. Install tomli for Python < 3.11: "
|
||||
"pip install tomli"
|
||||
)
|
||||
return {}
|
||||
|
||||
|
||||
def _load_config() -> Dict[str, Any]:
|
||||
"""Load configuration from file.
|
||||
|
||||
Returns:
|
||||
Config dict (empty if no config file found)
|
||||
"""
|
||||
config_path = _find_config_file()
|
||||
if config_path is None:
|
||||
return {}
|
||||
|
||||
try:
|
||||
return _parse_toml(config_path)
|
||||
except Exception as e:
|
||||
import logging
|
||||
logging.warning(f"Failed to load config from {config_path}: {e}")
|
||||
return {}
|
||||
|
||||
|
||||
def _dict_to_plugins_config(data: Dict[str, Any]) -> PluginsConfig:
|
||||
"""Convert dict to PluginsConfig."""
|
||||
return PluginsConfig(
|
||||
enabled=data.get("enabled", False),
|
||||
auto_discover=data.get("auto_discover", True),
|
||||
directories=data.get("directories", [
|
||||
"./.praisonai/plugins/",
|
||||
"~/.praisonai/plugins/"
|
||||
]),
|
||||
)
|
||||
|
||||
|
||||
def _dict_to_defaults_config(data: Dict[str, Any]) -> DefaultsConfig:
|
||||
"""Convert dict to DefaultsConfig."""
|
||||
return DefaultsConfig(
|
||||
model=data.get("model"),
|
||||
base_url=data.get("base_url"),
|
||||
allow_delegation=data.get("allow_delegation", False),
|
||||
allow_code_execution=data.get("allow_code_execution", False),
|
||||
code_execution_mode=data.get("code_execution_mode", "safe"),
|
||||
memory=data.get("memory"),
|
||||
knowledge=data.get("knowledge"),
|
||||
planning=data.get("planning"),
|
||||
reflection=data.get("reflection"),
|
||||
web=data.get("web"),
|
||||
output=data.get("output"),
|
||||
execution=data.get("execution"),
|
||||
caching=data.get("caching"),
|
||||
autonomy=data.get("autonomy"),
|
||||
skills=data.get("skills"),
|
||||
)
|
||||
|
||||
|
||||
def _dict_to_praison_config(data: Dict[str, Any]) -> PraisonConfig:
|
||||
"""Convert dict to PraisonConfig."""
|
||||
plugins_data = data.get("plugins", {})
|
||||
defaults_data = data.get("defaults", {})
|
||||
|
||||
return PraisonConfig(
|
||||
plugins=_dict_to_plugins_config(plugins_data),
|
||||
defaults=_dict_to_defaults_config(defaults_data),
|
||||
)
|
||||
|
||||
|
||||
def get_config(reload: bool = False) -> PraisonConfig:
|
||||
"""Get the global configuration.
|
||||
|
||||
Loads config lazily on first access and caches it.
|
||||
|
||||
Args:
|
||||
reload: Force reload from file
|
||||
|
||||
Returns:
|
||||
PraisonConfig instance
|
||||
"""
|
||||
global _config_cache, _config_loaded
|
||||
|
||||
if not _config_loaded or reload:
|
||||
raw_config = _load_config()
|
||||
_config_cache = _dict_to_praison_config(raw_config)
|
||||
_config_loaded = True
|
||||
|
||||
return _config_cache
|
||||
|
||||
|
||||
def get_plugins_config() -> PluginsConfig:
|
||||
"""Get plugins configuration.
|
||||
|
||||
Returns:
|
||||
PluginsConfig instance
|
||||
"""
|
||||
return get_config().plugins
|
||||
|
||||
|
||||
def get_defaults_config() -> DefaultsConfig:
|
||||
"""Get defaults configuration.
|
||||
|
||||
Returns:
|
||||
DefaultsConfig instance
|
||||
"""
|
||||
return get_config().defaults
|
||||
|
||||
|
||||
def get_default(key: str, fallback: Any = None) -> Any:
|
||||
"""Get a specific default value.
|
||||
|
||||
Args:
|
||||
key: Config key (e.g., "model", "memory", "memory.backend")
|
||||
fallback: Value to return if key not found
|
||||
|
||||
Returns:
|
||||
Config value or fallback
|
||||
"""
|
||||
defaults = get_defaults_config()
|
||||
|
||||
# Handle nested keys like "memory.backend"
|
||||
if "." in key:
|
||||
parts = key.split(".")
|
||||
value = getattr(defaults, parts[0], None)
|
||||
if value is None:
|
||||
return fallback
|
||||
for part in parts[1:]:
|
||||
if isinstance(value, dict):
|
||||
value = value.get(part)
|
||||
else:
|
||||
return fallback
|
||||
if value is None:
|
||||
return fallback
|
||||
return value
|
||||
|
||||
# Simple key
|
||||
value = getattr(defaults, key, None)
|
||||
return value if value is not None else fallback
|
||||
|
||||
|
||||
def is_plugins_enabled() -> bool:
|
||||
"""Check if plugins are enabled via config or env var.
|
||||
|
||||
Returns:
|
||||
True if plugins should be enabled
|
||||
"""
|
||||
# Check env var first (highest precedence)
|
||||
env_value = os.environ.get("PRAISONAI_PLUGINS", "").lower()
|
||||
if env_value:
|
||||
if env_value in ("true", "1", "yes", "on"):
|
||||
return True
|
||||
if env_value in ("false", "0", "no", "off"):
|
||||
return False
|
||||
# Treat as comma-separated list of plugin names
|
||||
return True
|
||||
|
||||
# Check config file
|
||||
plugins_config = get_plugins_config()
|
||||
if isinstance(plugins_config.enabled, bool):
|
||||
return plugins_config.enabled
|
||||
if isinstance(plugins_config.enabled, list):
|
||||
return len(plugins_config.enabled) > 0
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def get_enabled_plugins() -> Optional[List[str]]:
|
||||
"""Get list of enabled plugins (if specific list provided).
|
||||
|
||||
Returns:
|
||||
List of plugin names, or None if all plugins enabled
|
||||
"""
|
||||
# Check env var first
|
||||
env_value = os.environ.get("PRAISONAI_PLUGINS", "").lower()
|
||||
if env_value and env_value not in ("true", "1", "yes", "on", "false", "0", "no", "off"):
|
||||
# Treat as comma-separated list
|
||||
return [p.strip() for p in env_value.split(",") if p.strip()]
|
||||
|
||||
# Check config file
|
||||
plugins_config = get_plugins_config()
|
||||
if isinstance(plugins_config.enabled, list):
|
||||
return plugins_config.enabled
|
||||
|
||||
return None # All plugins enabled
|
||||
|
||||
|
||||
def clear_config_cache() -> None:
|
||||
"""Clear the config cache (for testing)."""
|
||||
global _config_cache, _config_loaded
|
||||
_config_cache = None
|
||||
_config_loaded = False
|
||||
|
||||
|
||||
def validate_config(config: Dict[str, Any], raise_on_error: bool = False) -> List[str]:
|
||||
"""Validate config file structure and types.
|
||||
|
||||
Args:
|
||||
config: Raw config dict (from TOML parsing)
|
||||
raise_on_error: If True, raise ConfigValidationError on errors
|
||||
|
||||
Returns:
|
||||
List of validation error messages (empty if valid)
|
||||
|
||||
Example:
|
||||
config = {"plugins": {"enabled": True}, "defaults": {"model": "gpt-4o"}}
|
||||
errors = validate_config(config)
|
||||
if errors:
|
||||
print("Config errors:", errors)
|
||||
"""
|
||||
errors = []
|
||||
|
||||
# Check root keys
|
||||
for key in config.keys():
|
||||
if key not in VALID_ROOT_KEYS:
|
||||
similar = _suggest_similar_key(key, VALID_ROOT_KEYS)
|
||||
if similar:
|
||||
errors.append(f"Unknown root key '{key}'. Did you mean '{similar}'?")
|
||||
else:
|
||||
errors.append(f"Unknown root key '{key}'. Valid keys: {', '.join(sorted(VALID_ROOT_KEYS))}")
|
||||
|
||||
# Validate [plugins] section
|
||||
if "plugins" in config:
|
||||
plugins = config["plugins"]
|
||||
if not isinstance(plugins, dict):
|
||||
errors.append(f"[plugins] must be a table, got {type(plugins).__name__}")
|
||||
else:
|
||||
for key, value in plugins.items():
|
||||
if key not in VALID_PLUGINS_KEYS:
|
||||
similar = _suggest_similar_key(key, VALID_PLUGINS_KEYS.keys())
|
||||
if similar:
|
||||
errors.append(f"[plugins] Unknown key '{key}'. Did you mean '{similar}'?")
|
||||
else:
|
||||
errors.append(f"[plugins] Unknown key '{key}'. Valid keys: {', '.join(sorted(VALID_PLUGINS_KEYS.keys()))}")
|
||||
else:
|
||||
valid_types = VALID_PLUGINS_KEYS[key]
|
||||
if not isinstance(value, valid_types):
|
||||
errors.append(f"[plugins.{key}] Expected {_format_types(valid_types)}, got {type(value).__name__}")
|
||||
|
||||
# Validate [defaults] section
|
||||
if "defaults" in config:
|
||||
defaults = config["defaults"]
|
||||
if not isinstance(defaults, dict):
|
||||
errors.append(f"[defaults] must be a table, got {type(defaults).__name__}")
|
||||
else:
|
||||
for key, value in defaults.items():
|
||||
if key not in VALID_DEFAULTS_KEYS:
|
||||
similar = _suggest_similar_key(key, VALID_DEFAULTS_KEYS.keys())
|
||||
if similar:
|
||||
errors.append(f"[defaults] Unknown key '{key}'. Did you mean '{similar}'?")
|
||||
else:
|
||||
errors.append(f"[defaults] Unknown key '{key}'. Valid keys: {', '.join(sorted(VALID_DEFAULTS_KEYS.keys()))}")
|
||||
else:
|
||||
valid_types = VALID_DEFAULTS_KEYS[key]
|
||||
if not isinstance(value, valid_types):
|
||||
errors.append(f"[defaults.{key}] Expected {_format_types(valid_types)}, got {type(value).__name__}")
|
||||
# Validate nested feature configs
|
||||
if isinstance(value, dict) and key in ("memory", "knowledge", "planning", "reflection",
|
||||
"guardrails", "web", "output", "execution",
|
||||
"caching", "autonomy", "context"):
|
||||
nested_errors = _validate_feature_config(key, value)
|
||||
errors.extend(nested_errors)
|
||||
|
||||
if raise_on_error and errors:
|
||||
raise ConfigValidationError(errors)
|
||||
|
||||
return errors
|
||||
|
||||
|
||||
def _suggest_similar_key(key: str, valid_keys) -> Optional[str]:
|
||||
"""Suggest a similar valid key using simple string matching."""
|
||||
key_lower = key.lower()
|
||||
for valid in valid_keys:
|
||||
# Exact match after lowercasing
|
||||
if valid.lower() == key_lower:
|
||||
return valid
|
||||
# Prefix match
|
||||
if valid.lower().startswith(key_lower) or key_lower.startswith(valid.lower()):
|
||||
return valid
|
||||
# Substring match
|
||||
if key_lower in valid.lower() or valid.lower() in key_lower:
|
||||
return valid
|
||||
return None
|
||||
|
||||
|
||||
def _format_types(types: tuple) -> str:
|
||||
"""Format type tuple for error messages."""
|
||||
type_names = []
|
||||
for t in types:
|
||||
if t is bool:
|
||||
type_names.append("bool")
|
||||
elif t is str:
|
||||
type_names.append("str")
|
||||
elif t is dict:
|
||||
type_names.append("table")
|
||||
elif t is list:
|
||||
type_names.append("array")
|
||||
else:
|
||||
type_names.append(t.__name__)
|
||||
return " or ".join(type_names)
|
||||
|
||||
|
||||
def _validate_feature_config(feature: str, config: Dict[str, Any]) -> List[str]:
|
||||
"""Validate a nested feature config (memory, knowledge, etc.)."""
|
||||
errors = []
|
||||
|
||||
# Common valid keys for feature configs
|
||||
common_keys = {"enabled", "backend", "provider", "preset", "verbose"}
|
||||
|
||||
# Feature-specific valid keys
|
||||
feature_keys = {
|
||||
"memory": {"enabled", "backend", "provider", "use_long_term", "use_short_term",
|
||||
"user_id", "session_id", "db_path", "collection_name"},
|
||||
"knowledge": {"enabled", "sources", "provider", "chunk_size", "chunk_overlap"},
|
||||
"planning": {"enabled", "max_steps", "reasoning"},
|
||||
"reflection": {"enabled", "max_reflect", "min_reflect", "llm"},
|
||||
"guardrails": {"enabled", "max_retries", "validator"},
|
||||
"web": {"enabled", "search", "fetch", "provider"},
|
||||
"output": {"enabled", "verbose", "markdown", "stream", "metrics", "preset"},
|
||||
"execution": {"enabled", "max_iter", "max_rpm", "max_execution_time", "preset"},
|
||||
"caching": {"enabled", "prompt_caching", "ttl"},
|
||||
"autonomy": {"enabled", "max_steps", "approval_required"},
|
||||
"context": {"enabled", "max_tokens", "strategy"},
|
||||
}
|
||||
|
||||
valid_keys = feature_keys.get(feature, common_keys)
|
||||
|
||||
for key in config.keys():
|
||||
if key not in valid_keys:
|
||||
similar = _suggest_similar_key(key, valid_keys)
|
||||
if similar:
|
||||
errors.append(f"[defaults.{feature}.{key}] Unknown key. Did you mean '{similar}'?")
|
||||
# Don't error on unknown nested keys - allow flexibility
|
||||
|
||||
return errors
|
||||
|
||||
|
||||
def get_config_path() -> Optional[Path]:
|
||||
"""Get the path to the config file if it exists.
|
||||
|
||||
Returns:
|
||||
Path to config file, or None if not found
|
||||
"""
|
||||
return _find_config_file()
|
||||
|
||||
|
||||
# Convenience function to merge config defaults with explicit params
|
||||
def apply_config_defaults(
|
||||
param_name: str,
|
||||
explicit_value: Any,
|
||||
config_class: Optional[type] = None,
|
||||
) -> Any:
|
||||
"""Apply config defaults to a parameter if not explicitly set.
|
||||
|
||||
Args:
|
||||
param_name: Name of the parameter (e.g., "memory", "knowledge")
|
||||
explicit_value: Value explicitly passed by user (None if not set)
|
||||
config_class: Optional config class to instantiate
|
||||
|
||||
Returns:
|
||||
Resolved value with config defaults applied
|
||||
|
||||
Example:
|
||||
# User passes memory=True, config has memory.backend="postgres"
|
||||
# Result: MemoryConfig with backend="postgres"
|
||||
|
||||
memory = apply_config_defaults("memory", True, MemoryConfig)
|
||||
"""
|
||||
# If user explicitly passed a value, respect it
|
||||
if explicit_value is not None:
|
||||
# If True and we have config defaults, merge them
|
||||
if explicit_value is True:
|
||||
config_defaults = get_default(param_name)
|
||||
if config_defaults and isinstance(config_defaults, dict):
|
||||
if config_class:
|
||||
# Check if enabled key exists and is True
|
||||
if config_defaults.get("enabled", True):
|
||||
return config_class(**{k: v for k, v in config_defaults.items() if k != "enabled"})
|
||||
return config_defaults
|
||||
return explicit_value
|
||||
|
||||
# Check if config has defaults for this param
|
||||
config_defaults = get_default(param_name)
|
||||
if config_defaults is None:
|
||||
return None
|
||||
|
||||
# If config has enabled=True, return the config
|
||||
if isinstance(config_defaults, dict):
|
||||
if config_defaults.get("enabled", False):
|
||||
if config_class:
|
||||
return config_class(**{k: v for k, v in config_defaults.items() if k != "enabled"})
|
||||
return True
|
||||
|
||||
return None
|
||||
@@ -1,156 +0,0 @@
|
||||
# PraisonAI Agents Configuration File
|
||||
# Copy this file to .praisonai/config.toml in your project directory
|
||||
# or to ~/.praisonai/config.toml for global settings
|
||||
|
||||
# ============================================================================
|
||||
# PLUGINS CONFIGURATION
|
||||
# ============================================================================
|
||||
# Enable background plugins (hooks, metrics, logging)
|
||||
# Note: Tools and guardrails work WITHOUT enabling plugins
|
||||
|
||||
[plugins]
|
||||
# Enable all plugins: true
|
||||
# Enable specific plugins: ["logging", "metrics"]
|
||||
# Disable: false
|
||||
enabled = false
|
||||
|
||||
# Auto-discover plugins from default directories
|
||||
auto_discover = true
|
||||
|
||||
# Plugin directories to scan
|
||||
directories = [
|
||||
"./.praisonai/plugins/",
|
||||
"~/.praisonai/plugins/"
|
||||
]
|
||||
|
||||
# ============================================================================
|
||||
# DEFAULT SETTINGS
|
||||
# ============================================================================
|
||||
# These defaults are used when you pass True to a feature parameter
|
||||
# Example: Agent(memory=True) will use [defaults.memory] settings
|
||||
|
||||
[defaults]
|
||||
# Default LLM model
|
||||
model = "gpt-4o-mini"
|
||||
|
||||
# Custom LLM endpoint (optional)
|
||||
# base_url = "http://localhost:11434/v1"
|
||||
|
||||
# Feature flags
|
||||
allow_delegation = false
|
||||
allow_code_execution = false
|
||||
code_execution_mode = "safe"
|
||||
|
||||
# ============================================================================
|
||||
# MEMORY DEFAULTS
|
||||
# ============================================================================
|
||||
# Used when: Agent(memory=True)
|
||||
|
||||
[defaults.memory]
|
||||
enabled = false
|
||||
backend = "file" # file, sqlite, redis, postgres, mongodb
|
||||
# user_id = "" # Auto-generated if empty
|
||||
auto_memory = false
|
||||
history = false
|
||||
history_limit = 10
|
||||
|
||||
# Learning configuration
|
||||
[defaults.memory.learn]
|
||||
enabled = false
|
||||
persona = true
|
||||
insights = true
|
||||
patterns = false
|
||||
|
||||
# ============================================================================
|
||||
# KNOWLEDGE DEFAULTS
|
||||
# ============================================================================
|
||||
# Used when: Agent(knowledge=True)
|
||||
|
||||
[defaults.knowledge]
|
||||
enabled = false
|
||||
embedder = "openai"
|
||||
chunking_strategy = "semantic"
|
||||
chunk_size = 1000
|
||||
chunk_overlap = 200
|
||||
retrieval_k = 5
|
||||
retrieval_threshold = 0.0
|
||||
rerank = false
|
||||
|
||||
# ============================================================================
|
||||
# PLANNING DEFAULTS
|
||||
# ============================================================================
|
||||
# Used when: Agent(planning=True)
|
||||
|
||||
[defaults.planning]
|
||||
enabled = false
|
||||
reasoning = false
|
||||
auto_approve = false
|
||||
read_only = false
|
||||
|
||||
# ============================================================================
|
||||
# REFLECTION DEFAULTS
|
||||
# ============================================================================
|
||||
# Used when: Agent(reflection=True)
|
||||
|
||||
[defaults.reflection]
|
||||
enabled = false
|
||||
min_iterations = 1
|
||||
max_iterations = 3
|
||||
|
||||
# ============================================================================
|
||||
# WEB DEFAULTS
|
||||
# ============================================================================
|
||||
# Used when: Agent(web=True)
|
||||
|
||||
[defaults.web]
|
||||
enabled = false
|
||||
search = true
|
||||
fetch = true
|
||||
search_provider = "duckduckgo"
|
||||
max_results = 5
|
||||
|
||||
# ============================================================================
|
||||
# OUTPUT DEFAULTS
|
||||
# ============================================================================
|
||||
# Used when: Agent(output=True) or default output mode
|
||||
|
||||
[defaults.output]
|
||||
preset = "silent" # silent, actions, verbose, json, stream, status
|
||||
verbose = false
|
||||
markdown = false
|
||||
stream = false
|
||||
metrics = false
|
||||
|
||||
# ============================================================================
|
||||
# EXECUTION DEFAULTS
|
||||
# ============================================================================
|
||||
# Used when: Agent(execution=True) or default execution limits
|
||||
|
||||
[defaults.execution]
|
||||
max_iter = 20
|
||||
max_retry_limit = 2
|
||||
# max_rpm = null
|
||||
# max_execution_time = null
|
||||
|
||||
# ============================================================================
|
||||
# CACHING DEFAULTS
|
||||
# ============================================================================
|
||||
# Used when: Agent(caching=True)
|
||||
|
||||
[defaults.caching]
|
||||
enabled = true
|
||||
prompt_caching = false
|
||||
|
||||
# ============================================================================
|
||||
# AUTONOMY DEFAULTS
|
||||
# ============================================================================
|
||||
# Used when: Agent(autonomy=True)
|
||||
|
||||
[defaults.autonomy]
|
||||
level = "suggest" # suggest, auto_edit, full_auto
|
||||
escalation_enabled = true
|
||||
escalation_threshold = 3
|
||||
doom_loop_detection = true
|
||||
max_consecutive_failures = 3
|
||||
require_approval_for_writes = true
|
||||
require_approval_for_shell = true
|
||||
@@ -323,7 +323,6 @@ class SmartBackend:
|
||||
# Check if ripgrep is available
|
||||
rg = self._get_ripgrep()
|
||||
if not rg.is_available():
|
||||
self._use_ripgrep = False
|
||||
return False
|
||||
|
||||
# Count files if not already counted for this path
|
||||
|
||||
@@ -24,8 +24,7 @@ from pathlib import Path
|
||||
from enum import Enum
|
||||
|
||||
from .models import (
|
||||
ContextLedger, BudgetAllocation, OptimizerStrategy, OptimizationResult,
|
||||
ContextConfig as ManagerConfig # Deprecated alias for backward compatibility
|
||||
ContextLedger, BudgetAllocation, OptimizerStrategy, OptimizationResult
|
||||
)
|
||||
from .tokens import estimate_tokens_heuristic, estimate_messages_tokens, estimate_message_tokens
|
||||
from .budgeter import ContextBudgeter
|
||||
@@ -289,6 +288,180 @@ class SnapshotHookData:
|
||||
"budget": self.budget.to_dict() if self.budget else None,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class ManagerConfig:
|
||||
"""
|
||||
Complete configuration for ContextManager.
|
||||
|
||||
Consolidates all context management settings with proper precedence.
|
||||
"""
|
||||
# Auto-compaction
|
||||
auto_compact: bool = True
|
||||
compact_threshold: float = 0.8
|
||||
strategy: OptimizerStrategy = OptimizerStrategy.SMART
|
||||
|
||||
# Compression benefit check
|
||||
compression_min_gain_pct: float = 5.0
|
||||
compression_max_attempts: int = 3
|
||||
|
||||
# Budget
|
||||
output_reserve: int = 8000
|
||||
history_ratio: float = 0.6
|
||||
default_tool_output_max: int = 10000
|
||||
|
||||
# Per-tool budgets
|
||||
tool_budgets: Dict[str, PerToolBudget] = field(default_factory=dict)
|
||||
protected_tools: List[str] = field(default_factory=list)
|
||||
|
||||
# LLM-powered summarization
|
||||
llm_summarize: bool = False # Enable LLM-powered summarization
|
||||
|
||||
# Smart tool output summarization
|
||||
smart_tool_summarize: bool = True # Summarize large tool outputs using LLM before truncating
|
||||
tool_summarize_limits: Dict[str, int] = field(default_factory=dict) # Per-tool min_chars_to_summarize
|
||||
|
||||
# Estimation
|
||||
estimation_mode: EstimationMode = EstimationMode.HEURISTIC
|
||||
log_estimation_mismatch: bool = False
|
||||
mismatch_threshold_pct: float = 15.0
|
||||
|
||||
# Monitoring
|
||||
monitor_enabled: bool = False
|
||||
monitor_path: str = "./context.txt"
|
||||
monitor_format: Literal["human", "json"] = "human"
|
||||
monitor_frequency: Literal["turn", "tool_call", "manual", "overflow"] = "turn"
|
||||
monitor_write_mode: Literal["sync", "async"] = "sync"
|
||||
redact_sensitive: bool = True
|
||||
snapshot_timing: Literal["pre_optimization", "post_optimization", "both"] = "post_optimization"
|
||||
|
||||
# Path validation
|
||||
allow_absolute_paths: bool = False
|
||||
|
||||
# Pruning
|
||||
prune_after_tokens: int = 40000
|
||||
keep_recent_turns: int = 5
|
||||
|
||||
# Source tracking
|
||||
source: str = "defaults" # defaults, env, config_file, cli
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
result = {
|
||||
"auto_compact": self.auto_compact,
|
||||
"compact_threshold": self.compact_threshold,
|
||||
"strategy": self.strategy.value,
|
||||
"compression_min_gain_pct": self.compression_min_gain_pct,
|
||||
"compression_max_attempts": self.compression_max_attempts,
|
||||
"output_reserve": self.output_reserve,
|
||||
"history_ratio": self.history_ratio,
|
||||
"default_tool_output_max": self.default_tool_output_max,
|
||||
"tool_budgets": {k: v.to_dict() for k, v in self.tool_budgets.items()},
|
||||
"protected_tools": self.protected_tools,
|
||||
"estimation_mode": self.estimation_mode.value,
|
||||
"log_estimation_mismatch": self.log_estimation_mismatch,
|
||||
"mismatch_threshold_pct": self.mismatch_threshold_pct,
|
||||
"monitor_enabled": self.monitor_enabled,
|
||||
"monitor_path": self.monitor_path,
|
||||
"monitor_format": self.monitor_format,
|
||||
"monitor_frequency": self.monitor_frequency,
|
||||
"monitor_write_mode": self.monitor_write_mode,
|
||||
"redact_sensitive": self.redact_sensitive,
|
||||
"snapshot_timing": self.snapshot_timing,
|
||||
"allow_absolute_paths": self.allow_absolute_paths,
|
||||
"prune_after_tokens": self.prune_after_tokens,
|
||||
"keep_recent_turns": self.keep_recent_turns,
|
||||
"llm_summarize": self.llm_summarize,
|
||||
"smart_tool_summarize": self.smart_tool_summarize,
|
||||
"tool_summarize_limits": self.tool_summarize_limits,
|
||||
"source": self.source,
|
||||
}
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def from_env(cls) -> "ManagerConfig":
|
||||
"""Load config from environment variables."""
|
||||
import os
|
||||
|
||||
def get_bool(key: str, default: bool) -> bool:
|
||||
val = os.getenv(key, str(default)).lower()
|
||||
return val in ("true", "1", "yes", "on")
|
||||
|
||||
def get_float(key: str, default: float) -> float:
|
||||
try:
|
||||
return float(os.getenv(key, str(default)))
|
||||
except ValueError:
|
||||
return default
|
||||
|
||||
def get_int(key: str, default: int) -> int:
|
||||
try:
|
||||
return int(os.getenv(key, str(default)))
|
||||
except ValueError:
|
||||
return default
|
||||
|
||||
strategy_str = os.getenv("PRAISONAI_CONTEXT_STRATEGY", "smart")
|
||||
try:
|
||||
strategy = OptimizerStrategy(strategy_str)
|
||||
except ValueError:
|
||||
strategy = OptimizerStrategy.SMART
|
||||
|
||||
estimation_str = os.getenv("PRAISONAI_CONTEXT_ESTIMATION_MODE", "heuristic")
|
||||
try:
|
||||
estimation_mode = EstimationMode(estimation_str)
|
||||
except ValueError:
|
||||
estimation_mode = EstimationMode.HEURISTIC
|
||||
|
||||
return cls(
|
||||
auto_compact=get_bool("PRAISONAI_CONTEXT_AUTO_COMPACT", True),
|
||||
compact_threshold=get_float("PRAISONAI_CONTEXT_THRESHOLD", 0.8),
|
||||
strategy=strategy,
|
||||
compression_min_gain_pct=get_float("PRAISONAI_CONTEXT_COMPRESSION_MIN_GAIN", 5.0),
|
||||
compression_max_attempts=get_int("PRAISONAI_CONTEXT_COMPRESSION_MAX_ATTEMPTS", 3),
|
||||
output_reserve=get_int("PRAISONAI_CONTEXT_OUTPUT_RESERVE", 8000),
|
||||
default_tool_output_max=get_int("PRAISONAI_CONTEXT_TOOL_OUTPUT_MAX", 10000),
|
||||
estimation_mode=estimation_mode,
|
||||
log_estimation_mismatch=get_bool("PRAISONAI_CONTEXT_LOG_MISMATCH", False),
|
||||
monitor_enabled=get_bool("PRAISONAI_CONTEXT_MONITOR", False),
|
||||
monitor_path=os.getenv("PRAISONAI_CONTEXT_MONITOR_PATH", "./context.txt"),
|
||||
monitor_format=os.getenv("PRAISONAI_CONTEXT_MONITOR_FORMAT", "human"),
|
||||
monitor_frequency=os.getenv("PRAISONAI_CONTEXT_MONITOR_FREQUENCY", "turn"),
|
||||
monitor_write_mode=os.getenv("PRAISONAI_CONTEXT_MONITOR_WRITE_MODE", "sync"),
|
||||
redact_sensitive=get_bool("PRAISONAI_CONTEXT_REDACT", True),
|
||||
source="env",
|
||||
)
|
||||
|
||||
def merge(self, **overrides) -> "ManagerConfig":
|
||||
"""Create new config with overrides applied."""
|
||||
current = self.to_dict()
|
||||
|
||||
for key, value in overrides.items():
|
||||
if value is not None and key in current:
|
||||
current[key] = value
|
||||
|
||||
# Handle enum conversions
|
||||
if isinstance(current.get("strategy"), str):
|
||||
try:
|
||||
current["strategy"] = OptimizerStrategy(current["strategy"])
|
||||
except ValueError:
|
||||
current["strategy"] = OptimizerStrategy.SMART
|
||||
|
||||
if isinstance(current.get("estimation_mode"), str):
|
||||
try:
|
||||
current["estimation_mode"] = EstimationMode(current["estimation_mode"])
|
||||
except ValueError:
|
||||
current["estimation_mode"] = EstimationMode.HEURISTIC
|
||||
|
||||
# Reconstruct tool budgets
|
||||
tool_budgets = {}
|
||||
for name, budget_dict in current.get("tool_budgets", {}).items():
|
||||
if isinstance(budget_dict, dict):
|
||||
tool_budgets[name] = PerToolBudget(**budget_dict)
|
||||
elif isinstance(budget_dict, PerToolBudget):
|
||||
tool_budgets[name] = budget_dict
|
||||
current["tool_budgets"] = tool_budgets
|
||||
|
||||
return ManagerConfig(**current)
|
||||
|
||||
|
||||
class ContextManager:
|
||||
"""
|
||||
Unified facade for context management.
|
||||
|
||||
@@ -272,7 +272,6 @@ class ContextConfig:
|
||||
output_reserve: int = 8000
|
||||
history_ratio: float = 0.6 # 60% of usable for history
|
||||
tool_output_max: int = 10000 # Max tokens per tool output
|
||||
default_tool_output_max: int = 10000 # Alias for tool_output_max (ManagerConfig compat)
|
||||
|
||||
# Pruning
|
||||
prune_after_tokens: int = 40000
|
||||
@@ -309,38 +308,6 @@ class ContextConfig:
|
||||
])
|
||||
aggregate_max_tokens: int = 4000 # Max tokens for aggregated context
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# Internal fields (from ManagerConfig consolidation)
|
||||
# These are typically set by the system, not users directly
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
# Compression benefit check
|
||||
compression_min_gain_pct: float = 5.0
|
||||
compression_max_attempts: int = 3
|
||||
|
||||
# Per-tool budgets (advanced)
|
||||
tool_budgets: Dict[str, Any] = field(default_factory=dict) # tool_name -> PerToolBudget
|
||||
tool_summarize_limits: Dict[str, int] = field(default_factory=dict) # Per-tool min_chars
|
||||
|
||||
# Estimation modes
|
||||
estimation_mode: str = "heuristic" # heuristic, accurate, validated
|
||||
log_estimation_mismatch: bool = False
|
||||
mismatch_threshold_pct: float = 15.0
|
||||
|
||||
# Extended monitoring options
|
||||
monitor_enabled: bool = False # Enable monitoring
|
||||
monitor_path: str = "./context.txt" # Path for monitoring output
|
||||
monitor_format: str = "human" # human, json
|
||||
monitor_frequency: str = "turn" # turn, tool_call, manual, overflow
|
||||
monitor_write_mode: str = "sync" # sync, async
|
||||
redact_sensitive: bool = True
|
||||
snapshot_timing: str = "post_optimization" # pre_optimization, post_optimization, both
|
||||
allow_absolute_paths: bool = False
|
||||
|
||||
# Source tracking
|
||||
source: str = "defaults" # defaults, env, config_file, cli
|
||||
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Convert to dictionary."""
|
||||
result = asdict(self)
|
||||
@@ -371,73 +338,6 @@ class ContextConfig:
|
||||
output_reserve=8000,
|
||||
history_ratio=0.6,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_env(cls) -> "ContextConfig":
|
||||
"""Load config from environment variables.
|
||||
|
||||
Reads PRAISONAI_CONTEXT_* environment variables.
|
||||
"""
|
||||
import os
|
||||
|
||||
def get_bool(key: str, default: bool) -> bool:
|
||||
val = os.getenv(key, str(default)).lower()
|
||||
return val in ("true", "1", "yes", "on")
|
||||
|
||||
def get_float(key: str, default: float) -> float:
|
||||
try:
|
||||
return float(os.getenv(key, str(default)))
|
||||
except ValueError:
|
||||
return default
|
||||
|
||||
def get_int(key: str, default: int) -> int:
|
||||
try:
|
||||
return int(os.getenv(key, str(default)))
|
||||
except ValueError:
|
||||
return default
|
||||
|
||||
strategy_str = os.getenv("PRAISONAI_CONTEXT_STRATEGY", "smart")
|
||||
try:
|
||||
strategy = OptimizerStrategy(strategy_str)
|
||||
except ValueError:
|
||||
strategy = OptimizerStrategy.SMART
|
||||
|
||||
return cls(
|
||||
auto_compact=get_bool("PRAISONAI_CONTEXT_AUTO_COMPACT", True),
|
||||
compact_threshold=get_float("PRAISONAI_CONTEXT_THRESHOLD", 0.8),
|
||||
strategy=strategy,
|
||||
compression_min_gain_pct=get_float("PRAISONAI_CONTEXT_COMPRESSION_MIN_GAIN", 5.0),
|
||||
compression_max_attempts=get_int("PRAISONAI_CONTEXT_COMPRESSION_MAX_ATTEMPTS", 3),
|
||||
output_reserve=get_int("PRAISONAI_CONTEXT_OUTPUT_RESERVE", 8000),
|
||||
tool_output_max=get_int("PRAISONAI_CONTEXT_TOOL_OUTPUT_MAX", 10000),
|
||||
default_tool_output_max=get_int("PRAISONAI_CONTEXT_TOOL_OUTPUT_MAX", 10000),
|
||||
log_estimation_mismatch=get_bool("PRAISONAI_CONTEXT_LOG_MISMATCH", False),
|
||||
monitor_enabled=get_bool("PRAISONAI_CONTEXT_MONITOR", False),
|
||||
monitor_path=os.getenv("PRAISONAI_CONTEXT_MONITOR_PATH", "./context.txt"),
|
||||
monitor_format=os.getenv("PRAISONAI_CONTEXT_MONITOR_FORMAT", "human"),
|
||||
monitor_frequency=os.getenv("PRAISONAI_CONTEXT_MONITOR_FREQUENCY", "turn"),
|
||||
monitor_write_mode=os.getenv("PRAISONAI_CONTEXT_MONITOR_WRITE_MODE", "sync"),
|
||||
redact_sensitive=get_bool("PRAISONAI_CONTEXT_REDACT", True),
|
||||
source="env",
|
||||
)
|
||||
|
||||
def merge(self, **overrides) -> "ContextConfig":
|
||||
"""Create new config with overrides applied."""
|
||||
current = self.to_dict()
|
||||
|
||||
for key, value in overrides.items():
|
||||
if value is not None and key in current:
|
||||
current[key] = value
|
||||
|
||||
# Handle enum conversions
|
||||
if isinstance(current.get("strategy"), str):
|
||||
try:
|
||||
current["strategy"] = OptimizerStrategy(current["strategy"])
|
||||
except ValueError:
|
||||
current["strategy"] = OptimizerStrategy.SMART
|
||||
|
||||
return ContextConfig(**{k: v for k, v in current.items()
|
||||
if k in ContextConfig.__dataclass_fields__})
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
|
||||
@@ -37,11 +37,6 @@ Usage:
|
||||
"""
|
||||
|
||||
__all__ = [
|
||||
# Easy enable API
|
||||
"enable",
|
||||
"disable",
|
||||
"list_plugins",
|
||||
"is_enabled",
|
||||
# Core
|
||||
"PluginManager",
|
||||
"Plugin",
|
||||
@@ -70,170 +65,6 @@ __all__ = [
|
||||
]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# EASY ENABLE API
|
||||
# ============================================================================
|
||||
# These functions provide a simple way to enable/disable plugins globally.
|
||||
# Zero performance impact when not called - plugins only load on enable().
|
||||
# ============================================================================
|
||||
|
||||
# Global state for plugin system (lazy initialized)
|
||||
_plugins_enabled: bool = False
|
||||
_enabled_plugin_names: list = None # None = all, list = specific
|
||||
|
||||
|
||||
def enable(plugins: list = None) -> None:
|
||||
"""Enable the plugin system.
|
||||
|
||||
Discovers and enables plugins from default directories.
|
||||
This is the main entry point for using plugins.
|
||||
|
||||
Args:
|
||||
plugins: Optional list of plugin names to enable.
|
||||
If None, enables all discovered plugins.
|
||||
|
||||
Examples:
|
||||
# Enable all discovered plugins
|
||||
from praisonaiagents import plugins
|
||||
plugins.enable()
|
||||
|
||||
# Enable specific plugins only
|
||||
plugins.enable(["logging", "metrics"])
|
||||
|
||||
Note:
|
||||
- Tools and guardrails work WITHOUT calling enable()
|
||||
- Only background plugins (hooks, metrics, logging) need enable()
|
||||
- Can also be enabled via PRAISONAI_PLUGINS env var
|
||||
- Can also be enabled via .praisonai/config.toml
|
||||
"""
|
||||
global _plugins_enabled, _enabled_plugin_names
|
||||
|
||||
_plugins_enabled = True
|
||||
_enabled_plugin_names = plugins # None = all, list = specific
|
||||
|
||||
# Get plugin manager and auto-discover
|
||||
from .manager import get_plugin_manager
|
||||
manager = get_plugin_manager()
|
||||
|
||||
# Auto-discover plugins from default directories
|
||||
manager.auto_discover_plugins()
|
||||
|
||||
# Enable specific plugins or all
|
||||
if plugins is not None:
|
||||
# Enable only specified plugins
|
||||
for name in plugins:
|
||||
manager.enable(name)
|
||||
else:
|
||||
# Enable all discovered plugins
|
||||
for plugin_info in manager.list_plugins():
|
||||
manager.enable(plugin_info.get("name", ""))
|
||||
|
||||
import logging
|
||||
logging.debug(f"Plugins enabled: {plugins if plugins else 'all'}")
|
||||
|
||||
|
||||
def disable(plugins: list = None) -> None:
|
||||
"""Disable plugins.
|
||||
|
||||
Args:
|
||||
plugins: Optional list of plugin names to disable.
|
||||
If None, disables all plugins.
|
||||
|
||||
Examples:
|
||||
# Disable all plugins
|
||||
plugins.disable()
|
||||
|
||||
# Disable specific plugins
|
||||
plugins.disable(["logging"])
|
||||
"""
|
||||
global _plugins_enabled, _enabled_plugin_names
|
||||
|
||||
from .manager import get_plugin_manager
|
||||
manager = get_plugin_manager()
|
||||
|
||||
if plugins is not None:
|
||||
# Disable specific plugins
|
||||
for name in plugins:
|
||||
manager.disable(name)
|
||||
else:
|
||||
# Disable all plugins
|
||||
_plugins_enabled = False
|
||||
_enabled_plugin_names = None
|
||||
for plugin_info in manager.list_plugins():
|
||||
manager.disable(plugin_info.get("name", ""))
|
||||
|
||||
|
||||
def list_plugins() -> list:
|
||||
"""List all discovered plugins.
|
||||
|
||||
Returns:
|
||||
List of plugin info dicts with name, version, enabled status.
|
||||
|
||||
Example:
|
||||
from praisonaiagents import plugins
|
||||
all_plugins = plugins.list_plugins()
|
||||
for p in all_plugins:
|
||||
print(f"{p['name']} v{p['version']} - {'enabled' if p['enabled'] else 'disabled'}")
|
||||
"""
|
||||
from .manager import get_plugin_manager
|
||||
manager = get_plugin_manager()
|
||||
|
||||
# Auto-discover if not already done
|
||||
if not manager._plugins and not manager._single_file_plugins:
|
||||
manager.auto_discover_plugins()
|
||||
|
||||
# Combine registered plugins and single-file plugins
|
||||
result = []
|
||||
|
||||
# Add registered plugins (Plugin class instances)
|
||||
for info in manager.list_plugins():
|
||||
if hasattr(info, 'name'):
|
||||
# PluginInfo object
|
||||
result.append({
|
||||
"name": info.name,
|
||||
"version": getattr(info, 'version', '1.0.0'),
|
||||
"description": getattr(info, 'description', ''),
|
||||
"enabled": manager.is_enabled(info.name),
|
||||
"type": "registered",
|
||||
})
|
||||
elif isinstance(info, dict):
|
||||
# Already a dict
|
||||
info["enabled"] = manager.is_enabled(info.get("name", ""))
|
||||
info["type"] = "registered"
|
||||
result.append(info)
|
||||
|
||||
# Add single-file plugins
|
||||
for name, meta in manager._single_file_plugins.items():
|
||||
result.append({
|
||||
"name": name,
|
||||
"version": meta.get("version", "1.0.0"),
|
||||
"description": meta.get("description", ""),
|
||||
"enabled": manager.is_enabled(name),
|
||||
"type": "single_file",
|
||||
})
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def is_enabled(name: str = None) -> bool:
|
||||
"""Check if plugins are enabled.
|
||||
|
||||
Args:
|
||||
name: Optional plugin name to check. If None, checks if system is enabled.
|
||||
|
||||
Returns:
|
||||
True if enabled, False otherwise.
|
||||
"""
|
||||
global _plugins_enabled
|
||||
|
||||
if name is None:
|
||||
return _plugins_enabled
|
||||
|
||||
from .manager import get_plugin_manager
|
||||
manager = get_plugin_manager()
|
||||
return manager.is_enabled(name)
|
||||
|
||||
|
||||
def __getattr__(name: str):
|
||||
"""Lazy load module components."""
|
||||
# Core classes
|
||||
|
||||
@@ -81,7 +81,6 @@ class Task:
|
||||
# ============================================================
|
||||
skip_on_failure: bool = False, # Allow workflow to continue if this task fails
|
||||
retry_delay: float = 0.0, # Seconds between retries (supports exponential backoff)
|
||||
on_error: str = "stop", # Flow control: "stop", "continue", or "retry"
|
||||
# ============================================================
|
||||
# NEW PARAMS FROM WORKFLOWSTEP (Phase 4 Consolidation)
|
||||
# ============================================================
|
||||
@@ -99,11 +98,6 @@ class Task:
|
||||
routing: Optional[Dict[str, List[str]]] = None, # Renamed from condition for clarity
|
||||
output_config: Optional[Any] = None,
|
||||
# ============================================================
|
||||
# UNIFIED OUTPUT PARAMETER (consolidates all output configs)
|
||||
# ============================================================
|
||||
# Single output param that handles: file path (str), Pydantic model, TaskOutputConfig
|
||||
output: Optional[Any] = None,
|
||||
# ============================================================
|
||||
# UNIFIED CONDITION SYNTAX (same as AgentFlow)
|
||||
# ============================================================
|
||||
# String-based condition expression (e.g., "{{score}} > 80")
|
||||
@@ -232,85 +226,12 @@ class Task:
|
||||
self.loop_var = loop_var
|
||||
# Consolidated config objects (from Task)
|
||||
self.execution = execution
|
||||
# ============================================================
|
||||
# RESOLVE EXECUTION CONFIG FIELDS TO TASK PROPERTIES
|
||||
# Precedence: Direct params (if non-default) > Config object > Defaults
|
||||
# We check if direct param equals its default to determine if user set it
|
||||
# ============================================================
|
||||
if execution is not None:
|
||||
# Resolve async_exec -> async_execution
|
||||
# Only apply config if direct param was NOT explicitly set (is default False)
|
||||
if hasattr(execution, 'async_exec'):
|
||||
# async_execution default is False, so if it's False, use config value
|
||||
if not async_execution: # direct param is False (default)
|
||||
self.async_execution = execution.async_exec
|
||||
# Resolve quality_check
|
||||
# quality_check default is True, so if it's True, use config value
|
||||
if hasattr(execution, 'quality_check'):
|
||||
if quality_check: # direct param is True (default)
|
||||
self.quality_check = execution.quality_check
|
||||
# Resolve max_retries
|
||||
# max_retries default is 3, so if it's 3, use config value
|
||||
if hasattr(execution, 'max_retries'):
|
||||
if max_retries == 3: # direct param is 3 (default)
|
||||
self.max_retries = execution.max_retries
|
||||
# Resolve rerun
|
||||
# rerun default is False, so if it's False, use config value
|
||||
if hasattr(execution, 'rerun'):
|
||||
if not rerun: # direct param is False (default)
|
||||
self.rerun = execution.rerun
|
||||
# Resolve on_error (config takes precedence for backward compat)
|
||||
if hasattr(execution, 'on_error'):
|
||||
self.on_error = execution.on_error
|
||||
else:
|
||||
self.on_error = on_error
|
||||
else:
|
||||
self.on_error = on_error
|
||||
# Handle routing parameter - use routing if provided, else fall back to condition
|
||||
if routing is not None:
|
||||
self.condition = routing
|
||||
self.routing = self.condition # Alias for backward compat
|
||||
self.output_config = output_config
|
||||
# ============================================================
|
||||
# UNIFIED OUTPUT PARAMETER RESOLUTION
|
||||
# ============================================================
|
||||
# Resolve unified 'output' param (consolidates output_file, output_json, output_pydantic, output_config)
|
||||
# Priority: unified 'output' param > individual params (for backward compat)
|
||||
if output is not None:
|
||||
if isinstance(output, str):
|
||||
# String = file path
|
||||
self.output_file = output
|
||||
elif hasattr(output, 'model_fields') or hasattr(output, '__fields__'):
|
||||
# Pydantic model class (v2 or v1)
|
||||
if isinstance(output, type):
|
||||
# It's a class, use for output_json
|
||||
self.output_json = output
|
||||
else:
|
||||
# It's an instance, use for output_pydantic
|
||||
self.output_pydantic = type(output)
|
||||
elif hasattr(output, 'file') or hasattr(output, 'variable'):
|
||||
# TaskOutputConfig or similar config object
|
||||
if hasattr(output, 'file') and output.file:
|
||||
self.output_file = output.file
|
||||
if hasattr(output, 'json_model') and output.json_model:
|
||||
self.output_json = output.json_model
|
||||
if hasattr(output, 'pydantic_model') and output.pydantic_model:
|
||||
self.output_pydantic = output.pydantic_model
|
||||
if hasattr(output, 'variable') and output.variable:
|
||||
self.output_variable = output.variable
|
||||
elif isinstance(output, dict):
|
||||
# Dict config
|
||||
if 'file' in output:
|
||||
self.output_file = output['file']
|
||||
if 'json_model' in output or 'json' in output:
|
||||
self.output_json = output.get('json_model') or output.get('json')
|
||||
if 'pydantic_model' in output or 'pydantic' in output:
|
||||
self.output_pydantic = output.get('pydantic_model') or output.get('pydantic')
|
||||
if 'variable' in output:
|
||||
self.output_variable = output.get('variable')
|
||||
# Store the original output param for reference
|
||||
self.output = output
|
||||
# ============================================================
|
||||
# UNIFIED CONDITION SYNTAX (same as AgentFlow)
|
||||
# ============================================================
|
||||
self.when = when
|
||||
@@ -325,11 +246,7 @@ class Task:
|
||||
self.hooks = hooks
|
||||
self.caching = caching
|
||||
# Output variable name - for storing output in workflow variables
|
||||
# Only set from param if explicitly provided (unified output may have set it)
|
||||
if output_variable is not None:
|
||||
self.output_variable = output_variable
|
||||
elif not hasattr(self, 'output_variable'):
|
||||
self.output_variable = None
|
||||
self.output_variable = output_variable
|
||||
|
||||
# Set logger level based on config verbose level
|
||||
verbose = self.config.get("verbose", 0)
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "praisonaiagents"
|
||||
version = "1.4.4"
|
||||
version = "1.4.1"
|
||||
description = "Praison AI agents for completing complex tasks with Self Reflection Agents"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
|
||||
@@ -172,20 +172,15 @@ class TestWorkflowDictParams:
|
||||
assert workflow.memory is not None
|
||||
|
||||
def test_workflowstep_context_dict(self):
|
||||
"""Task accepts context as dict (via TaskContextConfig)."""
|
||||
"""Task accepts context as dict."""
|
||||
from praisonaiagents.workflows.workflows import Task
|
||||
from praisonaiagents.workflows.workflow_configs import TaskContextConfig
|
||||
|
||||
step = Task(
|
||||
name="step1",
|
||||
action="do something",
|
||||
context=TaskContextConfig(from_steps=["step0"], retain_full=True),
|
||||
context={"from_steps": ["step0"], "retain_full": True},
|
||||
)
|
||||
# context stores the TaskContextConfig object
|
||||
assert hasattr(step.context, 'from_steps')
|
||||
assert step.context.from_steps == ["step0"]
|
||||
# retain_full_context is a separate param on Task
|
||||
assert step.context.retain_full is True
|
||||
assert step.context_from == ["step0"]
|
||||
assert step.retain_full_context is True
|
||||
|
||||
def test_workflowstep_output_dict(self):
|
||||
"""Task accepts output as dict."""
|
||||
@@ -193,35 +188,31 @@ class TestWorkflowDictParams:
|
||||
|
||||
step = Task(
|
||||
name="step1",
|
||||
action="do something",
|
||||
output={"file": "output.txt", "variable": "result"},
|
||||
)
|
||||
assert step.output_file == "output.txt"
|
||||
assert step.output_variable == "result"
|
||||
|
||||
def test_workflowstep_execution_dict(self):
|
||||
"""Task accepts execution as dict (params passed directly)."""
|
||||
"""Task accepts execution as dict."""
|
||||
from praisonaiagents.workflows.workflows import Task
|
||||
|
||||
step = Task(
|
||||
name="step1",
|
||||
action="do something",
|
||||
max_retries=5,
|
||||
quality_check=True,
|
||||
execution={"max_retries": 5, "quality_check": True},
|
||||
)
|
||||
assert step.max_retries == 5
|
||||
assert step.quality_check is True
|
||||
|
||||
def test_workflowstep_routing_dict(self):
|
||||
"""Task accepts routing as dict (via next_tasks param)."""
|
||||
"""Task accepts routing as dict."""
|
||||
from praisonaiagents.workflows.workflows import Task
|
||||
|
||||
step = Task(
|
||||
name="step1",
|
||||
action="do something",
|
||||
next_tasks=["step2", "step3"],
|
||||
routing={"next_steps": ["step2", "step3"]},
|
||||
)
|
||||
assert step.next_tasks == ["step2", "step3"]
|
||||
assert step.next_steps == ["step2", "step3"]
|
||||
|
||||
|
||||
# =============================================================================
|
||||
@@ -306,11 +297,9 @@ class TestBackwardCompatibility:
|
||||
|
||||
step = Task(
|
||||
name="step1",
|
||||
action="do something",
|
||||
context=["step0", "step_prev"],
|
||||
)
|
||||
# context stores the list directly
|
||||
assert step.context == ["step0", "step_prev"]
|
||||
assert step.context_from == ["step0", "step_prev"]
|
||||
|
||||
|
||||
# =============================================================================
|
||||
|
||||
@@ -15,18 +15,15 @@ import time
|
||||
|
||||
# Skip entire module if MCP package is not installed
|
||||
try:
|
||||
import mcp
|
||||
from praisonaiagents.mcp import MCP
|
||||
from praisonaiagents import Agent
|
||||
MCP_AVAILABLE = True
|
||||
except ImportError:
|
||||
MCP_AVAILABLE = False
|
||||
MCP = None
|
||||
Agent = None
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.skipif(not MCP_AVAILABLE, reason="MCP package not installed. Install with: pip install praisonaiagents[mcp]")
|
||||
@pytest.mark.skipif(not MCP_AVAILABLE, reason="MCP package not installed")
|
||||
class TestMultiAgentMCP:
|
||||
"""Integration tests for multiple agents with MCP."""
|
||||
|
||||
|
||||
@@ -11,9 +11,6 @@ Tests the HTTPStreamTransport implementation including:
|
||||
|
||||
import pytest
|
||||
|
||||
# Skip all tests in this module if MCP is not installed
|
||||
pytest.importorskip("mcp", reason="MCP module not installed. Install with: pip install praisonaiagents[mcp]")
|
||||
|
||||
|
||||
class TestHTTPStreamTransportInit:
|
||||
"""Test HTTPStreamTransport initialization."""
|
||||
|
||||
@@ -6,9 +6,6 @@ These tests define the expected behavior of ToolsMCPServer class.
|
||||
import pytest
|
||||
from unittest.mock import patch
|
||||
|
||||
# Skip all tests in this module if MCP is not installed
|
||||
pytest.importorskip("mcp", reason="MCP module not installed. Install with: pip install praisonaiagents[mcp]")
|
||||
|
||||
|
||||
class TestToolsMCPServerInit:
|
||||
"""Test ToolsMCPServer initialization."""
|
||||
|
||||
@@ -262,14 +262,6 @@ class TestWebSocketKeepalive:
|
||||
|
||||
def test_ping_interval_configurable(self):
|
||||
"""Test that ping interval is configurable."""
|
||||
import asyncio
|
||||
# Create event loop for asyncio.Queue() used in __init__
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
except RuntimeError:
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
|
||||
from praisonaiagents.mcp.mcp_websocket import WebSocketTransport
|
||||
|
||||
transport = WebSocketTransport(
|
||||
|
||||
@@ -339,7 +339,7 @@ class TestRulesManager:
|
||||
@pytest.fixture
|
||||
def rules_dir(self, tmp_path):
|
||||
"""Create a temp rules directory."""
|
||||
rules_path = tmp_path / ".praisonai" / "rules"
|
||||
rules_path = tmp_path / ".praison" / "rules"
|
||||
rules_path.mkdir(parents=True)
|
||||
return rules_path
|
||||
|
||||
@@ -363,7 +363,7 @@ class TestRulesManager:
|
||||
assert "**/*.py" in rule.globs
|
||||
|
||||
# Verify file was created
|
||||
rule_file = tmp_path / ".praisonai" / "rules" / "python_style.md"
|
||||
rule_file = tmp_path / ".praison" / "rules" / "python_style.md"
|
||||
assert rule_file.exists()
|
||||
|
||||
def test_load_rule_with_frontmatter(self, rules_dir, tmp_path):
|
||||
@@ -378,10 +378,10 @@ priority: 10
|
||||
# Test Rule
|
||||
- Rule content here
|
||||
"""
|
||||
(rules_dir / "unique_frontmatter_test.md").write_text(rule_content)
|
||||
(rules_dir / "test_rule.md").write_text(rule_content)
|
||||
|
||||
manager = RulesManager(workspace_path=str(tmp_path))
|
||||
rule = manager.get_rule_by_name("unique_frontmatter_test")
|
||||
rule = manager.get_rule_by_name("test_rule")
|
||||
|
||||
assert rule is not None
|
||||
assert rule.description == "Test rule"
|
||||
|
||||
@@ -252,7 +252,7 @@ class TestAiDecisionActivation(unittest.TestCase):
|
||||
def test_ai_decision_rule_loaded(self):
|
||||
"""Test that ai_decision rules are loaded."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
rules_dir = Path(tmpdir) / ".praisonai" / "rules"
|
||||
rules_dir = Path(tmpdir) / ".praison" / "rules"
|
||||
rules_dir.mkdir(parents=True)
|
||||
|
||||
rule_content = """---
|
||||
@@ -273,7 +273,7 @@ activation: ai_decision
|
||||
def test_evaluate_ai_decision_without_llm(self):
|
||||
"""Test ai_decision evaluation without LLM (defaults to True)."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
rules_dir = Path(tmpdir) / ".praisonai" / "rules"
|
||||
rules_dir = Path(tmpdir) / ".praison" / "rules"
|
||||
rules_dir.mkdir(parents=True)
|
||||
|
||||
rule_content = """---
|
||||
@@ -365,7 +365,7 @@ class TestWorkflows(unittest.TestCase):
|
||||
from praisonaiagents.memory import WorkflowManager
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
workflows_dir = Path(tmpdir) / ".praisonai" / "workflows"
|
||||
workflows_dir = Path(tmpdir) / ".praison" / "workflows"
|
||||
workflows_dir.mkdir(parents=True)
|
||||
|
||||
workflow_content = """---
|
||||
@@ -401,7 +401,7 @@ Build application
|
||||
from praisonaiagents.memory import WorkflowManager
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
workflows_dir = Path(tmpdir) / ".praisonai" / "workflows"
|
||||
workflows_dir = Path(tmpdir) / ".praison" / "workflows"
|
||||
workflows_dir.mkdir(parents=True)
|
||||
|
||||
workflow_content = """---
|
||||
@@ -462,7 +462,7 @@ class TestHooks(unittest.TestCase):
|
||||
from praisonaiagents.memory import HooksManager
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
config_dir = Path(tmpdir) / ".praisonai"
|
||||
config_dir = Path(tmpdir) / ".praison"
|
||||
config_dir.mkdir()
|
||||
|
||||
config = {
|
||||
@@ -526,7 +526,7 @@ class TestHooks(unittest.TestCase):
|
||||
timeout=60
|
||||
)
|
||||
|
||||
config_path = Path(tmpdir) / ".praisonai" / "hooks.json"
|
||||
config_path = Path(tmpdir) / ".praison" / "hooks.json"
|
||||
self.assertTrue(config_path.exists())
|
||||
|
||||
config = json.loads(config_path.read_text())
|
||||
|
||||
@@ -614,7 +614,6 @@ class TestPluginDiscovery:
|
||||
assert count2 == 0
|
||||
assert _count1 >= 0 # Use variable to avoid lint warning
|
||||
|
||||
@pytest.mark.skip(reason="Requires pytest-mock package")
|
||||
def test_discover_plugins_with_mock_entry_point(self, mocker):
|
||||
"""Should load tools from mocked entry_points."""
|
||||
from praisonaiagents import ToolRegistry, BaseTool
|
||||
@@ -651,7 +650,6 @@ class TestPluginDiscovery:
|
||||
tool = registry.get("mock_plugin")
|
||||
assert tool.run() == "mocked"
|
||||
|
||||
@pytest.mark.skip(reason="Requires pytest-mock package")
|
||||
def test_discover_plugins_handles_load_error(self, mocker):
|
||||
"""Should handle errors when loading plugins gracefully."""
|
||||
from praisonaiagents import ToolRegistry
|
||||
@@ -679,7 +677,6 @@ class TestPluginDiscovery:
|
||||
assert count == 0
|
||||
assert "broken_plugin" not in registry
|
||||
|
||||
@pytest.mark.skip(reason="Requires pytest-mock package")
|
||||
def test_discover_callable_function(self, mocker):
|
||||
"""Should register plain functions from entry_points."""
|
||||
from praisonaiagents import ToolRegistry
|
||||
@@ -709,7 +706,6 @@ class TestPluginDiscovery:
|
||||
func = registry.get("func_plugin")
|
||||
assert func("test") == "plugin: test"
|
||||
|
||||
@pytest.mark.skip(reason="Requires pytest-mock package")
|
||||
def test_auto_discover_on_get_if_not_found(self, mocker):
|
||||
"""get() should trigger discovery if tool not found."""
|
||||
from praisonaiagents import ToolRegistry, BaseTool
|
||||
|
||||
@@ -98,7 +98,6 @@ class TestStructuredOutputWiring:
|
||||
agent._yaml_action = "Test action"
|
||||
agent._yaml_output_variable = "result"
|
||||
agent._yaml_output_json = {"type": "array", "items": {"type": "string"}}
|
||||
agent._yaml_output_pydantic = None # Explicitly None to avoid conflict
|
||||
agent._yaml_step_name = "test_step"
|
||||
|
||||
# Mock the chat method
|
||||
@@ -110,7 +109,7 @@ class TestStructuredOutputWiring:
|
||||
|
||||
# Verify Task has output_json
|
||||
assert isinstance(normalized, Task)
|
||||
assert normalized.output_json == {"type": "array", "items": {"type": "string"}}
|
||||
assert normalized._output_json == {"type": "array", "items": {"type": "string"}}
|
||||
|
||||
def test_normalize_step_includes_output_pydantic(self):
|
||||
"""Test that _normalize_single_step transfers output_pydantic to Task."""
|
||||
@@ -122,7 +121,6 @@ class TestStructuredOutputWiring:
|
||||
agent.tools = None
|
||||
agent._yaml_action = "Test action"
|
||||
agent._yaml_output_variable = "result"
|
||||
agent._yaml_output_json = None # Explicitly None to avoid conflict
|
||||
agent._yaml_output_pydantic = "TopicList"
|
||||
agent._yaml_step_name = "test_step"
|
||||
agent.chat = Mock(return_value="test output")
|
||||
@@ -131,7 +129,7 @@ class TestStructuredOutputWiring:
|
||||
normalized = workflow._normalize_single_step(agent, 0)
|
||||
|
||||
assert isinstance(normalized, Task)
|
||||
assert normalized.output_pydantic == "TopicList"
|
||||
assert normalized._output_pydantic == "TopicList"
|
||||
|
||||
|
||||
class TestStructuredOutputExecution:
|
||||
@@ -146,12 +144,12 @@ class TestStructuredOutputExecution:
|
||||
mock_agent.name = "test_agent"
|
||||
mock_agent.chat = Mock(return_value='["topic1", "topic2"]')
|
||||
|
||||
# Create step with output_json (the actual SDK parameter)
|
||||
# Create step with output_json
|
||||
step = Task(
|
||||
name="test_step",
|
||||
agent=mock_agent,
|
||||
action="Find topics",
|
||||
output_json={"type": "array", "items": {"type": "string"}}
|
||||
output={"json_model": {"type": "array", "items": {"type": "string"}}}
|
||||
)
|
||||
|
||||
# Create and run workflow
|
||||
|
||||
@@ -49,10 +49,10 @@ class TestTaskAgentFields:
|
||||
step = Task(name="test_step", action="do something")
|
||||
assert step.agent_config is None
|
||||
|
||||
def test_workflow_step_tools_default_empty_list(self):
|
||||
"""tools should default to empty list."""
|
||||
def test_workflow_step_tools_default_none(self):
|
||||
"""tools should default to None."""
|
||||
step = Task(name="test_step", action="do something")
|
||||
assert step.tools == []
|
||||
assert step.tools is None
|
||||
|
||||
def test_workflow_step_to_dict_includes_agent_fields(self):
|
||||
"""to_dict() should include agent-related fields."""
|
||||
@@ -82,25 +82,25 @@ class TestWorkflowDefaultAgentConfig:
|
||||
assert hasattr(workflow, 'default_agent_config')
|
||||
assert workflow.default_agent_config["role"] == "Assistant"
|
||||
|
||||
def test_workflow_has_llm(self):
|
||||
"""Workflow should have llm field (for default LLM)."""
|
||||
def test_workflow_has_default_llm(self):
|
||||
"""Workflow should have default_llm field."""
|
||||
workflow = Workflow(
|
||||
name="test_workflow",
|
||||
llm="gpt-4o-mini"
|
||||
default_llm="gpt-4o-mini"
|
||||
)
|
||||
assert hasattr(workflow, 'llm')
|
||||
assert workflow.llm == "gpt-4o-mini"
|
||||
assert hasattr(workflow, 'default_llm')
|
||||
assert workflow.default_llm == "gpt-4o-mini"
|
||||
|
||||
def test_workflow_to_dict_includes_defaults(self):
|
||||
"""to_dict() should include default agent config fields."""
|
||||
workflow = Workflow(
|
||||
name="test_workflow",
|
||||
default_agent_config={"role": "Helper"},
|
||||
llm="gpt-4o"
|
||||
default_llm="gpt-4o"
|
||||
)
|
||||
d = workflow.to_dict()
|
||||
assert "default_agent_config" in d
|
||||
assert "default_llm" in d # to_dict still uses default_llm key for serialization
|
||||
assert "default_llm" in d
|
||||
|
||||
|
||||
class TestCreateStepAgent:
|
||||
@@ -341,7 +341,7 @@ class TestWorkflowLevelDefaults:
|
||||
|
||||
workflow = Workflow(
|
||||
name="test_workflow",
|
||||
llm="gpt-4o",
|
||||
default_llm="gpt-4o",
|
||||
steps=[
|
||||
Task(
|
||||
name="step1",
|
||||
@@ -368,7 +368,7 @@ class TestWorkflowLevelDefaults:
|
||||
|
||||
workflow = Workflow(
|
||||
name="test_workflow",
|
||||
llm="gpt-3.5-turbo",
|
||||
default_llm="gpt-3.5-turbo",
|
||||
steps=[
|
||||
Task(
|
||||
name="step1",
|
||||
|
||||
@@ -21,23 +21,20 @@ class TestTaskContextFields:
|
||||
"""Test Task dataclass context-related fields."""
|
||||
|
||||
def test_workflow_step_has_context_from_field(self):
|
||||
"""Task should have context field that stores TaskContextConfig."""
|
||||
"""Task should have context_from via consolidated context param."""
|
||||
step = Task(
|
||||
name="test_step",
|
||||
action="do something",
|
||||
context=TaskContextConfig(from_steps=["step1", "step2"])
|
||||
)
|
||||
# context stores the TaskContextConfig object directly
|
||||
assert hasattr(step, 'context')
|
||||
assert hasattr(step.context, 'from_steps')
|
||||
assert step.context.from_steps == ["step1", "step2"]
|
||||
assert hasattr(step, 'context_from')
|
||||
assert step.context_from == ["step1", "step2"]
|
||||
|
||||
def test_workflow_step_has_retain_full_context_field(self):
|
||||
"""Task should have retain_full_context field with default False."""
|
||||
"""Task should have retain_full_context field with default True."""
|
||||
step = Task(name="test_step", action="do something")
|
||||
assert hasattr(step, 'retain_full_context')
|
||||
# Default is False (opt-in for full context retention)
|
||||
assert step.retain_full_context == False
|
||||
assert step.retain_full_context == True
|
||||
|
||||
def test_workflow_step_has_output_variable_field(self):
|
||||
"""Task should have output_variable via consolidated output param."""
|
||||
@@ -51,18 +48,16 @@ class TestTaskContextFields:
|
||||
|
||||
def test_workflow_step_to_dict_includes_new_fields(self):
|
||||
"""to_dict() should include context-related fields."""
|
||||
# Use list-based context (the standard format for to_dict)
|
||||
step = Task(
|
||||
name="test_step",
|
||||
action="do something",
|
||||
context=["step1"],
|
||||
context=TaskContextConfig(from_steps=["step1"], retain_full=False),
|
||||
output=TaskOutputConfig(variable="result")
|
||||
)
|
||||
d = step.to_dict()
|
||||
# Check that context and output_variable are in dict
|
||||
assert "context" in d
|
||||
assert "output_variable" in d
|
||||
assert d["output_variable"] == "result"
|
||||
# Check that consolidated params are in dict
|
||||
assert "context" in d or "context_from" in d
|
||||
assert "output" in d or "output_variable" in d
|
||||
|
||||
|
||||
class TestContextPassing:
|
||||
|
||||
@@ -130,11 +130,9 @@ Write summary
|
||||
steps = manager._parse_steps(body)
|
||||
|
||||
assert len(steps) == 3
|
||||
# context_from is now accessed via context.from_steps (TaskContextConfig)
|
||||
assert steps[2].context is not None
|
||||
assert hasattr(steps[2].context, 'from_steps')
|
||||
assert "Research" in steps[2].context.from_steps
|
||||
assert "Analyze" in steps[2].context.from_steps
|
||||
assert steps[2].context_from is not None
|
||||
assert "Research" in steps[2].context_from
|
||||
assert "Analyze" in steps[2].context_from
|
||||
|
||||
|
||||
class TestParseRetainFullContext:
|
||||
@@ -176,13 +174,7 @@ Process the data
|
||||
steps = manager._parse_steps(body)
|
||||
|
||||
assert len(steps) == 1
|
||||
# When only retain_full_context: true is set (the default), no TaskContextConfig is created
|
||||
# This is because the SDK only creates config when: context_from is set OR retain_full is False
|
||||
# So context will be empty list (default from Task.__init__)
|
||||
context = steps[0].context
|
||||
assert context == [] or context is None or (
|
||||
hasattr(context, 'retain_full') and context.retain_full is True
|
||||
)
|
||||
assert steps[0].retain_full_context is True
|
||||
|
||||
def test_retain_full_context_default_true(self):
|
||||
"""retain_full_context should default to True."""
|
||||
@@ -199,9 +191,7 @@ Process the data
|
||||
steps = manager._parse_steps(body)
|
||||
|
||||
assert len(steps) == 1
|
||||
# When retain_full_context is not explicitly set, context may be None or empty list
|
||||
# Default behavior is to retain full context (no filtering)
|
||||
assert steps[0].context is None or (hasattr(steps[0].context, 'retain_full') and steps[0].context.retain_full is True) or steps[0].context == []
|
||||
assert steps[0].retain_full_context is True
|
||||
|
||||
|
||||
class TestParseOutputVariable:
|
||||
@@ -259,8 +249,7 @@ Do something
|
||||
workflow = manager._load_workflow(Path(temp_path))
|
||||
|
||||
assert workflow is not None
|
||||
# AgentFlow uses 'llm' field, not 'default_llm'
|
||||
assert workflow.llm == "gpt-4o"
|
||||
assert workflow.default_llm == "gpt-4o"
|
||||
# Planning is now a config object, check _planning_enabled
|
||||
assert workflow._planning_enabled is True
|
||||
assert workflow._planning_llm == "gpt-4o-mini"
|
||||
@@ -338,9 +327,8 @@ Write report based on analysis
|
||||
# Step 2
|
||||
assert steps[1].name == "Analyze"
|
||||
assert steps[1].agent_config["role"] == "Analyst"
|
||||
# context_from is now accessed via context.from_steps (TaskContextConfig)
|
||||
assert steps[1].context.from_steps == ["Research"]
|
||||
assert steps[1].context.retain_full is False
|
||||
assert steps[1].context_from == ["Research"]
|
||||
assert steps[1].retain_full_context is False
|
||||
|
||||
# Step 3
|
||||
assert steps[2].name == "Write"
|
||||
|
||||
@@ -697,13 +697,12 @@ class TestGuardrails:
|
||||
return StepResult(output=f"Attempt {attempt[0]}")
|
||||
|
||||
workflow = Workflow(steps=[
|
||||
Task(name="step1", handler=step1, guardrails=validator, max_retries=2)
|
||||
Task(name="step1", handler=step1, guardrails=validator, execution=TaskExecutionConfig(max_retries=2))
|
||||
])
|
||||
result = workflow.start("test")
|
||||
|
||||
# Should have tried up to max_retries + 1 times (initial + retries)
|
||||
# The actual behavior may vary based on implementation
|
||||
assert attempt[0] >= 2 # At least initial + 1 retry
|
||||
# Should have tried 3 times (initial + 2 retries)
|
||||
assert attempt[0] == 3
|
||||
|
||||
def test_validation_feedback_in_context(self):
|
||||
"""Test validation feedback is passed to context on retry."""
|
||||
@@ -1208,12 +1207,11 @@ class TestWorkflowConfiguration:
|
||||
assert workflow.planning_llm == "gpt-4o-mini"
|
||||
|
||||
def test_workflow_default_llm(self):
|
||||
"""Test default LLM setting (renamed from default_llm to llm)."""
|
||||
"""Test default LLM setting."""
|
||||
def step1(ctx): return StepResult(output="Done")
|
||||
|
||||
# Note: default_llm was renamed to llm for consistency with Agent API
|
||||
workflow = Workflow(steps=[step1], llm="gpt-4o-mini")
|
||||
assert workflow.llm == "gpt-4o-mini"
|
||||
workflow = Workflow(steps=[step1], default_llm="gpt-4o-mini")
|
||||
assert workflow.default_llm == "gpt-4o-mini"
|
||||
|
||||
def test_workflow_default_agent_config(self):
|
||||
"""Test default agent config."""
|
||||
@@ -1328,24 +1326,24 @@ class TestMigratedFeatures:
|
||||
assert step.output_pydantic == OutputModel
|
||||
|
||||
def test_step_async_execution_parameter(self):
|
||||
"""Test Task with async_execution parameter (passed directly)."""
|
||||
"""Test Task with async_execution parameter."""
|
||||
step = Task(
|
||||
name="async_step",
|
||||
action="Run async",
|
||||
async_execution=True
|
||||
execution=TaskExecutionConfig(async_exec=True)
|
||||
)
|
||||
|
||||
assert step.async_execution
|
||||
assert step.async_execution == True
|
||||
|
||||
def test_step_quality_check_parameter(self):
|
||||
"""Test Task with quality_check parameter (passed directly)."""
|
||||
"""Test Task with quality_check parameter."""
|
||||
step = Task(
|
||||
name="quality_step",
|
||||
action="Check quality",
|
||||
quality_check=False
|
||||
execution=TaskExecutionConfig(quality_check=False)
|
||||
)
|
||||
|
||||
assert not step.quality_check
|
||||
assert step.quality_check == False
|
||||
|
||||
def test_step_rerun_parameter(self):
|
||||
"""Test Task with rerun parameter."""
|
||||
@@ -1376,19 +1374,20 @@ class TestMigratedFeatures:
|
||||
assert callable(workflow.arun)
|
||||
|
||||
def test_step_to_dict_includes_new_fields(self):
|
||||
"""Test that to_dict includes key fields."""
|
||||
"""Test that to_dict includes all new fields."""
|
||||
step = Task(
|
||||
name="test_step",
|
||||
action="Test action",
|
||||
output_file="output.txt",
|
||||
async_execution=True
|
||||
output=TaskOutputConfig(file="output.txt"),
|
||||
images=["img.jpg"],
|
||||
execution=TaskExecutionConfig(async_exec=True, quality_check=False, rerun=False)
|
||||
)
|
||||
|
||||
d = step.to_dict()
|
||||
|
||||
assert d["output_file"] == "output.txt"
|
||||
assert d["async_execution"]
|
||||
assert d["name"] == "test_step"
|
||||
assert d["output"]["file"] == "output.txt"
|
||||
assert d["images"] == ["img.jpg"]
|
||||
assert d["execution"]["async"] == True
|
||||
|
||||
|
||||
# =============================================================================
|
||||
@@ -1547,7 +1546,7 @@ Process general request
|
||||
```
|
||||
"""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
workflows_dir = os.path.join(tmpdir, ".praisonai", "workflows")
|
||||
workflows_dir = os.path.join(tmpdir, ".praison", "workflows")
|
||||
os.makedirs(workflows_dir)
|
||||
|
||||
with open(os.path.join(workflows_dir, "route_test.md"), "w") as f:
|
||||
@@ -1563,8 +1562,8 @@ Process general request
|
||||
# Check that route pattern is parsed
|
||||
step1 = workflow.steps[0]
|
||||
assert step1.name == "Classifier"
|
||||
# Task uses 'condition' or 'routing' for branch conditions
|
||||
assert step1.condition is not None or step1.routing is not None
|
||||
assert step1.branch_condition is not None
|
||||
assert "technical" in step1.branch_condition
|
||||
|
||||
def test_parse_parallel_pattern_in_md(self):
|
||||
"""Test parsing parallel pattern from markdown."""
|
||||
@@ -1612,7 +1611,7 @@ Survey customers
|
||||
```
|
||||
"""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
workflows_dir = os.path.join(tmpdir, ".praisonai", "workflows")
|
||||
workflows_dir = os.path.join(tmpdir, ".praison", "workflows")
|
||||
os.makedirs(workflows_dir)
|
||||
|
||||
with open(os.path.join(workflows_dir, "parallel_test.md"), "w") as f:
|
||||
@@ -1628,8 +1627,7 @@ Survey customers
|
||||
# Check that parallel steps are parsed
|
||||
step1 = workflow.steps[0]
|
||||
assert step1.name == "Research"
|
||||
# Task uses 'next_tasks' for next steps
|
||||
assert hasattr(step1, 'next_tasks')
|
||||
assert hasattr(step1, 'parallel_steps') or step1.next_steps is not None
|
||||
|
||||
def test_parse_loop_pattern_in_md(self):
|
||||
"""Test parsing loop pattern from markdown."""
|
||||
@@ -1655,7 +1653,7 @@ Process {{current_item}}
|
||||
```
|
||||
"""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
workflows_dir = os.path.join(tmpdir, ".praisonai", "workflows")
|
||||
workflows_dir = os.path.join(tmpdir, ".praison", "workflows")
|
||||
os.makedirs(workflows_dir)
|
||||
|
||||
with open(os.path.join(workflows_dir, "loop_test.md"), "w") as f:
|
||||
@@ -1696,7 +1694,7 @@ Generate more content
|
||||
```
|
||||
"""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
workflows_dir = os.path.join(tmpdir, ".praisonai", "workflows")
|
||||
workflows_dir = os.path.join(tmpdir, ".praison", "workflows")
|
||||
os.makedirs(workflows_dir)
|
||||
|
||||
with open(os.path.join(workflows_dir, "repeat_test.md"), "w") as f:
|
||||
@@ -1734,7 +1732,7 @@ Generate the report
|
||||
```
|
||||
"""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
workflows_dir = os.path.join(tmpdir, ".praisonai", "workflows")
|
||||
workflows_dir = os.path.join(tmpdir, ".praison", "workflows")
|
||||
os.makedirs(workflows_dir)
|
||||
|
||||
with open(os.path.join(workflows_dir, "output_test.md"), "w") as f:
|
||||
@@ -1774,7 +1772,7 @@ Analyze these images
|
||||
```
|
||||
"""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
workflows_dir = os.path.join(tmpdir, ".praisonai", "workflows")
|
||||
workflows_dir = os.path.join(tmpdir, ".praison", "workflows")
|
||||
os.makedirs(workflows_dir)
|
||||
|
||||
with open(os.path.join(workflows_dir, "vision_test.md"), "w") as f:
|
||||
|
||||
@@ -1,283 +0,0 @@
|
||||
"""
|
||||
Unit tests for plugins.enable() API and config-driven defaults.
|
||||
|
||||
Tests:
|
||||
- plugins.enable() / plugins.disable()
|
||||
- PRAISONAI_PLUGINS env var
|
||||
- Config file loading
|
||||
- Config-driven defaults for Agent parameters
|
||||
"""
|
||||
|
||||
import os
|
||||
import pytest
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
class TestPluginsEnableAPI:
|
||||
"""Test the plugins.enable() API."""
|
||||
|
||||
def test_enable_all_plugins(self):
|
||||
"""Test enabling all plugins."""
|
||||
from praisonaiagents import plugins
|
||||
|
||||
# Reset state
|
||||
plugins._plugins_enabled = False
|
||||
plugins._enabled_plugin_names = None
|
||||
|
||||
# Enable all
|
||||
plugins.enable()
|
||||
|
||||
assert plugins.is_enabled() is True
|
||||
assert plugins._enabled_plugin_names is None # None means all
|
||||
|
||||
def test_enable_specific_plugins(self):
|
||||
"""Test enabling specific plugins by name."""
|
||||
from praisonaiagents import plugins
|
||||
|
||||
# Reset state
|
||||
plugins._plugins_enabled = False
|
||||
plugins._enabled_plugin_names = None
|
||||
|
||||
# Enable specific plugins
|
||||
plugins.enable(["logging", "metrics"])
|
||||
|
||||
assert plugins.is_enabled() is True
|
||||
assert plugins._enabled_plugin_names == ["logging", "metrics"]
|
||||
|
||||
def test_disable_all_plugins(self):
|
||||
"""Test disabling all plugins."""
|
||||
from praisonaiagents import plugins
|
||||
|
||||
# First enable
|
||||
plugins._plugins_enabled = True
|
||||
plugins._enabled_plugin_names = None
|
||||
|
||||
# Then disable
|
||||
plugins.disable()
|
||||
|
||||
assert plugins.is_enabled() is False
|
||||
|
||||
def test_list_plugins_returns_list(self):
|
||||
"""Test that list_plugins returns a list."""
|
||||
from praisonaiagents import plugins
|
||||
|
||||
result = plugins.list_plugins()
|
||||
assert isinstance(result, list)
|
||||
|
||||
def test_is_enabled_default_false(self):
|
||||
"""Test that plugins are disabled by default."""
|
||||
from praisonaiagents import plugins
|
||||
|
||||
# Reset state
|
||||
plugins._plugins_enabled = False
|
||||
|
||||
assert plugins.is_enabled() is False
|
||||
|
||||
|
||||
class TestPluginsEnvVar:
|
||||
"""Test PRAISONAI_PLUGINS environment variable."""
|
||||
|
||||
def test_env_var_true_enables_plugins(self):
|
||||
"""Test that PRAISONAI_PLUGINS=true enables plugins."""
|
||||
from praisonaiagents._config import _get_plugins_enabled
|
||||
|
||||
with patch.dict(os.environ, {"PRAISONAI_PLUGINS": "true"}):
|
||||
assert _get_plugins_enabled() is True
|
||||
|
||||
def test_env_var_false_disables_plugins(self):
|
||||
"""Test that PRAISONAI_PLUGINS=false disables plugins."""
|
||||
from praisonaiagents._config import _get_plugins_enabled
|
||||
|
||||
with patch.dict(os.environ, {"PRAISONAI_PLUGINS": "false"}):
|
||||
assert _get_plugins_enabled() is False
|
||||
|
||||
def test_env_var_list_enables_specific(self):
|
||||
"""Test that PRAISONAI_PLUGINS=logging,metrics enables specific plugins."""
|
||||
from praisonaiagents._config import _get_plugins_enabled, _get_plugins_list
|
||||
|
||||
with patch.dict(os.environ, {"PRAISONAI_PLUGINS": "logging,metrics"}):
|
||||
assert _get_plugins_enabled() is True
|
||||
assert _get_plugins_list() == ["logging", "metrics"]
|
||||
|
||||
def test_env_var_empty_disables(self):
|
||||
"""Test that empty PRAISONAI_PLUGINS disables plugins."""
|
||||
from praisonaiagents._config import _get_plugins_enabled
|
||||
|
||||
with patch.dict(os.environ, {"PRAISONAI_PLUGINS": ""}):
|
||||
assert _get_plugins_enabled() is False
|
||||
|
||||
|
||||
class TestConfigLoader:
|
||||
"""Test config file loading."""
|
||||
|
||||
def test_get_config_returns_praison_config(self):
|
||||
"""Test that get_config returns a PraisonConfig."""
|
||||
from praisonaiagents.config.loader import get_config, PraisonConfig, clear_config_cache
|
||||
|
||||
clear_config_cache()
|
||||
config = get_config()
|
||||
assert isinstance(config, PraisonConfig)
|
||||
|
||||
def test_get_plugins_config(self):
|
||||
"""Test getting plugins config."""
|
||||
from praisonaiagents.config.loader import get_plugins_config, PluginsConfig, clear_config_cache
|
||||
|
||||
clear_config_cache()
|
||||
plugins_config = get_plugins_config()
|
||||
assert isinstance(plugins_config, PluginsConfig)
|
||||
|
||||
def test_get_defaults_config(self):
|
||||
"""Test getting defaults config."""
|
||||
from praisonaiagents.config.loader import get_defaults_config, DefaultsConfig, clear_config_cache
|
||||
|
||||
clear_config_cache()
|
||||
defaults_config = get_defaults_config()
|
||||
assert isinstance(defaults_config, DefaultsConfig)
|
||||
|
||||
def test_get_default_with_fallback(self):
|
||||
"""Test get_default returns fallback when key not found."""
|
||||
from praisonaiagents.config.loader import get_default, clear_config_cache
|
||||
|
||||
clear_config_cache()
|
||||
result = get_default("nonexistent_key", "fallback_value")
|
||||
assert result == "fallback_value"
|
||||
|
||||
def test_is_plugins_enabled_from_config(self):
|
||||
"""Test is_plugins_enabled checks config file."""
|
||||
from praisonaiagents.config.loader import is_plugins_enabled, clear_config_cache
|
||||
|
||||
clear_config_cache()
|
||||
# Without env var, should check config (which defaults to False)
|
||||
with patch.dict(os.environ, {}, clear=True):
|
||||
# Remove PRAISONAI_PLUGINS if present
|
||||
os.environ.pop("PRAISONAI_PLUGINS", None)
|
||||
result = is_plugins_enabled()
|
||||
assert isinstance(result, bool)
|
||||
|
||||
def test_config_file_parsing(self):
|
||||
"""Test parsing a TOML config file."""
|
||||
from praisonaiagents.config.loader import _parse_toml, clear_config_cache
|
||||
|
||||
# Create a temp TOML file
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.toml', delete=False) as f:
|
||||
f.write("""
|
||||
[plugins]
|
||||
enabled = true
|
||||
auto_discover = true
|
||||
|
||||
[defaults]
|
||||
model = "gpt-4o-mini"
|
||||
|
||||
[defaults.memory]
|
||||
enabled = true
|
||||
backend = "file"
|
||||
""")
|
||||
f.flush()
|
||||
|
||||
try:
|
||||
result = _parse_toml(Path(f.name))
|
||||
assert result.get("plugins", {}).get("enabled") is True
|
||||
assert result.get("defaults", {}).get("model") == "gpt-4o-mini"
|
||||
assert result.get("defaults", {}).get("memory", {}).get("backend") == "file"
|
||||
finally:
|
||||
os.unlink(f.name)
|
||||
|
||||
|
||||
class TestConfigDrivenDefaults:
|
||||
"""Test config-driven defaults for Agent parameters."""
|
||||
|
||||
def test_apply_config_defaults_with_true(self):
|
||||
"""Test that True uses config defaults."""
|
||||
from praisonaiagents.config.loader import apply_config_defaults, clear_config_cache
|
||||
from praisonaiagents.config.feature_configs import MemoryConfig
|
||||
|
||||
clear_config_cache()
|
||||
|
||||
# When user passes True, should use config defaults
|
||||
# Without config file, should just return True
|
||||
result = apply_config_defaults("memory", True, MemoryConfig)
|
||||
# Should return True or a MemoryConfig
|
||||
assert result is True or isinstance(result, MemoryConfig)
|
||||
|
||||
def test_apply_config_defaults_with_none(self):
|
||||
"""Test that None checks config for enabled."""
|
||||
from praisonaiagents.config.loader import apply_config_defaults, clear_config_cache
|
||||
|
||||
clear_config_cache()
|
||||
|
||||
# When user passes None, should check config
|
||||
result = apply_config_defaults("memory", None, None)
|
||||
# Without config, should return None
|
||||
assert result is None
|
||||
|
||||
def test_apply_config_defaults_explicit_value(self):
|
||||
"""Test that explicit values are preserved."""
|
||||
from praisonaiagents.config.loader import apply_config_defaults, clear_config_cache
|
||||
from praisonaiagents.config.feature_configs import MemoryConfig
|
||||
|
||||
clear_config_cache()
|
||||
|
||||
# When user passes explicit config, should preserve it
|
||||
explicit_config = MemoryConfig(backend="postgres")
|
||||
result = apply_config_defaults("memory", explicit_config, MemoryConfig)
|
||||
assert result == explicit_config
|
||||
|
||||
|
||||
class TestToolsGuardrailsExempt:
|
||||
"""Test that tools and guardrails work without plugins.enable()."""
|
||||
|
||||
def test_tools_work_without_enable(self):
|
||||
"""Test that tools work without calling plugins.enable()."""
|
||||
from praisonaiagents import plugins
|
||||
|
||||
# Ensure plugins are disabled
|
||||
plugins._plugins_enabled = False
|
||||
|
||||
# Tools should still work - this is tested by Agent creation
|
||||
# Just verify the flag is false
|
||||
assert plugins.is_enabled() is False
|
||||
|
||||
def test_guardrails_work_without_enable(self):
|
||||
"""Test that guardrails work without calling plugins.enable()."""
|
||||
from praisonaiagents import plugins
|
||||
|
||||
# Ensure plugins are disabled
|
||||
plugins._plugins_enabled = False
|
||||
|
||||
# Guardrails should still work - this is tested by Agent creation
|
||||
# Just verify the flag is false
|
||||
assert plugins.is_enabled() is False
|
||||
|
||||
|
||||
class TestPluginManagerIntegration:
|
||||
"""Test PluginManager integration with enable API."""
|
||||
|
||||
def test_get_plugin_manager_singleton(self):
|
||||
"""Test that get_plugin_manager returns singleton."""
|
||||
from praisonaiagents.plugins import get_plugin_manager
|
||||
|
||||
manager1 = get_plugin_manager()
|
||||
manager2 = get_plugin_manager()
|
||||
assert manager1 is manager2
|
||||
|
||||
def test_plugin_manager_has_required_methods(self):
|
||||
"""Test that PluginManager has all required methods."""
|
||||
from praisonaiagents.plugins import get_plugin_manager
|
||||
|
||||
manager = get_plugin_manager()
|
||||
|
||||
# Check required methods exist
|
||||
assert hasattr(manager, 'register')
|
||||
assert hasattr(manager, 'unregister')
|
||||
assert hasattr(manager, 'enable')
|
||||
assert hasattr(manager, 'disable')
|
||||
assert hasattr(manager, 'is_enabled')
|
||||
assert hasattr(manager, 'list_plugins')
|
||||
assert hasattr(manager, 'auto_discover_plugins')
|
||||
assert hasattr(manager, 'execute_hook')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
@@ -238,22 +238,15 @@ class TestImportPerformance:
|
||||
"""Package import should be fast (< 200ms)."""
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Run import in fresh process to get accurate timing
|
||||
result = subprocess.run(
|
||||
[sys.executable, '-c',
|
||||
'import time; t=time.time(); import praisonaiagents; print(time.time()-t)'],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
cwd=os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
|
||||
text=True
|
||||
)
|
||||
|
||||
# Handle subprocess errors gracefully
|
||||
if result.returncode != 0 or not result.stdout.strip():
|
||||
# If subprocess failed, skip with informative message
|
||||
pytest.skip(f"Subprocess failed: returncode={result.returncode}, stderr={result.stderr[:200] if result.stderr else 'none'}")
|
||||
|
||||
import_time = float(result.stdout.strip())
|
||||
# 500ms is realistic for a feature-rich SDK on cold start
|
||||
# The lazy loading ensures heavy deps (litellm) aren't imported at startup
|
||||
|
||||
@@ -119,7 +119,6 @@ class TestMemoryConfigHistoryFields:
|
||||
assert agent._history_limit == 15
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="History injection implementation differs from test expectations - needs API alignment")
|
||||
class TestHistoryInjection:
|
||||
"""Test that history is actually injected into messages."""
|
||||
|
||||
|
||||
@@ -1,244 +0,0 @@
|
||||
"""
|
||||
TDD Tests for TaskExecutionConfig Resolution in Task class.
|
||||
|
||||
These tests verify that TaskExecutionConfig fields are properly resolved
|
||||
to Task properties when passed via the execution parameter.
|
||||
|
||||
Test Categories:
|
||||
1. TaskExecutionConfig full resolution
|
||||
2. Direct params precedence over config
|
||||
3. Default values when neither provided
|
||||
4. Backward compatibility with direct params
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from praisonaiagents.task.task import Task
|
||||
from praisonaiagents.workflows.workflow_configs import TaskExecutionConfig
|
||||
|
||||
|
||||
class TestTaskExecutionConfigResolution:
|
||||
"""Test that TaskExecutionConfig fields are resolved to Task properties."""
|
||||
|
||||
def test_async_exec_resolved_from_config(self):
|
||||
"""TaskExecutionConfig.async_exec should set Task.async_execution."""
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
execution=TaskExecutionConfig(async_exec=True)
|
||||
)
|
||||
assert task.async_execution == True
|
||||
|
||||
def test_quality_check_resolved_from_config(self):
|
||||
"""TaskExecutionConfig.quality_check should set Task.quality_check."""
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
execution=TaskExecutionConfig(quality_check=False)
|
||||
)
|
||||
assert task.quality_check == False
|
||||
|
||||
def test_rerun_resolved_from_config(self):
|
||||
"""TaskExecutionConfig.rerun should set Task.rerun."""
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
execution=TaskExecutionConfig(rerun=False)
|
||||
)
|
||||
assert task.rerun == False
|
||||
|
||||
def test_max_retries_resolved_from_config(self):
|
||||
"""TaskExecutionConfig.max_retries should set Task.max_retries."""
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
execution=TaskExecutionConfig(max_retries=10)
|
||||
)
|
||||
assert task.max_retries == 10
|
||||
|
||||
def test_on_error_resolved_from_config(self):
|
||||
"""TaskExecutionConfig.on_error should set Task.on_error (already implemented)."""
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
execution=TaskExecutionConfig(on_error="continue")
|
||||
)
|
||||
assert task.on_error == "continue"
|
||||
|
||||
def test_all_fields_resolved_together(self):
|
||||
"""All TaskExecutionConfig fields should be resolved together."""
|
||||
config = TaskExecutionConfig(
|
||||
async_exec=True,
|
||||
quality_check=False,
|
||||
rerun=False,
|
||||
max_retries=7,
|
||||
on_error="retry"
|
||||
)
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
execution=config
|
||||
)
|
||||
|
||||
assert task.async_execution == True
|
||||
assert task.quality_check == False
|
||||
assert task.rerun == False
|
||||
assert task.max_retries == 7
|
||||
assert task.on_error == "retry"
|
||||
|
||||
|
||||
class TestDirectParamsPrecedence:
|
||||
"""Test that non-default direct params take precedence over TaskExecutionConfig.
|
||||
|
||||
Note: Due to Python's inability to distinguish between f(x=False) and f() when
|
||||
default is False, we use this rule: direct params take precedence only when
|
||||
they differ from their default values. This means:
|
||||
- async_execution=True overrides config (default is False)
|
||||
- quality_check=False overrides config (default is True)
|
||||
- max_retries=5 overrides config (default is 3)
|
||||
- rerun=True overrides config (default is False)
|
||||
"""
|
||||
|
||||
def test_direct_async_execution_true_takes_precedence(self):
|
||||
"""Direct async_execution=True should override config's False."""
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
async_execution=True,
|
||||
execution=TaskExecutionConfig(async_exec=False)
|
||||
)
|
||||
# Direct param (True, non-default) should take precedence
|
||||
assert task.async_execution == True
|
||||
|
||||
def test_direct_quality_check_false_takes_precedence(self):
|
||||
"""Direct quality_check=False should override config's True."""
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
quality_check=False,
|
||||
execution=TaskExecutionConfig(quality_check=True)
|
||||
)
|
||||
# Direct param (False, non-default) should take precedence
|
||||
assert task.quality_check == False
|
||||
|
||||
def test_direct_max_retries_takes_precedence(self):
|
||||
"""Direct max_retries param should override config."""
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
max_retries=5,
|
||||
execution=TaskExecutionConfig(max_retries=10)
|
||||
)
|
||||
# Direct param (5, non-default) should take precedence
|
||||
assert task.max_retries == 5
|
||||
|
||||
def test_direct_rerun_takes_precedence(self):
|
||||
"""Direct rerun=True param should override config."""
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
rerun=True,
|
||||
execution=TaskExecutionConfig(rerun=False)
|
||||
)
|
||||
# Direct param (True, non-default) should take precedence
|
||||
assert task.rerun == True
|
||||
|
||||
|
||||
class TestDefaultValues:
|
||||
"""Test default values when neither direct param nor config provided."""
|
||||
|
||||
def test_default_async_execution(self):
|
||||
"""Default async_execution should be False."""
|
||||
task = Task(name="test_task", action="Do something")
|
||||
assert task.async_execution == False
|
||||
|
||||
def test_default_quality_check(self):
|
||||
"""Default quality_check should be True."""
|
||||
task = Task(name="test_task", action="Do something")
|
||||
assert task.quality_check == True
|
||||
|
||||
def test_default_rerun(self):
|
||||
"""Default rerun should be False."""
|
||||
task = Task(name="test_task", action="Do something")
|
||||
assert task.rerun == False
|
||||
|
||||
def test_default_max_retries(self):
|
||||
"""Default max_retries should be 3."""
|
||||
task = Task(name="test_task", action="Do something")
|
||||
assert task.max_retries == 3
|
||||
|
||||
def test_default_on_error(self):
|
||||
"""Default on_error should be 'stop'."""
|
||||
task = Task(name="test_task", action="Do something")
|
||||
assert task.on_error == "stop"
|
||||
|
||||
|
||||
class TestBackwardCompatibility:
|
||||
"""Test that direct params continue to work (backward compatibility)."""
|
||||
|
||||
def test_direct_async_execution_works(self):
|
||||
"""Direct async_execution param should work without config."""
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
async_execution=True
|
||||
)
|
||||
assert task.async_execution == True
|
||||
|
||||
def test_direct_quality_check_works(self):
|
||||
"""Direct quality_check param should work without config."""
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
quality_check=False
|
||||
)
|
||||
assert task.quality_check == False
|
||||
|
||||
def test_direct_max_retries_works(self):
|
||||
"""Direct max_retries param should work without config."""
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
max_retries=15
|
||||
)
|
||||
assert task.max_retries == 15
|
||||
|
||||
def test_direct_rerun_works(self):
|
||||
"""Direct rerun param should work without config."""
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
rerun=True
|
||||
)
|
||||
assert task.rerun == True
|
||||
|
||||
def test_direct_on_error_works(self):
|
||||
"""Direct on_error param should work without config."""
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
on_error="continue"
|
||||
)
|
||||
assert task.on_error == "continue"
|
||||
|
||||
|
||||
class TestExecutionConfigStorage:
|
||||
"""Test that execution config is properly stored."""
|
||||
|
||||
def test_execution_config_stored(self):
|
||||
"""TaskExecutionConfig should be stored in task.execution."""
|
||||
config = TaskExecutionConfig(async_exec=True)
|
||||
task = Task(
|
||||
name="test_task",
|
||||
action="Do something",
|
||||
execution=config
|
||||
)
|
||||
assert task.execution is config
|
||||
|
||||
def test_execution_none_when_not_provided(self):
|
||||
"""task.execution should be None when not provided."""
|
||||
task = Task(name="test_task", action="Do something")
|
||||
assert task.execution is None
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
Generated
+1
-1
@@ -2549,7 +2549,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "praisonaiagents"
|
||||
version = "1.4.4"
|
||||
version = "1.4.1"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "aiohttp" },
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,74 +0,0 @@
|
||||
[workspace]
|
||||
resolver = "2"
|
||||
members = [
|
||||
"praisonai",
|
||||
"praisonai-derive",
|
||||
"praisonai-cli",
|
||||
]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.2.0"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
authors = ["MervinPraison"]
|
||||
repository = "https://github.com/MervinPraison/PraisonAI"
|
||||
homepage = "https://docs.praison.ai"
|
||||
description = "High-performance, agentic AI framework for Rust"
|
||||
keywords = ["ai", "agents", "llm", "openai", "anthropic"]
|
||||
categories = ["api-bindings", "asynchronous"]
|
||||
|
||||
[workspace.dependencies]
|
||||
# Core async runtime
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
async-trait = "0.1"
|
||||
futures = "0.3"
|
||||
|
||||
# Serialization
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
serde_yaml = "0.9"
|
||||
|
||||
# Error handling
|
||||
thiserror = "2"
|
||||
anyhow = "1"
|
||||
|
||||
# CLI
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
|
||||
# LLM providers (via rig-core for multi-provider support)
|
||||
rig-core = "0.9"
|
||||
|
||||
# HTTP client
|
||||
reqwest = { version = "0.12", features = ["json", "stream"] }
|
||||
|
||||
# Logging
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
|
||||
# UUID for session/agent IDs
|
||||
uuid = { version = "1", features = ["v4"] }
|
||||
|
||||
# Optional: TUI
|
||||
ratatui = "0.29"
|
||||
crossterm = "0.28"
|
||||
|
||||
# Optional: MCP support
|
||||
# rmcp = { version = "0.12", optional = true }
|
||||
|
||||
# Testing
|
||||
tokio-test = "0.4"
|
||||
|
||||
# Proc macro dependencies
|
||||
syn = { version = "2", features = ["full", "parsing", "extra-traits"] }
|
||||
quote = "1"
|
||||
proc-macro2 = "1"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
codegen-units = 1
|
||||
strip = true
|
||||
panic = "abort"
|
||||
|
||||
[profile.dev]
|
||||
opt-level = 0
|
||||
debug = true
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,697 +0,0 @@
|
||||
# Rust Feature Parity Tracker
|
||||
|
||||
> **Python Features:** 282 | **Rust Features:** 666 | **Parity:** 100.0%
|
||||
|
||||
## Summary
|
||||
|
||||
| Metric | Count |
|
||||
|--------|-------|
|
||||
| Python Core Features | 282 |
|
||||
| Rust Features | 666 |
|
||||
| **Actual Gap Count** | **0** |
|
||||
| Language Limitations (N/A) | 6 |
|
||||
| **Parity** | **100.0%** |
|
||||
|
||||
## Implemented Features
|
||||
|
||||
- ✅ `A2A`
|
||||
- ✅ `A2AAgentCapabilities`
|
||||
- ✅ `A2AAgentCard`
|
||||
- ✅ `A2AAgentSkill`
|
||||
- ✅ `A2ATask`
|
||||
- ✅ `A2ATaskState`
|
||||
- ✅ `AGUI`
|
||||
- ✅ `AGUIEvent`
|
||||
- ✅ `AGUIEventType`
|
||||
- ✅ `AGUIMessage`
|
||||
- ✅ `AGUIRole`
|
||||
- ✅ `AGUIRunInput`
|
||||
- ✅ `AUTONOMY_PRESETS`
|
||||
- ✅ `AccuracyEvaluator`
|
||||
- ✅ `AccuracyEvaluatorBuilder`
|
||||
- ✅ `AccuracyResult`
|
||||
- ✅ `AddResult`
|
||||
- ✅ `Agent`
|
||||
- ✅ `AgentAppConfig`
|
||||
- ✅ `AgentAppProtocol`
|
||||
- ✅ `AgentBuilder`
|
||||
- ✅ `AgentConfig`
|
||||
- ✅ `AgentFlow`
|
||||
- ✅ `AgentManager`
|
||||
- ✅ `AgentMetrics`
|
||||
- ✅ `AgentOSConfig`
|
||||
- ✅ `AgentOSProtocol`
|
||||
- ✅ `AgentPluginProtocol`
|
||||
- ✅ `AgentProtocol`
|
||||
- ✅ `AgentTeam`
|
||||
- ✅ `AgentTeamBuilder`
|
||||
- ✅ `Agents`
|
||||
- ✅ `ApiStats`
|
||||
- ✅ `ApprovalCallback`
|
||||
- ✅ `ApprovalDecision`
|
||||
- ✅ `ArrayMode`
|
||||
- ✅ `AsyncDisplayCallback`
|
||||
- ✅ `AsyncGuardrail`
|
||||
- ✅ `AsyncStreamCallback`
|
||||
- ✅ `AudioAgent`
|
||||
- ✅ `AudioAgentBuilder`
|
||||
- ✅ `AudioConfig`
|
||||
- ✅ `AuthProfile`
|
||||
- ✅ `AutoAgentSpec`
|
||||
- ✅ `AutoAgents`
|
||||
- ✅ `AutoAgentsConfig`
|
||||
- ✅ `AutoRagAgent`
|
||||
- ✅ `AutoRagConfig`
|
||||
- ✅ `AutonomyConfig`
|
||||
- ✅ `AutonomyLevel`
|
||||
- ✅ `AutonomyPreset`
|
||||
- ✅ `BlocklistGuardrail`
|
||||
- ✅ `BotAction`
|
||||
- ✅ `BotAttachment`
|
||||
- ✅ `BotChannel`
|
||||
- ✅ `BotConfig`
|
||||
- ✅ `BotMessage`
|
||||
- ✅ `BotProtocol`
|
||||
- ✅ `BotResponse`
|
||||
- ✅ `BotUser`
|
||||
- ✅ `BotsChatMessage`
|
||||
- ✅ `BotsChatProtocol`
|
||||
- ✅ `BudgetAllocation`
|
||||
- ✅ `BudgetLevel`
|
||||
- ✅ `CACHING_PRESETS`
|
||||
- ✅ `CONTEXT_PRESETS`
|
||||
- ✅ `CachingConfig`
|
||||
- ✅ `CachingPreset`
|
||||
- ✅ `Chunking`
|
||||
- ✅ `ChunkingConfig`
|
||||
- ✅ `ChunkingStrategy`
|
||||
- ✅ `Citation`
|
||||
- ✅ `CitationsMode`
|
||||
- ✅ `ClosureCondition`
|
||||
- ✅ `CodeAgent`
|
||||
- ✅ `CodeAgentBuilder`
|
||||
- ✅ `CodeConfig`
|
||||
- ✅ `CodeExecutionResult`
|
||||
- ✅ `CodeExecutionStep`
|
||||
- ✅ `ColorPalette`
|
||||
- ✅ `ConditionProtocol`
|
||||
- ✅ `ConfigAutoRagConfig`
|
||||
- ✅ `ConfigValidationError`
|
||||
- ✅ `ConnectionStatus`
|
||||
- ✅ `ConsoleExporter`
|
||||
- ✅ `ContextAgent`
|
||||
- ✅ `ContextAgentConfig`
|
||||
- ✅ `ContextBudgeter`
|
||||
- ✅ `ContextChunk`
|
||||
- ✅ `ContextConfig`
|
||||
- ✅ `ContextEntry`
|
||||
- ✅ `ContextEvent`
|
||||
- ✅ `ContextEventType`
|
||||
- ✅ `ContextLedger`
|
||||
- ✅ `ContextListSink`
|
||||
- ✅ `ContextManager`
|
||||
- ✅ `ContextNoOpSink`
|
||||
- ✅ `ContextPack`
|
||||
- ✅ `ContextPolicy`
|
||||
- ✅ `ContextPreset`
|
||||
- ✅ `ContextSegment`
|
||||
- ✅ `ContextStrategy`
|
||||
- ✅ `ContextTraceEmitter`
|
||||
- ✅ `ContextTraceSink`
|
||||
- ✅ `ContextTraceSinkProtocol`
|
||||
- ✅ `ConversationHistory`
|
||||
- ✅ `CriteriaEvaluator`
|
||||
- ✅ `CriteriaEvaluatorBuilder`
|
||||
- ✅ `CriteriaResult`
|
||||
- ✅ `CriteriaScore`
|
||||
- ✅ `DEFAULT_OUTPUT_MODE`
|
||||
- ✅ `DbAdapter`
|
||||
- ✅ `DeepResearchAgent`
|
||||
- ✅ `DeepResearchAgentBuilder`
|
||||
- ✅ `DeepResearchCitation`
|
||||
- ✅ `DeepResearchConfig`
|
||||
- ✅ `DeepResearchResponse`
|
||||
- ✅ `DeepResearchResult`
|
||||
- ✅ `DefaultsConfig`
|
||||
- ✅ `DictCondition`
|
||||
- ✅ `DisplayCallback`
|
||||
- ✅ `DisplayEvent`
|
||||
- ✅ `DisplayEventType`
|
||||
- ✅ `DisplayType`
|
||||
- ✅ `Document`
|
||||
- ✅ `EXECUTION_PRESETS`
|
||||
- ✅ `EmbeddingAgent`
|
||||
- ✅ `EmbeddingAgentBuilder`
|
||||
- ✅ `EmbeddingConfig`
|
||||
- ✅ `EmbeddingResult`
|
||||
- ✅ `EmbeddingUsage`
|
||||
- ✅ `Error`
|
||||
- ✅ `ErrorLog`
|
||||
- ✅ `EvalToolCallResult`
|
||||
- ✅ `EvaluationScore`
|
||||
- ✅ `Evaluator`
|
||||
- ✅ `EvaluatorConfig`
|
||||
- ✅ `Event`
|
||||
- ✅ `EventBus`
|
||||
- ✅ `EventType`
|
||||
- ✅ `ExecutionConfig`
|
||||
- ✅ `ExecutionPreset`
|
||||
- ✅ `ExpandPrompts`
|
||||
- ✅ `ExpandResult`
|
||||
- ✅ `ExpandStrategy`
|
||||
- ✅ `ExpressionCondition`
|
||||
- ✅ `FailoverConfig`
|
||||
- ✅ `FailoverManager`
|
||||
- ✅ `FailoverStatus`
|
||||
- ✅ `FastContext`
|
||||
- ✅ `FastContextConfig`
|
||||
- ✅ `FastContextResult`
|
||||
- ✅ `FileMatch`
|
||||
- ✅ `FileSearchCall`
|
||||
- ✅ `FileSessionStore`
|
||||
- ✅ `FlowDisplay`
|
||||
- ✅ `FlowStep`
|
||||
- ✅ `FunctionGuardrail`
|
||||
- ✅ `FunctionPlugin`
|
||||
- ✅ `FunctionStats`
|
||||
- ✅ `FunctionTool`
|
||||
- ✅ `GUARDRAIL_PRESETS`
|
||||
- ✅ `GatewayClientProtocol`
|
||||
- ✅ `GatewayConfig`
|
||||
- ✅ `GatewayEvent`
|
||||
- ✅ `GatewayEventType`
|
||||
- ✅ `GatewayHealth`
|
||||
- ✅ `GatewayMessage`
|
||||
- ✅ `GatewayProtocol`
|
||||
- ✅ `GatewaySessionProtocol`
|
||||
- ✅ `Guardrail`
|
||||
- ✅ `GuardrailAction`
|
||||
- ✅ `GuardrailChain`
|
||||
- ✅ `GuardrailConfig`
|
||||
- ✅ `GuardrailPreset`
|
||||
- ✅ `GuardrailResult`
|
||||
- ✅ `GuardrailsAction`
|
||||
- ✅ `GuardrailsConfig`
|
||||
- ✅ `GuardrailsResult`
|
||||
- ✅ `Handoff`
|
||||
- ✅ `HandoffChain`
|
||||
- ✅ `HandoffConfig`
|
||||
- ✅ `HandoffCycleError`
|
||||
- ✅ `HandoffDepthError`
|
||||
- ✅ `HandoffError`
|
||||
- ✅ `HandoffFilter`
|
||||
- ✅ `HandoffFilters`
|
||||
- ✅ `HandoffInputData`
|
||||
- ✅ `HandoffResult`
|
||||
- ✅ `HandoffTimeoutError`
|
||||
- ✅ `HookDecision`
|
||||
- ✅ `HookDefinition`
|
||||
- ✅ `HookEvent`
|
||||
- ✅ `HookInput`
|
||||
- ✅ `HookPluginProtocol`
|
||||
- ✅ `HookRegistry`
|
||||
- ✅ `HookResult`
|
||||
- ✅ `HookRunner`
|
||||
- ✅ `HooksConfig`
|
||||
- ✅ `If`
|
||||
- ✅ `ImageAgent`
|
||||
- ✅ `ImageAgentBuilder`
|
||||
- ✅ `ImageConfig`
|
||||
- ✅ `ImageResult`
|
||||
- ✅ `InMemoryAdapter`
|
||||
- ✅ `InMemorySessionStore`
|
||||
- ✅ `InMemoryVectorStore`
|
||||
- ✅ `IndexStats`
|
||||
- ✅ `IndexType`
|
||||
- ✅ `JsonFileExporter`
|
||||
- ✅ `Judge`
|
||||
- ✅ `JudgeConfig`
|
||||
- ✅ `JudgeResult`
|
||||
- ✅ `KNOWLEDGE_PRESETS`
|
||||
- ✅ `Knowledge`
|
||||
- ✅ `KnowledgeAddResult`
|
||||
- ✅ `KnowledgeBackendError`
|
||||
- ✅ `KnowledgeBuilder`
|
||||
- ✅ `KnowledgeChunkingStrategy`
|
||||
- ✅ `KnowledgeConfig`
|
||||
- ✅ `KnowledgeModuleConfig`
|
||||
- ✅ `KnowledgePreset`
|
||||
- ✅ `KnowledgeRetrievalResult`
|
||||
- ✅ `KnowledgeRetrievalStrategy`
|
||||
- ✅ `KnowledgeStoreProtocol`
|
||||
- ✅ `LLMGuardrail`
|
||||
- ✅ `LLMMessage`
|
||||
- ✅ `LLMPluginProtocol`
|
||||
- ✅ `LengthGuardrail`
|
||||
- ✅ `LineRange`
|
||||
- ✅ `LlmConfig`
|
||||
- ✅ `LlmMessage`
|
||||
- ✅ `LlmProtocol`
|
||||
- ✅ `LlmProvider`
|
||||
- ✅ `LlmResponse`
|
||||
- ✅ `Loop`
|
||||
- ✅ `MCP`
|
||||
- ✅ `MCPBuilder`
|
||||
- ✅ `MCPCall`
|
||||
- ✅ `MCPCallResult`
|
||||
- ✅ `MCPConfig`
|
||||
- ✅ `MCPContent`
|
||||
- ✅ `MCPPrompt`
|
||||
- ✅ `MCPPromptArgument`
|
||||
- ✅ `MCPResource`
|
||||
- ✅ `MCPServer`
|
||||
- ✅ `MCPTool`
|
||||
- ✅ `MEMORY_PRESETS`
|
||||
- ✅ `MEMORY_URL_SCHEMES`
|
||||
- ✅ `MULTI_AGENT_EXECUTION_PRESETS`
|
||||
- ✅ `MULTI_AGENT_OUTPUT_PRESETS`
|
||||
- ✅ `ManagerConfig`
|
||||
- ✅ `Memory`
|
||||
- ✅ `MemoryAdapter`
|
||||
- ✅ `MemoryBackend`
|
||||
- ✅ `MemoryConfig`
|
||||
- ✅ `MemoryMessage`
|
||||
- ✅ `MemoryPreset`
|
||||
- ✅ `MemoryProtocol`
|
||||
- ✅ `Message`
|
||||
- ✅ `MessageType`
|
||||
- ✅ `MinimalTelemetry`
|
||||
- ✅ `MockLlmProvider`
|
||||
- ✅ `MultiAgentContextManager`
|
||||
- ✅ `MultiAgentExecutionConfig`
|
||||
- ✅ `MultiAgentExecutionPreset`
|
||||
- ✅ `MultiAgentHooksConfig`
|
||||
- ✅ `MultiAgentMemoryConfig`
|
||||
- ✅ `MultiAgentOutputConfig`
|
||||
- ✅ `MultiAgentOutputPreset`
|
||||
- ✅ `MultiAgentPlanningConfig`
|
||||
- ✅ `OCRAgent`
|
||||
- ✅ `OCRAgentBuilder`
|
||||
- ✅ `OCRConfig`
|
||||
- ✅ `OCRPage`
|
||||
- ✅ `OCRResult`
|
||||
- ✅ `OUTPUT_PRESETS`
|
||||
- ✅ `ObsCollector`
|
||||
- ✅ `OnError`
|
||||
- ✅ `OpenAiProvider`
|
||||
- ✅ `OptimizerStrategy`
|
||||
- ✅ `OutputConfig`
|
||||
- ✅ `OutputPreset`
|
||||
- ✅ `PARITY_AUTONOMY_PRESETS`
|
||||
- ✅ `PARITY_WORKING_FRAMES`
|
||||
- ✅ `PARITY_WORKING_PHASES`
|
||||
- ✅ `PLANNING_PRESETS`
|
||||
- ✅ `PRAISON_COLORS`
|
||||
- ✅ `Parallel`
|
||||
- ✅ `ParityAgentOSProtocol`
|
||||
- ✅ `ParityAgents`
|
||||
- ✅ `ParityApprovalCallback`
|
||||
- ✅ `ParityApprovalDecision`
|
||||
- ✅ `ParityDisplayEvent`
|
||||
- ✅ `ParityDisplayEventType`
|
||||
- ✅ `ParityEmbeddingResult`
|
||||
- ✅ `ParityEmbeddingUsage`
|
||||
- ✅ `ParityHandoffConfig`
|
||||
- ✅ `ParityHandoffFilters`
|
||||
- ✅ `ParityPluginRegistry`
|
||||
- ✅ `ParityPluginType`
|
||||
- ✅ `ParityRiskLevel`
|
||||
- ✅ `ParitySessionConfig`
|
||||
- ✅ `ParityToolDefinition`
|
||||
- ✅ `ParseError`
|
||||
- ✅ `PatternGuardrail`
|
||||
- ✅ `PerformanceEvaluator`
|
||||
- ✅ `PerformanceEvaluatorBuilder`
|
||||
- ✅ `PerformanceMetrics`
|
||||
- ✅ `PerformanceMonitor`
|
||||
- ✅ `PerformanceReport`
|
||||
- ✅ `PerformanceResult`
|
||||
- ✅ `Pipeline`
|
||||
- ✅ `Plan`
|
||||
- ✅ `PlanStep`
|
||||
- ✅ `PlanStorage`
|
||||
- ✅ `PlanningAgent`
|
||||
- ✅ `PlanningAgentConfig`
|
||||
- ✅ `PlanningConfig`
|
||||
- ✅ `PlanningPreset`
|
||||
- ✅ `PlanningStep`
|
||||
- ✅ `PlanningStepStatus`
|
||||
- ✅ `Plugin`
|
||||
- ✅ `PluginHook`
|
||||
- ✅ `PluginInfo`
|
||||
- ✅ `PluginManager`
|
||||
- ✅ `PluginMetadata`
|
||||
- ✅ `PluginParseError`
|
||||
- ✅ `PluginProtocol`
|
||||
- ✅ `PluginRegistry`
|
||||
- ✅ `PluginType`
|
||||
- ✅ `PluginsConfig`
|
||||
- ✅ `PluginsEnabled`
|
||||
- ✅ `PolicyAction`
|
||||
- ✅ `PolicyEngine`
|
||||
- ✅ `PolicyResult`
|
||||
- ✅ `PolicyRule`
|
||||
- ✅ `PraisonColors`
|
||||
- ✅ `PraisonConfig`
|
||||
- ✅ `Process`
|
||||
- ✅ `PromptExpanderAgent`
|
||||
- ✅ `PromptExpanderAgentBuilder`
|
||||
- ✅ `PromptExpanderConfig`
|
||||
- ✅ `ProtocolLlmResponse`
|
||||
- ✅ `ProtocolToolCall`
|
||||
- ✅ `Provider`
|
||||
- ✅ `ProviderStatus`
|
||||
- ✅ `QueryMode`
|
||||
- ✅ `QueryResult`
|
||||
- ✅ `QueryRewriterAgent`
|
||||
- ✅ `QueryRewriterAgentBuilder`
|
||||
- ✅ `QueryRewriterConfig`
|
||||
- ✅ `RAG`
|
||||
- ✅ `RAGBuilder`
|
||||
- ✅ `RAGCitation`
|
||||
- ✅ `RAGConfig`
|
||||
- ✅ `RAGResult`
|
||||
- ✅ `READ_ONLY_TOOLS`
|
||||
- ✅ `RECOMMENDED_PROMPT_PREFIX`
|
||||
- ✅ `REFLECTION_PRESETS`
|
||||
- ✅ `RESEARCH_TOOLS`
|
||||
- ✅ `RESTRICTED_TOOLS`
|
||||
- ✅ `RagRetrievalPolicy`
|
||||
- ✅ `RealtimeAgent`
|
||||
- ✅ `RealtimeAgentBuilder`
|
||||
- ✅ `RealtimeConfig`
|
||||
- ✅ `ReasoningStep`
|
||||
- ✅ `ReflectionConfig`
|
||||
- ✅ `ReflectionOutput`
|
||||
- ✅ `ReflectionPreset`
|
||||
- ✅ `ReliabilityEvaluator`
|
||||
- ✅ `ReliabilityEvaluatorBuilder`
|
||||
- ✅ `ReliabilityResult`
|
||||
- ✅ `Repeat`
|
||||
- ✅ `RerankResult`
|
||||
- ✅ `RerankerProtocol`
|
||||
- ✅ `ResearchCitation`
|
||||
- ✅ `ResolveOptions`
|
||||
- ✅ `ResolvedValue`
|
||||
- ✅ `ResourceLimits`
|
||||
- ✅ `ResourceUsage`
|
||||
- ✅ `Result`
|
||||
- ✅ `ResultExt`
|
||||
- ✅ `RetrievalConfig`
|
||||
- ✅ `RetrievalPolicy`
|
||||
- ✅ `RetrievalResult`
|
||||
- ✅ `RetrievalStrategy`
|
||||
- ✅ `RetrieverProtocol`
|
||||
- ✅ `RewritePrompts`
|
||||
- ✅ `RewriteResult`
|
||||
- ✅ `RewriteStrategy`
|
||||
- ✅ `RiskLevel`
|
||||
- ✅ `Role`
|
||||
- ✅ `Route`
|
||||
- ✅ `RoutingConditionProtocol`
|
||||
- ✅ `RunnableAgentProtocol`
|
||||
- ✅ `SandboxConfig`
|
||||
- ✅ `SandboxProtocol`
|
||||
- ✅ `SandboxResult`
|
||||
- ✅ `SandboxStatus`
|
||||
- ✅ `SandboxStatusInfo`
|
||||
- ✅ `ScopeRequiredError`
|
||||
- ✅ `SearchResult`
|
||||
- ✅ `SearchResultItem`
|
||||
- ✅ `SecurityConfig`
|
||||
- ✅ `SecurityPolicy`
|
||||
- ✅ `Session`
|
||||
- ✅ `SessionConfig`
|
||||
- ✅ `SessionData`
|
||||
- ✅ `SessionInfo`
|
||||
- ✅ `SessionMessage`
|
||||
- ✅ `SessionStore`
|
||||
- ✅ `SimilarityResult`
|
||||
- ✅ `SimpleReranker`
|
||||
- ✅ `SkillLoader`
|
||||
- ✅ `SkillManager`
|
||||
- ✅ `SkillMetadata`
|
||||
- ✅ `SkillProperties`
|
||||
- ✅ `SkillsConfig`
|
||||
- ✅ `Span`
|
||||
- ✅ `SpanEvent`
|
||||
- ✅ `SpanKind`
|
||||
- ✅ `SpanStatus`
|
||||
- ✅ `SpecializedAutoRagConfig`
|
||||
- ✅ `StepResult`
|
||||
- ✅ `StepStatus`
|
||||
- ✅ `StreamCallback`
|
||||
- ✅ `StreamCollector`
|
||||
- ✅ `StreamEvent`
|
||||
- ✅ `StreamEventType`
|
||||
- ✅ `StreamHandler`
|
||||
- ✅ `StreamMetrics`
|
||||
- ✅ `StringMode`
|
||||
- ✅ `Task`
|
||||
- ✅ `TaskBuilder`
|
||||
- ✅ `TaskConfig`
|
||||
- ✅ `TaskOutput`
|
||||
- ✅ `TaskStatus`
|
||||
- ✅ `TaskType`
|
||||
- ✅ `TelemetryCollector`
|
||||
- ✅ `TelemetryContext`
|
||||
- ✅ `TelemetryEvent`
|
||||
- ✅ `TelemetryEventType`
|
||||
- ✅ `TemplateConfig`
|
||||
- ✅ `ThinkingBudget`
|
||||
- ✅ `ThinkingBudgetBuilder`
|
||||
- ✅ `ThinkingConfig`
|
||||
- ✅ `ThinkingTracker`
|
||||
- ✅ `ThinkingUsage`
|
||||
- ✅ `TodoItem`
|
||||
- ✅ `TodoList`
|
||||
- ✅ `TodoPriority`
|
||||
- ✅ `TokenBudget`
|
||||
- ✅ `TokenUsage`
|
||||
- ✅ `Tool`
|
||||
- ✅ `ToolCall`
|
||||
- ✅ `ToolCallData`
|
||||
- ✅ `ToolCallResult`
|
||||
- ✅ `ToolDefinition`
|
||||
- ✅ `ToolPluginProtocol`
|
||||
- ✅ `ToolProtocol`
|
||||
- ✅ `ToolRegistry`
|
||||
- ✅ `ToolResult`
|
||||
- ✅ `ToolSchema`
|
||||
- ✅ `Tools`
|
||||
- ✅ `TraceContext`
|
||||
- ✅ `TraceContextData`
|
||||
- ✅ `TraceEvent`
|
||||
- ✅ `TraceExporter`
|
||||
- ✅ `TraceSink`
|
||||
- ✅ `TraceSinkProtocol`
|
||||
- ✅ `Tracer`
|
||||
- ✅ `TransportConfig`
|
||||
- ✅ `TransportType`
|
||||
- ✅ `Usage`
|
||||
- ✅ `ValidationError`
|
||||
- ✅ `VectorRecord`
|
||||
- ✅ `VectorStoreProtocol`
|
||||
- ✅ `VideoAgent`
|
||||
- ✅ `VideoAgentBuilder`
|
||||
- ✅ `VideoConfig`
|
||||
- ✅ `VideoResult`
|
||||
- ✅ `VideoStatus`
|
||||
- ✅ `VisionAgent`
|
||||
- ✅ `VisionAgentBuilder`
|
||||
- ✅ `VisionConfig`
|
||||
- ✅ `WEB_PRESETS`
|
||||
- ✅ `WORKFLOW_STEP_EXECUTION_PRESETS`
|
||||
- ✅ `WORKING_FRAMES`
|
||||
- ✅ `WORKING_PHASES`
|
||||
- ✅ `WebConfig`
|
||||
- ✅ `WebPreset`
|
||||
- ✅ `WebSearchCall`
|
||||
- ✅ `WebSearchProvider`
|
||||
- ✅ `Workflow`
|
||||
- ✅ `WorkflowContext`
|
||||
- ✅ `WorkflowStepExecutionPreset`
|
||||
- ✅ `add_approval_callback`
|
||||
- ✅ `add_display_callback`
|
||||
- ✅ `add_error_log`
|
||||
- ✅ `aembed`
|
||||
- ✅ `aembedding`
|
||||
- ✅ `aembeddings`
|
||||
- ✅ `apply_config_defaults`
|
||||
- ✅ `async_display_callbacks`
|
||||
- ✅ `build_context`
|
||||
- ✅ `callback_count`
|
||||
- ✅ `clean_display_content`
|
||||
- ✅ `clean_triple_backticks`
|
||||
- ✅ `clean_whitespace`
|
||||
- ✅ `cleanup_telemetry_resources`
|
||||
- ✅ `clear_all_callbacks`
|
||||
- ✅ `clear_display_callbacks`
|
||||
- ✅ `clear_error_logs`
|
||||
- ✅ `cosine_similarity`
|
||||
- ✅ `create_context_agent`
|
||||
- ✅ `create_context_agent_with_config`
|
||||
- ✅ `credit_card_rule`
|
||||
- ✅ `deduplicate_chunks`
|
||||
- ✅ `detect_memory_backend`
|
||||
- ✅ `detect_url_scheme`
|
||||
- ✅ `disable_performance_mode`
|
||||
- ✅ `disable_plugins`
|
||||
- ✅ `disable_telemetry`
|
||||
- ✅ `discover_and_load_plugins`
|
||||
- ✅ `discover_plugins`
|
||||
- ✅ `display_error`
|
||||
- ✅ `display_generating`
|
||||
- ✅ `display_instruction`
|
||||
- ✅ `display_interaction`
|
||||
- ✅ `display_reasoning_steps`
|
||||
- ✅ `display_self_reflection`
|
||||
- ✅ `display_tool_call`
|
||||
- ✅ `display_working_status`
|
||||
- ✅ `email_rule`
|
||||
- ✅ `embed`
|
||||
- ✅ `embedding`
|
||||
- ✅ `embeddings`
|
||||
- ✅ `enable_performance_mode`
|
||||
- ✅ `enable_plugins`
|
||||
- ✅ `enable_telemetry`
|
||||
- ✅ `ensure_plugin_dir`
|
||||
- ✅ `error_logs`
|
||||
- ✅ `estimate_messages_tokens`
|
||||
- ✅ `estimate_tokens`
|
||||
- ✅ `estimate_tokens_heuristic`
|
||||
- ✅ `estimate_tool_schema_tokens`
|
||||
- ✅ `evaluate_condition`
|
||||
- ✅ `execute_async_callback`
|
||||
- ✅ `execute_async_callbacks`
|
||||
- ✅ `execute_callback`
|
||||
- ✅ `execute_callbacks`
|
||||
- ✅ `execute_sync_callbacks`
|
||||
- ✅ `extract_json`
|
||||
- ✅ `format_skill_for_prompt`
|
||||
- ✅ `generate_skills_xml`
|
||||
- ✅ `get_collector`
|
||||
- ✅ `get_config`
|
||||
- ✅ `get_config_path`
|
||||
- ✅ `get_default`
|
||||
- ✅ `get_default_plugin_dirs`
|
||||
- ✅ `get_defaults_config`
|
||||
- ✅ `get_dimensions`
|
||||
- ✅ `get_enabled_plugins`
|
||||
- ✅ `get_error_logs`
|
||||
- ✅ `get_event_bus`
|
||||
- ✅ `get_model_context_window`
|
||||
- ✅ `get_model_limit`
|
||||
- ✅ `get_monitor`
|
||||
- ✅ `get_output_reserve`
|
||||
- ✅ `get_performance_report`
|
||||
- ✅ `get_plugin_manager`
|
||||
- ✅ `get_plugin_template`
|
||||
- ✅ `get_plugins_config`
|
||||
- ✅ `get_telemetry`
|
||||
- ✅ `handoff`
|
||||
- ✅ `handoff_filters`
|
||||
- ✅ `has_callbacks`
|
||||
- ✅ `is_numeric_string`
|
||||
- ✅ `is_path_like`
|
||||
- ✅ `is_performance_mode`
|
||||
- ✅ `is_plugin_enabled`
|
||||
- ✅ `is_plugins_enabled`
|
||||
- ✅ `is_policy_string`
|
||||
- ✅ `is_read_only_tool`
|
||||
- ✅ `is_research_tool`
|
||||
- ✅ `is_restricted_tool`
|
||||
- ✅ `is_telemetry_enabled`
|
||||
- ✅ `list_plugins`
|
||||
- ✅ `load_plugin`
|
||||
- ✅ `log_error`
|
||||
- ✅ `loop_step`
|
||||
- ✅ `make_array_error`
|
||||
- ✅ `make_preset_error`
|
||||
- ✅ `parallel`
|
||||
- ✅ `parallel_step`
|
||||
- ✅ `parity_add_approval_callback`
|
||||
- ✅ `parity_add_display_callback`
|
||||
- ✅ `parity_register_approval_callback`
|
||||
- ✅ `parity_register_async_display_callback`
|
||||
- ✅ `parity_register_display_callback`
|
||||
- ✅ `parity_remove_approval_callback`
|
||||
- ✅ `parity_remove_display_callback`
|
||||
- ✅ `parity_request_approval`
|
||||
- ✅ `parse_plugin_header`
|
||||
- ✅ `parse_plugin_header_from_file`
|
||||
- ✅ `parse_policy_string`
|
||||
- ✅ `phone_rule`
|
||||
- ✅ `profanity_rule`
|
||||
- ✅ `prompt_with_handoff_instructions`
|
||||
- ✅ `publish`
|
||||
- ✅ `record_event`
|
||||
- ✅ `register_approval_callback`
|
||||
- ✅ `register_async_display_callback`
|
||||
- ✅ `register_display_callback`
|
||||
- ✅ `remove_approval_callback`
|
||||
- ✅ `remove_display_callback`
|
||||
- ✅ `repeat`
|
||||
- ✅ `repeat_step`
|
||||
- ✅ `request_approval`
|
||||
- ✅ `resolve`
|
||||
- ✅ `resolve_autonomy`
|
||||
- ✅ `resolve_autonomy_preset`
|
||||
- ✅ `resolve_caching`
|
||||
- ✅ `resolve_caching_preset`
|
||||
- ✅ `resolve_context`
|
||||
- ✅ `resolve_context_preset`
|
||||
- ✅ `resolve_execution`
|
||||
- ✅ `resolve_execution_preset`
|
||||
- ✅ `resolve_guardrail_policies`
|
||||
- ✅ `resolve_guardrail_preset`
|
||||
- ✅ `resolve_guardrails`
|
||||
- ✅ `resolve_hooks`
|
||||
- ✅ `resolve_knowledge`
|
||||
- ✅ `resolve_knowledge_preset`
|
||||
- ✅ `resolve_memory`
|
||||
- ✅ `resolve_memory_preset`
|
||||
- ✅ `resolve_multi_agent_execution_preset`
|
||||
- ✅ `resolve_multi_agent_output_preset`
|
||||
- ✅ `resolve_output`
|
||||
- ✅ `resolve_output_preset`
|
||||
- ✅ `resolve_planning`
|
||||
- ✅ `resolve_planning_preset`
|
||||
- ✅ `resolve_reflection`
|
||||
- ✅ `resolve_reflection_preset`
|
||||
- ✅ `resolve_routing`
|
||||
- ✅ `resolve_skills`
|
||||
- ✅ `resolve_web`
|
||||
- ✅ `resolve_web_preset`
|
||||
- ✅ `resolve_workflow_step_execution_preset`
|
||||
- ✅ `route`
|
||||
- ✅ `route_step`
|
||||
- ✅ `ssn_rule`
|
||||
- ✅ `subscribe`
|
||||
- ✅ `subscribe_all`
|
||||
- ✅ `suggest_similar`
|
||||
- ✅ `sync_display_callbacks`
|
||||
- ✅ `tool`
|
||||
- ✅ `trace_context`
|
||||
- ✅ `track_api`
|
||||
- ✅ `track_function`
|
||||
- ✅ `track_workflow`
|
||||
- ✅ `truncate_context`
|
||||
- ✅ `validate_config`
|
||||
- ✅ `validate_keys`
|
||||
- ✅ `when`
|
||||
|
||||
## N/A (Rust Language Limitations)
|
||||
|
||||
These Python features cannot be directly implemented in Rust due to reserved keywords or module naming conflicts. Equivalent functionality exists under different names.
|
||||
|
||||
- ⚠️ `config` → Use `parity_config` instead
|
||||
- ⚠️ `db` → Use `parity_db` instead
|
||||
- ⚠️ `memory` (Rust reserved/module conflict)
|
||||
- ⚠️ `obs` → Use `parity_obs` instead
|
||||
- ⚠️ `tools` (Rust reserved/module conflict)
|
||||
- ⚠️ `workflows` (Rust reserved/module conflict)
|
||||
|
||||
---
|
||||
|
||||
*Generated by `praisonai._dev.parity.generator`*
|
||||
@@ -1,104 +0,0 @@
|
||||
# PraisonAI Rust SDK
|
||||
|
||||
High-performance, agentic AI framework for Rust.
|
||||
|
||||
## Quick Start
|
||||
|
||||
```rust
|
||||
use praisonai::{Agent, tool, Tool};
|
||||
|
||||
#[tool(description = "Search the web")]
|
||||
async fn search(query: String) -> String {
|
||||
format!("Results for: {}", query)
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let agent = Agent::new()
|
||||
.instructions("You are a helpful assistant")
|
||||
.tool(SearchTool::new())
|
||||
.build()?;
|
||||
|
||||
let response = agent.chat("Hello!").await?;
|
||||
println!("{}", response);
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
Add to your `Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
praisonai = "0.1"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
```
|
||||
|
||||
## CLI Usage
|
||||
|
||||
```bash
|
||||
# Install CLI
|
||||
cargo install praisonai-cli
|
||||
|
||||
# Interactive chat
|
||||
praisonai-rust chat
|
||||
|
||||
# Single-shot prompt
|
||||
praisonai-rust "What is 2+2?"
|
||||
|
||||
# Run workflow from YAML
|
||||
praisonai-rust run agents.yaml
|
||||
```
|
||||
|
||||
## Features
|
||||
|
||||
- **Agent-Centric**: Every design decision centers on Agents
|
||||
- **Multi-Provider LLM**: OpenAI, Anthropic, Ollama support via rig-core
|
||||
- **Tool System**: `#[tool]` macro for easy tool creation
|
||||
- **Multi-Agent Workflows**: AgentTeam, AgentFlow patterns
|
||||
- **Memory**: Conversation history and long-term storage
|
||||
- **Async-First**: Built on Tokio for high performance
|
||||
|
||||
## Crates
|
||||
|
||||
| Crate | Description |
|
||||
|-------|-------------|
|
||||
| `praisonai` | Core library (Agent, Tools, Workflows) |
|
||||
| `praisonai-derive` | Proc macros (`#[tool]`) |
|
||||
| `praisonai-cli` | CLI binary |
|
||||
|
||||
## API Parity with Python
|
||||
|
||||
```python
|
||||
# Python
|
||||
from praisonaiagents import Agent, tool
|
||||
|
||||
@tool
|
||||
def search(query: str) -> str:
|
||||
"""Search the web."""
|
||||
return f"Results for: {query}"
|
||||
|
||||
agent = Agent(instructions="Be helpful", tools=[search])
|
||||
response = agent.start("Hello!")
|
||||
```
|
||||
|
||||
```rust
|
||||
// Rust
|
||||
use praisonai::{Agent, tool, Tool};
|
||||
|
||||
#[tool(description = "Search the web")]
|
||||
async fn search(query: String) -> String {
|
||||
format!("Results for: {}", query)
|
||||
}
|
||||
|
||||
let agent = Agent::new()
|
||||
.instructions("Be helpful")
|
||||
.tool(SearchTool::new())
|
||||
.build()?;
|
||||
let response = agent.chat("Hello!").await?;
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
@@ -1,23 +0,0 @@
|
||||
//! Basic agent example
|
||||
//!
|
||||
//! Run with: cargo run --example basic_agent
|
||||
|
||||
use praisonai::Agent;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
// Create a simple agent
|
||||
let agent = Agent::new()
|
||||
.name("assistant")
|
||||
.instructions("You are a helpful AI assistant. Be concise and friendly.")
|
||||
.model("gpt-4o-mini")
|
||||
.build()?;
|
||||
|
||||
println!("Agent created: {}", agent.name());
|
||||
println!("Model: {}", agent.model());
|
||||
|
||||
let response = agent.chat("What is 2 + 2?").await?;
|
||||
println!("Response: {}", response);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
//! Configuration options example
|
||||
//!
|
||||
//! Run with: cargo run -p praisonai --example config_example
|
||||
|
||||
use praisonai::{Agent, ExecutionConfig, MemoryConfig, OutputConfig};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
println!("=== PraisonAI Configuration Examples ===\n");
|
||||
|
||||
// 1. Output Configuration
|
||||
println!("1. Output Configuration:");
|
||||
let output_silent = OutputConfig::new().silent();
|
||||
let output_verbose = OutputConfig::new().verbose();
|
||||
let output_json = OutputConfig::new().json().file("output.json");
|
||||
|
||||
println!(" - Silent mode: {}", output_silent.mode);
|
||||
println!(" - Verbose mode: {}", output_verbose.mode);
|
||||
println!(
|
||||
" - JSON mode: {}, file: {:?}",
|
||||
output_json.mode, output_json.file
|
||||
);
|
||||
|
||||
// 2. Execution Configuration
|
||||
println!("\n2. Execution Configuration:");
|
||||
let exec_config = ExecutionConfig::new()
|
||||
.max_iterations(5)
|
||||
.timeout(60)
|
||||
.no_stream();
|
||||
|
||||
println!(" - Max iterations: {}", exec_config.max_iterations);
|
||||
println!(" - Timeout: {} seconds", exec_config.timeout_secs);
|
||||
println!(" - Streaming: {}", exec_config.stream);
|
||||
|
||||
// 3. Memory Configuration
|
||||
println!("\n3. Memory Configuration:");
|
||||
let mem_config = MemoryConfig::new()
|
||||
.with_long_term()
|
||||
.provider("sqlite")
|
||||
.max_messages(100);
|
||||
|
||||
println!(" - Short-term: {}", mem_config.use_short_term);
|
||||
println!(" - Long-term: {}", mem_config.use_long_term);
|
||||
println!(" - Provider: {}", mem_config.provider);
|
||||
println!(" - Max messages: {}", mem_config.max_messages);
|
||||
|
||||
// 4. Agent with all configurations
|
||||
println!("\n4. Creating Agent with Custom Config:");
|
||||
let agent = Agent::new()
|
||||
.name("configured-agent")
|
||||
.instructions("You are a configured assistant.")
|
||||
.model("gpt-4o-mini")
|
||||
.temperature(0.5)
|
||||
.max_tokens(1000)
|
||||
.max_iterations(3)
|
||||
.verbose(true)
|
||||
.stream(false)
|
||||
.memory_config(MemoryConfig::new().max_messages(20))
|
||||
.build()?;
|
||||
|
||||
println!(" - Name: {}", agent.name());
|
||||
println!(" - Model: {}", agent.model());
|
||||
println!(" - Agent ID: {}", agent.id());
|
||||
|
||||
println!("\n=== All configurations demonstrated successfully! ===");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
//! Memory configuration example
|
||||
//!
|
||||
//! Run with: cargo run -p praisonai --example memory_example
|
||||
|
||||
use praisonai::{Agent, MemoryConfig};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
// Create memory config with custom settings
|
||||
let memory_config = MemoryConfig::new()
|
||||
.with_long_term() // Enable long-term memory
|
||||
.provider("memory") // Use in-memory provider
|
||||
.max_messages(50); // Keep last 50 messages
|
||||
|
||||
println!("Memory Config:");
|
||||
println!(" - Short-term: {}", memory_config.use_short_term);
|
||||
println!(" - Long-term: {}", memory_config.use_long_term);
|
||||
println!(" - Provider: {}", memory_config.provider);
|
||||
println!(" - Max messages: {}", memory_config.max_messages);
|
||||
|
||||
// Create agent with memory config
|
||||
let agent = Agent::new()
|
||||
.name("memory-agent")
|
||||
.instructions("You are a helpful assistant that remembers our conversation.")
|
||||
.model("gpt-4o-mini")
|
||||
.memory_config(memory_config)
|
||||
.build()?;
|
||||
|
||||
println!("\nAgent created: {}", agent.name());
|
||||
|
||||
// Demonstrate memory operations
|
||||
println!("\nMemory operations:");
|
||||
|
||||
// Get initial history (should be empty)
|
||||
let history = agent.history().await?;
|
||||
println!(" - Initial history count: {}", history.len());
|
||||
|
||||
// Clear memory
|
||||
agent.clear_memory().await?;
|
||||
println!(" - Memory cleared");
|
||||
|
||||
// Verify cleared
|
||||
let history = agent.history().await?;
|
||||
println!(" - History after clear: {}", history.len());
|
||||
|
||||
println!("\nTo test with real API, set OPENAI_API_KEY and call agent.chat()");
|
||||
println!("The agent will remember previous messages in the conversation.");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
//! Multi-agent workflow example
|
||||
//!
|
||||
//! Run with: cargo run --example multi_agent
|
||||
|
||||
use praisonai::{Agent, AgentTeam, Process};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
// Create specialized agents
|
||||
let researcher = Agent::new()
|
||||
.name("researcher")
|
||||
.instructions("You are a research specialist. Find and summarize information.")
|
||||
.model("gpt-4o-mini")
|
||||
.build()?;
|
||||
|
||||
let writer = Agent::new()
|
||||
.name("writer")
|
||||
.instructions("You are a skilled writer. Take research and create engaging content.")
|
||||
.model("gpt-4o-mini")
|
||||
.build()?;
|
||||
|
||||
let editor = Agent::new()
|
||||
.name("editor")
|
||||
.instructions("You are an editor. Review and polish the content for clarity.")
|
||||
.model("gpt-4o-mini")
|
||||
.build()?;
|
||||
|
||||
// Create a team with sequential process
|
||||
let team = AgentTeam::new()
|
||||
.agent(researcher)
|
||||
.agent(writer)
|
||||
.agent(editor)
|
||||
.process(Process::Sequential)
|
||||
.verbose(true)
|
||||
.build();
|
||||
|
||||
println!("Team created with {} agents", team.len());
|
||||
println!("Process: Sequential (researcher -> writer -> editor)");
|
||||
|
||||
// Run the workflow
|
||||
// Note: Requires OPENAI_API_KEY environment variable
|
||||
// let result = team.start("Write a blog post about Rust programming").await?;
|
||||
// println!("Final output:\n{}", result);
|
||||
|
||||
println!("\nTo test with real API, set OPENAI_API_KEY and uncomment the start() call.");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
//! Agent with tools example
|
||||
//!
|
||||
//! Run with: cargo run --example with_tools
|
||||
|
||||
use praisonai::{tool, Agent, Tool};
|
||||
|
||||
// Define a tool using the #[tool] macro
|
||||
#[tool(description = "Get the current weather for a location")]
|
||||
async fn get_weather(location: String) -> String {
|
||||
// In a real app, this would call a weather API
|
||||
format!("The weather in {} is sunny, 72°F", location)
|
||||
}
|
||||
|
||||
// Another tool
|
||||
#[tool(description = "Calculate the sum of two numbers")]
|
||||
async fn add_numbers(a: i32, b: i32) -> i32 {
|
||||
a + b
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
// Create tools
|
||||
let weather_tool = GetWeatherTool::new();
|
||||
let add_tool = AddNumbersTool::new();
|
||||
|
||||
println!(
|
||||
"Tool: {} - {}",
|
||||
weather_tool.name(),
|
||||
weather_tool.description()
|
||||
);
|
||||
println!("Tool: {} - {}", add_tool.name(), add_tool.description());
|
||||
|
||||
// Create an agent with tools
|
||||
let agent = Agent::new()
|
||||
.name("tool-agent")
|
||||
.instructions("You are a helpful assistant with access to tools.")
|
||||
.model("gpt-4o-mini")
|
||||
.tool(weather_tool)
|
||||
.tool(add_tool)
|
||||
.build()?;
|
||||
|
||||
println!("\nAgent created with {} tools", agent.tool_count().await);
|
||||
|
||||
// Test tool execution directly
|
||||
let result = get_weather("San Francisco".to_string()).await;
|
||||
println!("Direct tool call: {}", result);
|
||||
|
||||
let sum = add_numbers(5, 3).await;
|
||||
println!("Direct add call: {}", sum);
|
||||
|
||||
println!("\nTo test with LLM, set OPENAI_API_KEY and call agent.chat()");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
# Example workflow YAML file
|
||||
# Run with: praisonai run workflow.yaml
|
||||
|
||||
name: Research and Write Workflow
|
||||
process: sequential
|
||||
|
||||
agents:
|
||||
- name: researcher
|
||||
role: Research Specialist
|
||||
instructions: |
|
||||
You are a research specialist. Your job is to:
|
||||
1. Search for relevant information on the given topic
|
||||
2. Summarize key findings
|
||||
3. Provide citations where possible
|
||||
model: gpt-4o-mini
|
||||
tools:
|
||||
- search_web
|
||||
|
||||
- name: writer
|
||||
role: Content Writer
|
||||
instructions: |
|
||||
You are a skilled content writer. Your job is to:
|
||||
1. Take the research provided
|
||||
2. Create engaging, well-structured content
|
||||
3. Use clear and accessible language
|
||||
model: gpt-4o-mini
|
||||
|
||||
- name: editor
|
||||
role: Editor
|
||||
instructions: |
|
||||
You are an editor. Your job is to:
|
||||
1. Review the content for clarity and accuracy
|
||||
2. Fix any grammatical or stylistic issues
|
||||
3. Ensure the content flows well
|
||||
model: gpt-4o-mini
|
||||
|
||||
steps:
|
||||
- agent: researcher
|
||||
action: Research the topic and provide key findings
|
||||
|
||||
- agent: writer
|
||||
action: Write a blog post based on the research
|
||||
|
||||
- agent: editor
|
||||
action: Review and polish the final content
|
||||
@@ -1,137 +0,0 @@
|
||||
//! Workflow patterns example
|
||||
//!
|
||||
//! Run with: cargo run -p praisonai --example workflow_patterns
|
||||
|
||||
use praisonai::{Agent, AgentTeam, Process, StepResult, WorkflowContext};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
println!("=== PraisonAI Workflow Patterns ===\n");
|
||||
|
||||
// Create reusable agents
|
||||
let _analyst = Agent::new()
|
||||
.name("analyst")
|
||||
.instructions("You analyze data and provide insights.")
|
||||
.model("gpt-4o-mini")
|
||||
.build()?;
|
||||
|
||||
let _summarizer = Agent::new()
|
||||
.name("summarizer")
|
||||
.instructions("You create concise summaries.")
|
||||
.model("gpt-4o-mini")
|
||||
.build()?;
|
||||
|
||||
let _reviewer = Agent::new()
|
||||
.name("reviewer")
|
||||
.instructions("You review and improve content quality.")
|
||||
.model("gpt-4o-mini")
|
||||
.build()?;
|
||||
|
||||
// Pattern 1: Sequential Workflow
|
||||
println!("1. Sequential Workflow:");
|
||||
println!(" analyst -> summarizer -> reviewer");
|
||||
let sequential_team = AgentTeam::new()
|
||||
.agent(
|
||||
Agent::new()
|
||||
.name("analyst")
|
||||
.instructions("Analyze")
|
||||
.model("gpt-4o-mini")
|
||||
.build()?,
|
||||
)
|
||||
.agent(
|
||||
Agent::new()
|
||||
.name("summarizer")
|
||||
.instructions("Summarize")
|
||||
.model("gpt-4o-mini")
|
||||
.build()?,
|
||||
)
|
||||
.agent(
|
||||
Agent::new()
|
||||
.name("reviewer")
|
||||
.instructions("Review")
|
||||
.model("gpt-4o-mini")
|
||||
.build()?,
|
||||
)
|
||||
.process(Process::Sequential)
|
||||
.build();
|
||||
println!(" Team size: {} agents", sequential_team.len());
|
||||
|
||||
// Pattern 2: Parallel Workflow
|
||||
println!("\n2. Parallel Workflow:");
|
||||
println!(" [analyst, summarizer, reviewer] run simultaneously");
|
||||
let parallel_team = AgentTeam::new()
|
||||
.agent(
|
||||
Agent::new()
|
||||
.name("analyst")
|
||||
.instructions("Analyze")
|
||||
.model("gpt-4o-mini")
|
||||
.build()?,
|
||||
)
|
||||
.agent(
|
||||
Agent::new()
|
||||
.name("summarizer")
|
||||
.instructions("Summarize")
|
||||
.model("gpt-4o-mini")
|
||||
.build()?,
|
||||
)
|
||||
.agent(
|
||||
Agent::new()
|
||||
.name("reviewer")
|
||||
.instructions("Review")
|
||||
.model("gpt-4o-mini")
|
||||
.build()?,
|
||||
)
|
||||
.process(Process::Parallel)
|
||||
.build();
|
||||
println!(" Team size: {} agents", parallel_team.len());
|
||||
|
||||
// Pattern 3: Hierarchical Workflow
|
||||
println!("\n3. Hierarchical Workflow:");
|
||||
println!(" Manager validates each step before proceeding");
|
||||
let hierarchical_team = AgentTeam::new()
|
||||
.agent(
|
||||
Agent::new()
|
||||
.name("researcher")
|
||||
.instructions("Research")
|
||||
.model("gpt-4o-mini")
|
||||
.build()?,
|
||||
)
|
||||
.agent(
|
||||
Agent::new()
|
||||
.name("writer")
|
||||
.instructions("Write")
|
||||
.model("gpt-4o-mini")
|
||||
.build()?,
|
||||
)
|
||||
.process(Process::Hierarchical)
|
||||
.verbose(true)
|
||||
.build();
|
||||
println!(" Team size: {} agents", hierarchical_team.len());
|
||||
|
||||
// Demonstrate workflow context
|
||||
println!("\n4. Workflow Context:");
|
||||
let mut context = WorkflowContext::new();
|
||||
context.set("task", "Analyze market trends");
|
||||
context.set("format", "bullet points");
|
||||
println!(" Variables set: task, format");
|
||||
println!(" Task: {:?}", context.get("task"));
|
||||
println!(" Format: {:?}", context.get("format"));
|
||||
|
||||
// Demonstrate step results
|
||||
println!("\n5. Step Results:");
|
||||
let success = StepResult::success("analyst", "Analysis complete with 5 insights");
|
||||
let failure = StepResult::failure("reviewer", "Validation failed");
|
||||
println!(
|
||||
" Success: agent={}, success={}",
|
||||
success.agent, success.success
|
||||
);
|
||||
println!(
|
||||
" Failure: agent={}, success={}, error={:?}",
|
||||
failure.agent, failure.success, failure.error
|
||||
);
|
||||
|
||||
println!("\n=== All workflow patterns demonstrated! ===");
|
||||
println!("\nTo run with real LLM, set OPENAI_API_KEY and call team.start(task)");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
[package]
|
||||
name = "praisonai-cli"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
repository.workspace = true
|
||||
description = "CLI for PraisonAI - chat, run, prompt commands"
|
||||
keywords = ["ai", "agents", "cli", "terminal"]
|
||||
categories = ["command-line-utilities"]
|
||||
|
||||
[[bin]]
|
||||
name = "praisonai-rust"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
# Core library
|
||||
praisonai = { path = "../praisonai", version = "0.2.0" }
|
||||
|
||||
# Async runtime
|
||||
tokio = { workspace = true }
|
||||
futures = { workspace = true }
|
||||
|
||||
# CLI parsing
|
||||
clap = { workspace = true }
|
||||
|
||||
# Serialization
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
serde_yaml = { workspace = true }
|
||||
|
||||
# Error handling
|
||||
anyhow = { workspace = true }
|
||||
|
||||
# Logging
|
||||
tracing = { workspace = true }
|
||||
tracing-subscriber = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
tokio-test = { workspace = true }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
tui = ["ratatui", "crossterm", "praisonai/tui"]
|
||||
|
||||
[dependencies.ratatui]
|
||||
version = "0.29"
|
||||
optional = true
|
||||
|
||||
[dependencies.crossterm]
|
||||
version = "0.28"
|
||||
optional = true
|
||||
@@ -1,69 +0,0 @@
|
||||
//! Interactive chat command
|
||||
|
||||
use anyhow::Result;
|
||||
use praisonai::Agent;
|
||||
use std::io::{self, Write};
|
||||
|
||||
/// Run interactive chat session
|
||||
pub async fn run(model: String, instructions: Option<String>) -> Result<()> {
|
||||
let instructions = instructions
|
||||
.unwrap_or_else(|| "You are a helpful AI assistant. Be concise and helpful.".to_string());
|
||||
|
||||
let agent = Agent::new()
|
||||
.name("assistant")
|
||||
.instructions(&instructions)
|
||||
.model(&model)
|
||||
.build()?;
|
||||
|
||||
println!("PraisonAI Chat (model: {})", model);
|
||||
println!("Type 'exit' or 'quit' to end the session.");
|
||||
println!("Type '/clear' to clear conversation history.");
|
||||
println!();
|
||||
|
||||
loop {
|
||||
// Print prompt
|
||||
print!("You: ");
|
||||
io::stdout().flush()?;
|
||||
|
||||
// Read input
|
||||
let mut input = String::new();
|
||||
io::stdin().read_line(&mut input)?;
|
||||
let input = input.trim();
|
||||
|
||||
// Check for exit commands
|
||||
if input.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if input == "exit" || input == "quit" || input == "/exit" || input == "/quit" {
|
||||
println!("Goodbye!");
|
||||
break;
|
||||
}
|
||||
|
||||
if input == "/clear" {
|
||||
agent.clear_memory().await?;
|
||||
println!("Conversation cleared.");
|
||||
continue;
|
||||
}
|
||||
|
||||
if input == "/help" {
|
||||
println!("Commands:");
|
||||
println!(" /clear - Clear conversation history");
|
||||
println!(" /help - Show this help");
|
||||
println!(" /exit - Exit chat");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get response from agent
|
||||
match agent.chat(input).await {
|
||||
Ok(response) => {
|
||||
println!("\nAssistant: {}\n", response);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("\nError: {}\n", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
//! CLI command implementations
|
||||
|
||||
pub mod chat;
|
||||
pub mod prompt;
|
||||
pub mod run;
|
||||
@@ -1,25 +0,0 @@
|
||||
//! Single-shot prompt command
|
||||
|
||||
use anyhow::Result;
|
||||
use praisonai::Agent;
|
||||
|
||||
/// Run a single prompt
|
||||
pub async fn run(text: String, model: String) -> Result<()> {
|
||||
let agent = Agent::new()
|
||||
.name("assistant")
|
||||
.instructions("You are a helpful AI assistant. Be concise and direct.")
|
||||
.model(&model)
|
||||
.memory(false) // No memory needed for single-shot
|
||||
.build()?;
|
||||
|
||||
match agent.chat(&text).await {
|
||||
Ok(response) => {
|
||||
println!("{}", response);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,149 +0,0 @@
|
||||
//! Run workflow from YAML file
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use praisonai::{Agent, AgentTeam, Process};
|
||||
use serde::Deserialize;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
/// Workflow definition from YAML
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct WorkflowYaml {
|
||||
/// Workflow name
|
||||
#[serde(default)]
|
||||
name: Option<String>,
|
||||
|
||||
/// Process type
|
||||
#[serde(default)]
|
||||
process: Option<String>,
|
||||
|
||||
/// Agents in the workflow
|
||||
agents: Vec<AgentYaml>,
|
||||
|
||||
/// Steps (optional, for explicit ordering)
|
||||
#[serde(default)]
|
||||
steps: Vec<StepYaml>,
|
||||
}
|
||||
|
||||
/// Agent definition from YAML
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct AgentYaml {
|
||||
/// Agent name
|
||||
name: String,
|
||||
|
||||
/// Agent role/description
|
||||
#[serde(default)]
|
||||
role: Option<String>,
|
||||
|
||||
/// Agent instructions
|
||||
#[serde(default)]
|
||||
instructions: Option<String>,
|
||||
|
||||
/// Model to use
|
||||
#[serde(default)]
|
||||
model: Option<String>,
|
||||
|
||||
/// Tools (names) - reserved for future tool integration
|
||||
#[serde(default)]
|
||||
#[allow(dead_code)]
|
||||
tools: Vec<String>,
|
||||
}
|
||||
|
||||
/// Step definition from YAML
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct StepYaml {
|
||||
/// Agent name for this step - reserved for explicit step ordering
|
||||
#[allow(dead_code)]
|
||||
agent: String,
|
||||
|
||||
/// Action/task for this step
|
||||
#[serde(default)]
|
||||
action: Option<String>,
|
||||
}
|
||||
|
||||
/// Run a workflow from a YAML file
|
||||
pub async fn run(file: String, verbose: bool) -> Result<()> {
|
||||
let path = Path::new(&file);
|
||||
|
||||
if !path.exists() {
|
||||
anyhow::bail!("File not found: {}", file);
|
||||
}
|
||||
|
||||
let content =
|
||||
fs::read_to_string(path).with_context(|| format!("Failed to read file: {}", file))?;
|
||||
|
||||
let workflow: WorkflowYaml = serde_yaml::from_str(&content)
|
||||
.with_context(|| format!("Failed to parse YAML: {}", file))?;
|
||||
|
||||
if verbose {
|
||||
println!(
|
||||
"Loaded workflow: {}",
|
||||
workflow.name.as_deref().unwrap_or("unnamed")
|
||||
);
|
||||
println!("Agents: {}", workflow.agents.len());
|
||||
}
|
||||
|
||||
// Build agents
|
||||
let mut team_builder = AgentTeam::new();
|
||||
|
||||
for agent_yaml in &workflow.agents {
|
||||
let instructions = agent_yaml
|
||||
.instructions
|
||||
.clone()
|
||||
.or_else(|| agent_yaml.role.clone())
|
||||
.unwrap_or_else(|| format!("You are {}.", agent_yaml.name));
|
||||
|
||||
let model = agent_yaml
|
||||
.model
|
||||
.clone()
|
||||
.unwrap_or_else(|| "gpt-4o-mini".to_string());
|
||||
|
||||
let agent = Agent::new()
|
||||
.name(&agent_yaml.name)
|
||||
.instructions(&instructions)
|
||||
.model(&model)
|
||||
.build()?;
|
||||
|
||||
if verbose {
|
||||
println!(" - {} ({})", agent_yaml.name, model);
|
||||
}
|
||||
|
||||
team_builder = team_builder.agent(agent);
|
||||
}
|
||||
|
||||
// Set process type
|
||||
let process = match workflow.process.as_deref() {
|
||||
Some("parallel") => Process::Parallel,
|
||||
Some("hierarchical") => Process::Hierarchical,
|
||||
_ => Process::Sequential,
|
||||
};
|
||||
|
||||
let team = team_builder.process(process).verbose(verbose).build();
|
||||
|
||||
if verbose {
|
||||
println!("Process: {:?}", process);
|
||||
println!("Running workflow...\n");
|
||||
}
|
||||
|
||||
// Get the task from the first step or use a default
|
||||
let task = workflow
|
||||
.steps
|
||||
.first()
|
||||
.and_then(|s| s.action.clone())
|
||||
.unwrap_or_else(|| "Execute the workflow".to_string());
|
||||
|
||||
// Run the workflow
|
||||
match team.start(&task).await {
|
||||
Ok(result) => {
|
||||
if verbose {
|
||||
println!("\n--- Result ---\n");
|
||||
}
|
||||
println!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Workflow failed: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
//! PraisonAI CLI - Command-line interface for PraisonAI
|
||||
//!
|
||||
//! # Commands
|
||||
//!
|
||||
//! - `praisonai chat` - Interactive chat session
|
||||
//! - `praisonai run <file>` - Run a workflow from YAML file
|
||||
//! - `praisonai "prompt"` - Single-shot prompt execution
|
||||
//! - `praisonai --version` - Show version
|
||||
|
||||
mod commands;
|
||||
|
||||
use anyhow::Result;
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
/// PraisonAI - High-performance agentic AI framework
|
||||
#[derive(Parser)]
|
||||
#[command(name = "praisonai-rust")]
|
||||
#[command(author = "PraisonAI <[email protected]>")]
|
||||
#[command(version = env!("CARGO_PKG_VERSION"))]
|
||||
#[command(about = "High-performance, agentic AI framework for Rust")]
|
||||
#[command(long_about = None)]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
command: Option<Commands>,
|
||||
|
||||
/// Single-shot prompt (when no subcommand is given)
|
||||
#[arg(trailing_var_arg = true)]
|
||||
prompt: Vec<String>,
|
||||
|
||||
/// Model to use (e.g., gpt-4o-mini, claude-3-sonnet)
|
||||
#[arg(short, long, default_value = "gpt-4o-mini")]
|
||||
model: String,
|
||||
|
||||
/// Enable verbose output
|
||||
#[arg(short, long)]
|
||||
verbose: bool,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
/// Start an interactive chat session
|
||||
Chat {
|
||||
/// Model to use
|
||||
#[arg(short, long, default_value = "gpt-4o-mini")]
|
||||
model: String,
|
||||
|
||||
/// System instructions
|
||||
#[arg(short, long)]
|
||||
instructions: Option<String>,
|
||||
},
|
||||
|
||||
/// Run a workflow from YAML file
|
||||
Run {
|
||||
/// Path to the YAML workflow file
|
||||
file: String,
|
||||
|
||||
/// Enable verbose output
|
||||
#[arg(short, long)]
|
||||
verbose: bool,
|
||||
},
|
||||
|
||||
/// Execute a single prompt
|
||||
Prompt {
|
||||
/// The prompt to execute
|
||||
text: String,
|
||||
|
||||
/// Model to use
|
||||
#[arg(short, long, default_value = "gpt-4o-mini")]
|
||||
model: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
// Initialize logging
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(
|
||||
tracing_subscriber::EnvFilter::from_default_env()
|
||||
.add_directive(tracing::Level::INFO.into()),
|
||||
)
|
||||
.init();
|
||||
|
||||
let cli = Cli::parse();
|
||||
|
||||
match cli.command {
|
||||
Some(Commands::Chat {
|
||||
model,
|
||||
instructions,
|
||||
}) => commands::chat::run(model, instructions).await,
|
||||
Some(Commands::Run { file, verbose }) => commands::run::run(file, verbose).await,
|
||||
Some(Commands::Prompt { text, model }) => commands::prompt::run(text, model).await,
|
||||
None => {
|
||||
// If no subcommand but prompt provided, run single-shot
|
||||
if !cli.prompt.is_empty() {
|
||||
let text = cli.prompt.join(" ");
|
||||
commands::prompt::run(text, cli.model).await
|
||||
} else {
|
||||
// No command and no prompt - show help
|
||||
println!("PraisonAI - High-performance agentic AI framework\n");
|
||||
println!("Usage:");
|
||||
println!(" praisonai chat Start interactive chat");
|
||||
println!(" praisonai run <file> Run workflow from YAML");
|
||||
println!(" praisonai \"<prompt>\" Single-shot prompt");
|
||||
println!("\nOptions:");
|
||||
println!(" -m, --model <MODEL> Model to use [default: gpt-4o-mini]");
|
||||
println!(" -v, --verbose Enable verbose output");
|
||||
println!(" -h, --help Show help");
|
||||
println!(" -V, --version Show version");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
[package]
|
||||
name = "praisonai-derive"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
repository.workspace = true
|
||||
description = "Procedural macros for PraisonAI - #[tool] decorator"
|
||||
keywords = ["ai", "agents", "macros", "proc-macro"]
|
||||
categories = ["development-tools::procedural-macro-helpers"]
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
syn = { workspace = true }
|
||||
quote = { workspace = true }
|
||||
proc-macro2 = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
async-trait = { workspace = true }
|
||||
@@ -1,249 +0,0 @@
|
||||
//! Procedural macros for PraisonAI
|
||||
//!
|
||||
//! This crate provides the `#[tool]` attribute macro for defining tools.
|
||||
//!
|
||||
//! # Example
|
||||
//!
|
||||
//! ```rust,ignore
|
||||
//! use praisonai::tool;
|
||||
//!
|
||||
//! #[tool(description = "Search the web for information")]
|
||||
//! async fn search_web(query: String) -> String {
|
||||
//! format!("Results for: {}", query)
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::{format_ident, quote};
|
||||
use syn::{parse_macro_input, Expr, ExprLit, ItemFn, Lit, Meta};
|
||||
|
||||
/// The `#[tool]` attribute macro for defining tools.
|
||||
///
|
||||
/// This macro transforms a function into a tool that can be used by agents.
|
||||
///
|
||||
/// # Attributes
|
||||
///
|
||||
/// - `description`: A description of what the tool does (required for LLM understanding)
|
||||
/// - `name`: Override the tool name (defaults to function name)
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// use praisonai::tool;
|
||||
///
|
||||
/// #[tool(description = "Search the web")]
|
||||
/// async fn search(query: String) -> String {
|
||||
/// format!("Results for: {}", query)
|
||||
/// }
|
||||
///
|
||||
/// // With custom name
|
||||
/// #[tool(name = "web_search", description = "Search the internet")]
|
||||
/// async fn my_search_fn(query: String, max_results: u32) -> Vec<String> {
|
||||
/// vec![format!("Result for: {}", query)]
|
||||
/// }
|
||||
/// ```
|
||||
#[proc_macro_attribute]
|
||||
pub fn tool(attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
let input_fn = parse_macro_input!(item as ItemFn);
|
||||
|
||||
// Extract attributes from the attr token stream
|
||||
let mut description = String::new();
|
||||
let mut custom_name: Option<String> = None;
|
||||
|
||||
// Parse attributes manually for syn 2.x
|
||||
let attr_parser = syn::meta::parser(|meta| {
|
||||
if meta.path.is_ident("description") {
|
||||
let value: syn::LitStr = meta.value()?.parse()?;
|
||||
description = value.value();
|
||||
Ok(())
|
||||
} else if meta.path.is_ident("name") {
|
||||
let value: syn::LitStr = meta.value()?.parse()?;
|
||||
custom_name = Some(value.value());
|
||||
Ok(())
|
||||
} else {
|
||||
Err(meta.error("unsupported attribute"))
|
||||
}
|
||||
});
|
||||
|
||||
parse_macro_input!(attr with attr_parser);
|
||||
|
||||
// Get function details
|
||||
let fn_name = &input_fn.sig.ident;
|
||||
let fn_vis = &input_fn.vis;
|
||||
let fn_block = &input_fn.block;
|
||||
let fn_inputs = &input_fn.sig.inputs;
|
||||
let fn_output = &input_fn.sig.output;
|
||||
let fn_asyncness = &input_fn.sig.asyncness;
|
||||
|
||||
// Tool name (custom or function name)
|
||||
let tool_name = custom_name.unwrap_or_else(|| fn_name.to_string());
|
||||
|
||||
// Use docstring as description if not provided
|
||||
let description = if description.is_empty() {
|
||||
// Try to extract from doc comments
|
||||
let mut doc = String::new();
|
||||
for attr in &input_fn.attrs {
|
||||
if attr.path().is_ident("doc") {
|
||||
if let Meta::NameValue(nv) = &attr.meta {
|
||||
if let Expr::Lit(ExprLit {
|
||||
lit: Lit::Str(lit), ..
|
||||
}) = &nv.value
|
||||
{
|
||||
if !doc.is_empty() {
|
||||
doc.push(' ');
|
||||
}
|
||||
doc.push_str(lit.value().trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if doc.is_empty() {
|
||||
format!("Tool: {}", tool_name)
|
||||
} else {
|
||||
doc
|
||||
}
|
||||
} else {
|
||||
description
|
||||
};
|
||||
|
||||
// Generate parameter schema
|
||||
let mut param_names: Vec<syn::Ident> = Vec::new();
|
||||
let mut param_types: Vec<syn::Type> = Vec::new();
|
||||
let mut param_name_strs: Vec<String> = Vec::new();
|
||||
let mut param_json_types: Vec<String> = Vec::new();
|
||||
|
||||
for input in fn_inputs.iter() {
|
||||
if let syn::FnArg::Typed(pat_type) = input {
|
||||
if let syn::Pat::Ident(pat_ident) = &*pat_type.pat {
|
||||
let name = pat_ident.ident.clone();
|
||||
let name_str = name.to_string();
|
||||
let ty = (*pat_type.ty).clone();
|
||||
|
||||
// Map Rust types to JSON Schema types
|
||||
let json_type = rust_type_to_json_schema(&pat_type.ty);
|
||||
|
||||
param_names.push(name);
|
||||
param_name_strs.push(name_str);
|
||||
param_types.push(ty);
|
||||
param_json_types.push(json_type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the struct name for the tool
|
||||
let struct_name = format_ident!("{}Tool", to_pascal_case(&tool_name));
|
||||
|
||||
// Generate the implementation
|
||||
let expanded = quote! {
|
||||
// Keep the original function
|
||||
#fn_vis #fn_asyncness fn #fn_name(#fn_inputs) #fn_output #fn_block
|
||||
|
||||
/// Auto-generated tool struct for #fn_name
|
||||
#[derive(Debug, Clone)]
|
||||
#fn_vis struct #struct_name;
|
||||
|
||||
impl #struct_name {
|
||||
/// Create a new instance of this tool
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for #struct_name {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl praisonai::Tool for #struct_name {
|
||||
fn name(&self) -> &str {
|
||||
#tool_name
|
||||
}
|
||||
|
||||
fn description(&self) -> &str {
|
||||
#description
|
||||
}
|
||||
|
||||
fn parameters_schema(&self) -> serde_json::Value {
|
||||
let mut properties = serde_json::Map::new();
|
||||
let mut required = Vec::new();
|
||||
|
||||
#(
|
||||
properties.insert(
|
||||
#param_name_strs.to_string(),
|
||||
serde_json::json!({ "type": #param_json_types })
|
||||
);
|
||||
required.push(serde_json::Value::String(#param_name_strs.to_string()));
|
||||
)*
|
||||
|
||||
serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": properties,
|
||||
"required": required
|
||||
})
|
||||
}
|
||||
|
||||
async fn execute(&self, args: serde_json::Value) -> praisonai::Result<serde_json::Value> {
|
||||
#(
|
||||
let #param_names: #param_types = serde_json::from_value(
|
||||
args.get(#param_name_strs)
|
||||
.cloned()
|
||||
.unwrap_or(serde_json::Value::Null)
|
||||
).map_err(|e| praisonai::Error::tool(format!("Failed to parse {}: {}", #param_name_strs, e)))?;
|
||||
)*
|
||||
|
||||
let result = #fn_name(#(#param_names),*).await;
|
||||
serde_json::to_value(result)
|
||||
.map_err(|e| praisonai::Error::tool(format!("Failed to serialize result: {}", e)))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TokenStream::from(expanded)
|
||||
}
|
||||
|
||||
/// Convert a Rust type to JSON Schema type string
|
||||
fn rust_type_to_json_schema(ty: &syn::Type) -> String {
|
||||
let type_str = quote!(#ty).to_string().replace(" ", "");
|
||||
|
||||
match type_str.as_str() {
|
||||
"String" | "&str" | "str" => "string".to_string(),
|
||||
"i8" | "i16" | "i32" | "i64" | "i128" | "isize" | "u8" | "u16" | "u32" | "u64" | "u128"
|
||||
| "usize" => "integer".to_string(),
|
||||
"f32" | "f64" => "number".to_string(),
|
||||
"bool" => "boolean".to_string(),
|
||||
_ if type_str.starts_with("Vec<") => "array".to_string(),
|
||||
_ if type_str.starts_with("Option<") => {
|
||||
// Extract inner type
|
||||
let inner = &type_str[7..type_str.len() - 1];
|
||||
rust_type_str_to_json_schema(inner)
|
||||
}
|
||||
_ => "object".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn rust_type_str_to_json_schema(type_str: &str) -> String {
|
||||
match type_str {
|
||||
"String" | "&str" | "str" => "string".to_string(),
|
||||
"i8" | "i16" | "i32" | "i64" | "i128" | "isize" | "u8" | "u16" | "u32" | "u64" | "u128"
|
||||
| "usize" => "integer".to_string(),
|
||||
"f32" | "f64" => "number".to_string(),
|
||||
"bool" => "boolean".to_string(),
|
||||
_ if type_str.starts_with("Vec<") => "array".to_string(),
|
||||
_ => "object".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert snake_case to PascalCase
|
||||
fn to_pascal_case(s: &str) -> String {
|
||||
s.split('_')
|
||||
.map(|word| {
|
||||
let mut chars = word.chars();
|
||||
match chars.next() {
|
||||
None => String::new(),
|
||||
Some(first) => first.to_uppercase().chain(chars).collect(),
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
[package]
|
||||
name = "praisonai"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
repository.workspace = true
|
||||
description = "Core library for PraisonAI - Agent, Tools, Workflows"
|
||||
keywords = ["ai", "agents", "llm", "tools"]
|
||||
categories = ["api-bindings", "asynchronous"]
|
||||
|
||||
[dependencies]
|
||||
# Async runtime
|
||||
tokio = { workspace = true }
|
||||
async-trait = { workspace = true }
|
||||
futures = { workspace = true }
|
||||
|
||||
# Serialization
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
serde_yaml = { workspace = true }
|
||||
|
||||
# Error handling
|
||||
thiserror = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
|
||||
# LLM providers
|
||||
rig-core = { workspace = true }
|
||||
|
||||
# HTTP client
|
||||
reqwest = { workspace = true }
|
||||
|
||||
# Logging
|
||||
tracing = { workspace = true }
|
||||
|
||||
# UUID
|
||||
uuid = { workspace = true }
|
||||
|
||||
# Date/time
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
|
||||
# Directories
|
||||
dirs = "5"
|
||||
|
||||
# Regex for policy matching
|
||||
regex = "1"
|
||||
|
||||
# Lazy static initialization
|
||||
once_cell = "1"
|
||||
lazy_static = "1"
|
||||
|
||||
# TOML parsing for config
|
||||
toml = "0.8"
|
||||
|
||||
# Derive macros
|
||||
praisonai-derive = { path = "../praisonai-derive", version = "0.2.0" }
|
||||
|
||||
[dev-dependencies]
|
||||
tokio-test = { workspace = true }
|
||||
tracing-subscriber = { workspace = true }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
# Optional TUI support
|
||||
tui = ["ratatui", "crossterm"]
|
||||
|
||||
[dependencies.ratatui]
|
||||
version = "0.29"
|
||||
optional = true
|
||||
|
||||
[dependencies.crossterm]
|
||||
version = "0.28"
|
||||
optional = true
|
||||
|
||||
[[example]]
|
||||
name = "basic_agent"
|
||||
path = "../examples/basic_agent.rs"
|
||||
|
||||
[[example]]
|
||||
name = "with_tools"
|
||||
path = "../examples/with_tools.rs"
|
||||
|
||||
[[example]]
|
||||
name = "multi_agent"
|
||||
path = "../examples/multi_agent.rs"
|
||||
|
||||
[[example]]
|
||||
name = "memory_example"
|
||||
path = "../examples/memory_example.rs"
|
||||
|
||||
[[example]]
|
||||
name = "config_example"
|
||||
path = "../examples/config_example.rs"
|
||||
|
||||
[[example]]
|
||||
name = "workflow_patterns"
|
||||
path = "../examples/workflow_patterns.rs"
|
||||
@@ -1,249 +0,0 @@
|
||||
//! Agent builder pattern implementation
|
||||
//!
|
||||
//! Provides a fluent API for constructing agents.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::config::MemoryConfig;
|
||||
use crate::error::Result;
|
||||
use crate::llm::{LlmConfig, LlmProvider, OpenAiProvider};
|
||||
use crate::memory::Memory;
|
||||
use crate::tools::{Tool, ToolRegistry};
|
||||
|
||||
use super::Agent;
|
||||
|
||||
/// Configuration for an agent
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AgentConfig {
|
||||
/// Maximum iterations for tool calling loop
|
||||
pub max_iterations: usize,
|
||||
/// Enable verbose output
|
||||
pub verbose: bool,
|
||||
/// Enable streaming
|
||||
pub stream: bool,
|
||||
}
|
||||
|
||||
impl Default for AgentConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
max_iterations: 10,
|
||||
verbose: false,
|
||||
stream: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Builder for creating agents
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// let agent = Agent::new()
|
||||
/// .name("assistant")
|
||||
/// .instructions("You are helpful")
|
||||
/// .model("gpt-4o-mini")
|
||||
/// .build()?;
|
||||
/// ```
|
||||
pub struct AgentBuilder {
|
||||
name: Option<String>,
|
||||
instructions: Option<String>,
|
||||
llm_config: LlmConfig,
|
||||
tools: Vec<Box<dyn Tool>>,
|
||||
memory_config: MemoryConfig,
|
||||
config: AgentConfig,
|
||||
}
|
||||
|
||||
impl AgentBuilder {
|
||||
/// Create a new agent builder
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
name: None,
|
||||
instructions: None,
|
||||
llm_config: LlmConfig::default(),
|
||||
tools: Vec::new(),
|
||||
memory_config: MemoryConfig::default(),
|
||||
config: AgentConfig::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the agent name
|
||||
pub fn name(mut self, name: impl Into<String>) -> Self {
|
||||
self.name = Some(name.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the system instructions
|
||||
pub fn instructions(mut self, instructions: impl Into<String>) -> Self {
|
||||
self.instructions = Some(instructions.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the LLM model
|
||||
pub fn model(mut self, model: impl Into<String>) -> Self {
|
||||
self.llm_config.model = model.into();
|
||||
self
|
||||
}
|
||||
|
||||
/// Alias for model() - matches Python SDK
|
||||
pub fn llm(self, model: impl Into<String>) -> Self {
|
||||
self.model(model)
|
||||
}
|
||||
|
||||
/// Set the API key
|
||||
pub fn api_key(mut self, key: impl Into<String>) -> Self {
|
||||
self.llm_config.api_key = Some(key.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the base URL for the LLM API
|
||||
pub fn base_url(mut self, url: impl Into<String>) -> Self {
|
||||
self.llm_config.base_url = Some(url.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the temperature
|
||||
pub fn temperature(mut self, temp: f32) -> Self {
|
||||
self.llm_config.temperature = temp;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set max tokens
|
||||
pub fn max_tokens(mut self, max: u32) -> Self {
|
||||
self.llm_config.max_tokens = Some(max);
|
||||
self
|
||||
}
|
||||
|
||||
/// Add a tool
|
||||
pub fn tool(mut self, tool: impl Tool + 'static) -> Self {
|
||||
self.tools.push(Box::new(tool));
|
||||
self
|
||||
}
|
||||
|
||||
/// Add multiple tools
|
||||
pub fn tools(mut self, tools: impl IntoIterator<Item = impl Tool + 'static>) -> Self {
|
||||
for tool in tools {
|
||||
self.tools.push(Box::new(tool));
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Enable memory
|
||||
pub fn memory(mut self, enabled: bool) -> Self {
|
||||
self.memory_config.use_short_term = enabled;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set memory configuration
|
||||
pub fn memory_config(mut self, config: MemoryConfig) -> Self {
|
||||
self.memory_config = config;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set max iterations for tool calling
|
||||
pub fn max_iterations(mut self, max: usize) -> Self {
|
||||
self.config.max_iterations = max;
|
||||
self
|
||||
}
|
||||
|
||||
/// Enable verbose output
|
||||
pub fn verbose(mut self, enabled: bool) -> Self {
|
||||
self.config.verbose = enabled;
|
||||
self
|
||||
}
|
||||
|
||||
/// Enable/disable streaming
|
||||
pub fn stream(mut self, enabled: bool) -> Self {
|
||||
self.config.stream = enabled;
|
||||
self
|
||||
}
|
||||
|
||||
/// Build the agent
|
||||
pub fn build(self) -> Result<Agent> {
|
||||
let name = self.name.unwrap_or_else(|| "agent".to_string());
|
||||
let instructions = self
|
||||
.instructions
|
||||
.unwrap_or_else(|| "You are a helpful AI assistant.".to_string());
|
||||
|
||||
// Create LLM provider
|
||||
let llm: Arc<dyn LlmProvider> = Arc::new(OpenAiProvider::new(self.llm_config));
|
||||
|
||||
// Create tool registry
|
||||
let mut tool_registry = ToolRegistry::new();
|
||||
for tool in self.tools {
|
||||
// We need to register the boxed tool
|
||||
// This is a bit awkward but necessary for the builder pattern
|
||||
tool_registry.register(BoxedTool(tool));
|
||||
}
|
||||
|
||||
// Create memory
|
||||
let memory = Memory::in_memory(self.memory_config);
|
||||
|
||||
Ok(Agent::from_builder(
|
||||
name,
|
||||
instructions,
|
||||
llm,
|
||||
tool_registry,
|
||||
memory,
|
||||
self.config,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for AgentBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrapper to make Box<dyn Tool> implement Tool
|
||||
struct BoxedTool(Box<dyn Tool>);
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl crate::tools::Tool for BoxedTool {
|
||||
fn name(&self) -> &str {
|
||||
self.0.name()
|
||||
}
|
||||
|
||||
fn description(&self) -> &str {
|
||||
self.0.description()
|
||||
}
|
||||
|
||||
fn parameters_schema(&self) -> serde_json::Value {
|
||||
self.0.parameters_schema()
|
||||
}
|
||||
|
||||
async fn execute(&self, args: serde_json::Value) -> Result<serde_json::Value> {
|
||||
self.0.execute(args).await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_builder_defaults() {
|
||||
let builder = AgentBuilder::new();
|
||||
let agent = builder.build().unwrap();
|
||||
|
||||
assert_eq!(agent.name(), "agent");
|
||||
assert!(agent.instructions().contains("helpful"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_builder_chain() {
|
||||
let agent = AgentBuilder::new()
|
||||
.name("test-agent")
|
||||
.instructions("Be concise")
|
||||
.model("gpt-4")
|
||||
.temperature(0.5)
|
||||
.max_iterations(5)
|
||||
.verbose(true)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(agent.name(), "test-agent");
|
||||
assert_eq!(agent.instructions(), "Be concise");
|
||||
assert_eq!(agent.model(), "gpt-4");
|
||||
}
|
||||
}
|
||||
@@ -1,277 +0,0 @@
|
||||
//! Agent module for PraisonAI
|
||||
//!
|
||||
//! This module provides the core Agent struct and builder pattern.
|
||||
//! Agents are the primary execution unit in PraisonAI.
|
||||
//!
|
||||
//! # Example
|
||||
//!
|
||||
//! ```rust,ignore
|
||||
//! use praisonai_core::Agent;
|
||||
//!
|
||||
//! let agent = Agent::new()
|
||||
//! .name("assistant")
|
||||
//! .instructions("You are a helpful assistant")
|
||||
//! .build();
|
||||
//!
|
||||
//! let response = agent.chat("Hello!").await?;
|
||||
//! ```
|
||||
|
||||
mod builder;
|
||||
|
||||
pub use builder::{AgentBuilder, AgentConfig};
|
||||
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::llm::{LlmProvider, Message};
|
||||
use crate::memory::Memory;
|
||||
use crate::tools::{Tool, ToolRegistry, ToolResult};
|
||||
|
||||
/// The core Agent struct
|
||||
///
|
||||
/// Agents are the primary execution unit in PraisonAI. They combine:
|
||||
/// - LLM provider for generating responses
|
||||
/// - Tools for performing actions
|
||||
/// - Memory for conversation history
|
||||
/// - Instructions for behavior
|
||||
pub struct Agent {
|
||||
/// Unique agent ID
|
||||
id: String,
|
||||
/// Agent name
|
||||
name: String,
|
||||
/// System instructions
|
||||
instructions: String,
|
||||
/// LLM provider
|
||||
llm: Arc<dyn LlmProvider>,
|
||||
/// Tool registry
|
||||
tools: Arc<RwLock<ToolRegistry>>,
|
||||
/// Memory
|
||||
memory: Arc<RwLock<Memory>>,
|
||||
/// Configuration
|
||||
config: AgentConfig,
|
||||
}
|
||||
|
||||
impl Agent {
|
||||
/// Create a new agent builder
|
||||
#[allow(clippy::new_ret_no_self)]
|
||||
pub fn new() -> AgentBuilder {
|
||||
AgentBuilder::new()
|
||||
}
|
||||
|
||||
/// Create an agent with minimal config
|
||||
pub fn simple(instructions: impl Into<String>) -> Result<Self> {
|
||||
AgentBuilder::new().instructions(instructions).build()
|
||||
}
|
||||
|
||||
/// Get the agent ID
|
||||
pub fn id(&self) -> &str {
|
||||
&self.id
|
||||
}
|
||||
|
||||
/// Get the agent name
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
/// Get the instructions
|
||||
pub fn instructions(&self) -> &str {
|
||||
&self.instructions
|
||||
}
|
||||
|
||||
/// Get the LLM model name
|
||||
pub fn model(&self) -> &str {
|
||||
self.llm.model()
|
||||
}
|
||||
|
||||
/// Chat with the agent (main entry point)
|
||||
///
|
||||
/// This is the primary method for interacting with an agent.
|
||||
/// It handles the full conversation loop including tool calls.
|
||||
pub async fn chat(&self, prompt: &str) -> Result<String> {
|
||||
// Build messages
|
||||
let mut messages = vec![Message::system(&self.instructions)];
|
||||
|
||||
// Add conversation history
|
||||
{
|
||||
let memory = self.memory.read().await;
|
||||
let history = memory.history().await?;
|
||||
messages.extend(history);
|
||||
}
|
||||
|
||||
// Add user message
|
||||
let user_msg = Message::user(prompt);
|
||||
messages.push(user_msg.clone());
|
||||
|
||||
// Store user message in memory
|
||||
{
|
||||
let mut memory = self.memory.write().await;
|
||||
memory.store(user_msg).await?;
|
||||
}
|
||||
|
||||
// Get tool definitions
|
||||
let tool_defs = {
|
||||
let tools = self.tools.read().await;
|
||||
if tools.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(tools.definitions())
|
||||
}
|
||||
};
|
||||
|
||||
// Call LLM
|
||||
let mut iterations = 0;
|
||||
let max_iterations = self.config.max_iterations;
|
||||
|
||||
loop {
|
||||
iterations += 1;
|
||||
if iterations > max_iterations {
|
||||
return Err(Error::agent(format!(
|
||||
"Max iterations ({}) exceeded",
|
||||
max_iterations
|
||||
)));
|
||||
}
|
||||
|
||||
let response = self.llm.chat(&messages, tool_defs.as_deref()).await?;
|
||||
|
||||
// If no tool calls, return the response
|
||||
if response.tool_calls.is_empty() {
|
||||
let assistant_msg = Message::assistant(&response.content);
|
||||
|
||||
// Store assistant message
|
||||
{
|
||||
let mut memory = self.memory.write().await;
|
||||
memory.store(assistant_msg).await?;
|
||||
}
|
||||
|
||||
return Ok(response.content);
|
||||
}
|
||||
|
||||
// Handle tool calls
|
||||
let mut assistant_msg = Message::assistant(&response.content);
|
||||
assistant_msg.tool_calls = Some(response.tool_calls.clone());
|
||||
messages.push(assistant_msg);
|
||||
|
||||
for tool_call in &response.tool_calls {
|
||||
let args: serde_json::Value = serde_json::from_str(tool_call.arguments())
|
||||
.unwrap_or(serde_json::Value::Object(serde_json::Map::new()));
|
||||
|
||||
let result = {
|
||||
let tools = self.tools.read().await;
|
||||
tools.execute(tool_call.name(), args).await
|
||||
};
|
||||
|
||||
let tool_result = match result {
|
||||
Ok(r) => r,
|
||||
Err(e) => ToolResult::failure(tool_call.name(), e.to_string()),
|
||||
};
|
||||
|
||||
let result_str = if tool_result.success {
|
||||
serde_json::to_string(&tool_result.value).unwrap_or_default()
|
||||
} else {
|
||||
format!("Error: {}", tool_result.error.unwrap_or_default())
|
||||
};
|
||||
|
||||
messages.push(Message::tool(&tool_call.id, result_str));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Start a conversation (alias for chat)
|
||||
pub async fn start(&self, prompt: &str) -> Result<String> {
|
||||
self.chat(prompt).await
|
||||
}
|
||||
|
||||
/// Run a task (alias for chat)
|
||||
pub async fn run(&self, task: &str) -> Result<String> {
|
||||
self.chat(task).await
|
||||
}
|
||||
|
||||
/// Add a tool to the agent
|
||||
pub async fn add_tool(&self, tool: impl Tool + 'static) {
|
||||
let mut tools = self.tools.write().await;
|
||||
tools.register(tool);
|
||||
}
|
||||
|
||||
/// Get the number of tools
|
||||
pub async fn tool_count(&self) -> usize {
|
||||
let tools = self.tools.read().await;
|
||||
tools.len()
|
||||
}
|
||||
|
||||
/// Clear conversation memory
|
||||
pub async fn clear_memory(&self) -> Result<()> {
|
||||
let mut memory = self.memory.write().await;
|
||||
memory.clear().await
|
||||
}
|
||||
|
||||
/// Get conversation history
|
||||
pub async fn history(&self) -> Result<Vec<Message>> {
|
||||
let memory = self.memory.read().await;
|
||||
memory.history().await
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Agent {
|
||||
fn default() -> Self {
|
||||
AgentBuilder::new()
|
||||
.build()
|
||||
.expect("Default agent should build")
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Agent {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Agent")
|
||||
.field("id", &self.id)
|
||||
.field("name", &self.name)
|
||||
.field("model", &self.llm.model())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
// Internal constructor used by builder
|
||||
impl Agent {
|
||||
pub(crate) fn from_builder(
|
||||
name: String,
|
||||
instructions: String,
|
||||
llm: Arc<dyn LlmProvider>,
|
||||
tools: ToolRegistry,
|
||||
memory: Memory,
|
||||
config: AgentConfig,
|
||||
) -> Self {
|
||||
Self {
|
||||
id: Uuid::new_v4().to_string(),
|
||||
name,
|
||||
instructions,
|
||||
llm,
|
||||
tools: Arc::new(RwLock::new(tools)),
|
||||
memory: Arc::new(RwLock::new(memory)),
|
||||
config,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_agent_builder() {
|
||||
let agent = Agent::new()
|
||||
.name("test")
|
||||
.instructions("Be helpful")
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(agent.name(), "test");
|
||||
assert_eq!(agent.instructions(), "Be helpful");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_agent() {
|
||||
let agent = Agent::default();
|
||||
assert!(!agent.id().is_empty());
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,143 +0,0 @@
|
||||
//! Error types for PraisonAI Core
|
||||
//!
|
||||
//! This module defines the error types used throughout the crate.
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
/// Result type alias using our Error type
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// Main error type for PraisonAI Core
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
/// Agent-related errors
|
||||
#[error("Agent error: {0}")]
|
||||
Agent(String),
|
||||
|
||||
/// Tool execution errors
|
||||
#[error("Tool error: {0}")]
|
||||
Tool(String),
|
||||
|
||||
/// LLM provider errors
|
||||
#[error("LLM error: {0}")]
|
||||
Llm(String),
|
||||
|
||||
/// Memory errors
|
||||
#[error("Memory error: {0}")]
|
||||
Memory(String),
|
||||
|
||||
/// Workflow errors
|
||||
#[error("Workflow error: {0}")]
|
||||
Workflow(String),
|
||||
|
||||
/// Configuration errors
|
||||
#[error("Config error: {0}")]
|
||||
Config(String),
|
||||
|
||||
/// Embedding errors
|
||||
#[error("Embedding error: {0}")]
|
||||
Embedding(String),
|
||||
|
||||
/// Serialization/deserialization errors
|
||||
#[error("Serialization error: {0}")]
|
||||
Serialization(#[from] serde_json::Error),
|
||||
|
||||
/// YAML parsing errors
|
||||
#[error("YAML error: {0}")]
|
||||
Yaml(#[from] serde_yaml::Error),
|
||||
|
||||
/// HTTP request errors
|
||||
#[error("HTTP error: {0}")]
|
||||
Http(#[from] reqwest::Error),
|
||||
|
||||
/// IO errors
|
||||
#[error("IO error: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
/// Generic error with context
|
||||
#[error("{context}: {source}")]
|
||||
WithContext {
|
||||
context: String,
|
||||
#[source]
|
||||
source: Box<Error>,
|
||||
},
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Create an agent error
|
||||
pub fn agent(msg: impl Into<String>) -> Self {
|
||||
Self::Agent(msg.into())
|
||||
}
|
||||
|
||||
/// Create a tool error
|
||||
pub fn tool(msg: impl Into<String>) -> Self {
|
||||
Self::Tool(msg.into())
|
||||
}
|
||||
|
||||
/// Create an LLM error
|
||||
pub fn llm(msg: impl Into<String>) -> Self {
|
||||
Self::Llm(msg.into())
|
||||
}
|
||||
|
||||
/// Create a memory error
|
||||
pub fn memory(msg: impl Into<String>) -> Self {
|
||||
Self::Memory(msg.into())
|
||||
}
|
||||
|
||||
/// Create a workflow error
|
||||
pub fn workflow(msg: impl Into<String>) -> Self {
|
||||
Self::Workflow(msg.into())
|
||||
}
|
||||
|
||||
/// Create a config error
|
||||
pub fn config(msg: impl Into<String>) -> Self {
|
||||
Self::Config(msg.into())
|
||||
}
|
||||
|
||||
/// Create an IO error from a string message
|
||||
pub fn io(msg: impl Into<String>) -> Self {
|
||||
Self::Io(std::io::Error::other(msg.into()))
|
||||
}
|
||||
|
||||
/// Create a handoff error
|
||||
pub fn handoff(msg: impl Into<String>) -> Self {
|
||||
Self::Workflow(format!("Handoff error: {}", msg.into()))
|
||||
}
|
||||
|
||||
/// Add context to an error
|
||||
pub fn with_context(self, context: impl Into<String>) -> Self {
|
||||
Self::WithContext {
|
||||
context: context.into(),
|
||||
source: Box::new(self),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Extension trait for adding context to Results
|
||||
pub trait ResultExt<T> {
|
||||
/// Add context to an error
|
||||
fn context(self, context: impl Into<String>) -> Result<T>;
|
||||
}
|
||||
|
||||
impl<T> ResultExt<T> for Result<T> {
|
||||
fn context(self, context: impl Into<String>) -> Result<T> {
|
||||
self.map_err(|e| e.with_context(context))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_error_display() {
|
||||
let err = Error::agent("test error");
|
||||
assert_eq!(err.to_string(), "Agent error: test error");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_with_context() {
|
||||
let err = Error::tool("failed to execute").with_context("search_web");
|
||||
assert!(err.to_string().contains("search_web"));
|
||||
}
|
||||
}
|
||||
@@ -1,441 +0,0 @@
|
||||
//! PraisonAI Core - High-performance, agentic AI framework for Rust
|
||||
//!
|
||||
//! This crate provides the core functionality for building AI agents and multi-agent workflows.
|
||||
//!
|
||||
//! # Quick Start
|
||||
//!
|
||||
//! ```rust,ignore
|
||||
//! use praisonai::{Agent, tool};
|
||||
//!
|
||||
//! #[tool(description = "Search the web")]
|
||||
//! async fn search(query: String) -> String {
|
||||
//! format!("Results for: {}", query)
|
||||
//! }
|
||||
//!
|
||||
//! #[tokio::main]
|
||||
//! async fn main() -> anyhow::Result<()> {
|
||||
//! let agent = Agent::new()
|
||||
//! .instructions("You are a helpful assistant")
|
||||
//! .build();
|
||||
//!
|
||||
//! let response = agent.chat("Hello!").await?;
|
||||
//! println!("{}", response);
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! # Architecture
|
||||
//!
|
||||
//! PraisonAI follows an agent-centric design with these core components:
|
||||
//!
|
||||
//! - **Agent**: The core execution unit that processes prompts and uses tools
|
||||
//! - **Tool**: Functions that agents can call to perform actions
|
||||
//! - **AgentTeam**: Coordinates multiple agents for complex workflows
|
||||
//! - **AgentFlow**: Defines workflow patterns (sequential, parallel, etc.)
|
||||
//! - **Memory**: Persists conversation history and context
|
||||
//!
|
||||
//! # Design Principles
|
||||
//!
|
||||
//! - **Agent-Centric**: Every design decision centers on Agents
|
||||
//! - **Protocol-Driven**: Traits define contracts, implementations are pluggable
|
||||
//! - **Minimal API**: Fewer parameters, sensible defaults
|
||||
//! - **Performance-First**: Lazy loading, optional dependencies
|
||||
//! - **Async-Safe**: All I/O operations are async
|
||||
|
||||
pub mod agent;
|
||||
pub mod agents;
|
||||
pub mod bots;
|
||||
pub mod bus;
|
||||
pub mod conditions;
|
||||
pub mod config;
|
||||
pub mod context;
|
||||
pub mod display;
|
||||
pub mod embedding;
|
||||
pub mod error;
|
||||
pub mod eval;
|
||||
pub mod failover;
|
||||
pub mod gateway;
|
||||
pub mod guardrails;
|
||||
pub mod handoff;
|
||||
pub mod hooks;
|
||||
pub mod knowledge;
|
||||
pub mod llm;
|
||||
pub mod mcp;
|
||||
pub mod memory;
|
||||
pub mod planning;
|
||||
pub mod plugins;
|
||||
pub mod policy;
|
||||
pub mod presets;
|
||||
pub mod protocols;
|
||||
pub mod rag;
|
||||
pub mod sandbox;
|
||||
pub mod session;
|
||||
pub mod skills;
|
||||
pub mod specialized_agents;
|
||||
pub mod streaming;
|
||||
pub mod task;
|
||||
pub mod telemetry;
|
||||
pub mod thinking;
|
||||
pub mod tools;
|
||||
pub mod trace;
|
||||
pub mod workflows;
|
||||
|
||||
// Parity module - Python SDK feature parity
|
||||
pub mod parity;
|
||||
|
||||
// Re-export core types for simple API
|
||||
pub use agent::{Agent, AgentBuilder, AgentConfig};
|
||||
pub use config::{
|
||||
// Feature configs
|
||||
AutonomyConfig,
|
||||
AutonomyLevel,
|
||||
CachingConfig,
|
||||
ChunkingStrategy,
|
||||
// Core configs
|
||||
ExecutionConfig,
|
||||
GuardrailAction,
|
||||
GuardrailConfig,
|
||||
GuardrailResult,
|
||||
HooksConfig,
|
||||
KnowledgeConfig,
|
||||
MemoryConfig,
|
||||
// Multi-agent configs
|
||||
MultiAgentExecutionConfig,
|
||||
MultiAgentHooksConfig,
|
||||
MultiAgentMemoryConfig,
|
||||
MultiAgentOutputConfig,
|
||||
MultiAgentPlanningConfig,
|
||||
OutputConfig,
|
||||
PlanningConfig,
|
||||
ReflectionConfig,
|
||||
SkillsConfig,
|
||||
TemplateConfig,
|
||||
WebConfig,
|
||||
WebSearchProvider,
|
||||
};
|
||||
pub use error::{Error, Result};
|
||||
pub use hooks::{HookDecision, HookEvent, HookInput, HookRegistry, HookResult, HookRunner};
|
||||
pub use llm::{
|
||||
LlmConfig, LlmProvider, LlmResponse, Message, MockLlmProvider, Role, ToolCall, Usage,
|
||||
};
|
||||
pub use memory::{ConversationHistory, Memory, MemoryAdapter};
|
||||
pub use session::{
|
||||
FileSessionStore, InMemorySessionStore, Session, SessionData, SessionInfo, SessionMessage,
|
||||
SessionStore,
|
||||
};
|
||||
pub use task::{OnError, Task, TaskBuilder, TaskConfig, TaskOutput, TaskStatus, TaskType};
|
||||
pub use tools::{Tool, ToolRegistry, ToolResult};
|
||||
pub use workflows::{
|
||||
AgentFlow, AgentTeam, Loop, Parallel, Process, Repeat, Route, StepResult, WorkflowContext,
|
||||
};
|
||||
|
||||
// Handoff system
|
||||
pub use handoff::{
|
||||
ContextPolicy, Handoff, HandoffChain, HandoffConfig, HandoffFilters, HandoffInputData,
|
||||
HandoffResult,
|
||||
};
|
||||
|
||||
// Context management
|
||||
pub use context::{
|
||||
BudgetAllocation, ContextBudgeter, ContextConfig, ContextLedger, ContextManager,
|
||||
ContextSegment, MultiAgentContextManager, OptimizerStrategy,
|
||||
};
|
||||
|
||||
// Plugin system
|
||||
pub use plugins::{
|
||||
FunctionPlugin, Plugin, PluginHook, PluginInfo, PluginManager, PluginType,
|
||||
};
|
||||
|
||||
// Event bus
|
||||
pub use bus::{Event, EventBus, EventType};
|
||||
|
||||
// Specialized agents
|
||||
pub use agents::{
|
||||
AudioAgent, AudioAgentBuilder, AudioConfig, CodeAgent, CodeAgentBuilder, CodeConfig,
|
||||
CodeExecutionResult, DeepResearchAgent, DeepResearchAgentBuilder, DeepResearchConfig,
|
||||
DeepResearchResult, ImageAgent, ImageAgentBuilder, ImageConfig, ImageResult, OCRAgent,
|
||||
OCRAgentBuilder, OCRConfig, OCRPage, OCRResult, RealtimeAgent, RealtimeAgentBuilder,
|
||||
RealtimeConfig, ResearchCitation, VideoAgent, VideoAgentBuilder, VideoConfig, VideoResult,
|
||||
VideoStatus, VisionAgent, VisionAgentBuilder, VisionConfig,
|
||||
};
|
||||
|
||||
// RAG system
|
||||
pub use rag::{
|
||||
build_context, deduplicate_chunks, estimate_tokens, get_model_context_window, truncate_context,
|
||||
Citation, CitationsMode, ContextChunk, ContextPack, RAGBuilder, RAGConfig, RAGResult,
|
||||
RetrievalConfig, RetrievalResult, RetrievalStrategy, TokenBudget, RAG,
|
||||
};
|
||||
|
||||
// MCP integration
|
||||
pub use mcp::{
|
||||
ConnectionStatus, MCPBuilder, MCPCall, MCPCallResult, MCPConfig, MCPContent, MCPPrompt,
|
||||
MCPResource, MCPServer, MCPTool, SecurityConfig, TransportConfig, TransportType, MCP,
|
||||
};
|
||||
|
||||
// Protocol system
|
||||
pub use protocols::{
|
||||
AgentMetrics, AgentOSConfig, AgentOSProtocol, AgentProtocol, BotAction, BotAttachment,
|
||||
BotMessage, BotProtocol, BotResponse, LlmMessage, LlmProtocol, LlmResponse as ProtocolLlmResponse,
|
||||
MemoryMessage, MemoryProtocol, RunnableAgentProtocol, TokenUsage, ToolCall as ProtocolToolCall,
|
||||
ToolProtocol, ToolSchema,
|
||||
};
|
||||
|
||||
// Knowledge system
|
||||
pub use knowledge::{
|
||||
AddResult as KnowledgeAddResult, Chunking, ChunkingConfig, ChunkingStrategy as KnowledgeChunkingStrategy,
|
||||
Document, IndexStats, IndexType, InMemoryVectorStore, Knowledge, KnowledgeBackendError,
|
||||
KnowledgeBuilder, KnowledgeConfig as KnowledgeModuleConfig, KnowledgeStoreProtocol, QueryMode, QueryResult,
|
||||
RerankerProtocol, RerankResult, RetrievalResult as KnowledgeRetrievalResult,
|
||||
RetrievalStrategy as KnowledgeRetrievalStrategy, RetrieverProtocol, ScopeRequiredError,
|
||||
SearchResult, SearchResultItem, SimpleReranker, VectorRecord, VectorStoreProtocol,
|
||||
};
|
||||
|
||||
// Streaming system
|
||||
pub use streaming::{
|
||||
AsyncStreamCallback, StreamCallback, StreamCollector, StreamEvent, StreamEventType,
|
||||
StreamHandler, StreamMetrics, ToolCallData,
|
||||
};
|
||||
|
||||
// Guardrails system
|
||||
pub use guardrails::{
|
||||
AsyncGuardrail, BlocklistGuardrail, FunctionGuardrail, Guardrail, GuardrailAction as GuardrailsAction,
|
||||
GuardrailChain, GuardrailConfig as GuardrailsConfig, GuardrailResult as GuardrailsResult,
|
||||
LengthGuardrail, PatternGuardrail,
|
||||
};
|
||||
|
||||
// Thinking system
|
||||
pub use thinking::{
|
||||
BudgetLevel, ThinkingBudget, ThinkingBudgetBuilder, ThinkingConfig, ThinkingTracker,
|
||||
ThinkingUsage,
|
||||
};
|
||||
|
||||
// Skills system
|
||||
pub use skills::{
|
||||
format_skill_for_prompt, generate_skills_xml, ParseError, SkillLoader, SkillManager,
|
||||
SkillMetadata, SkillProperties, ValidationError,
|
||||
};
|
||||
|
||||
// Planning system
|
||||
pub use planning::{
|
||||
is_read_only_tool, is_research_tool, is_restricted_tool, Plan, PlanStep, PlanStorage,
|
||||
StepStatus, TodoItem, TodoList, TodoPriority, READ_ONLY_TOOLS, RESEARCH_TOOLS,
|
||||
RESTRICTED_TOOLS,
|
||||
};
|
||||
|
||||
// Eval system
|
||||
pub use eval::{
|
||||
AccuracyEvaluator, AccuracyEvaluatorBuilder, AccuracyResult, CriteriaEvaluator,
|
||||
CriteriaEvaluatorBuilder, CriteriaResult, CriteriaScore, EvaluationScore, EvaluatorConfig,
|
||||
Judge, JudgeConfig, JudgeResult, PerformanceEvaluator, PerformanceEvaluatorBuilder,
|
||||
PerformanceMetrics, PerformanceResult, ReliabilityEvaluator, ReliabilityEvaluatorBuilder,
|
||||
ReliabilityResult, ToolCallResult as EvalToolCallResult,
|
||||
};
|
||||
|
||||
// Telemetry system
|
||||
pub use telemetry::{
|
||||
get_collector, get_monitor, get_performance_report, record_event, track_api, track_function,
|
||||
ApiStats, FunctionStats, PerformanceMonitor, PerformanceReport, TelemetryCollector,
|
||||
TelemetryEvent, TelemetryEventType,
|
||||
};
|
||||
|
||||
// Policy system
|
||||
pub use policy::{
|
||||
credit_card_rule, email_rule, phone_rule, profanity_rule, ssn_rule, PolicyAction,
|
||||
PolicyEngine, PolicyResult, PolicyRule,
|
||||
};
|
||||
|
||||
// Trace system
|
||||
pub use trace::{
|
||||
ConsoleExporter, ContextEvent, ContextEventType, ContextListSink, ContextNoOpSink,
|
||||
ContextTraceEmitter, ContextTraceSinkProtocol, JsonFileExporter, Span, SpanEvent, SpanKind,
|
||||
SpanStatus, TraceContext, TraceExporter, Tracer,
|
||||
};
|
||||
|
||||
// Embedding system
|
||||
pub use embedding::{
|
||||
cosine_similarity, get_dimensions, EmbeddingAgent, EmbeddingAgentBuilder, EmbeddingConfig,
|
||||
EmbeddingResult, EmbeddingUsage, SimilarityResult,
|
||||
};
|
||||
|
||||
// Failover system
|
||||
pub use failover::{
|
||||
AuthProfile, FailoverConfig, FailoverManager, FailoverStatus, ProviderStatus,
|
||||
};
|
||||
|
||||
// Gateway system
|
||||
pub use gateway::{
|
||||
EventType as GatewayEventType, GatewayClientProtocol, GatewayConfig, GatewayEvent,
|
||||
GatewayHealth, GatewayMessage, GatewayProtocol, GatewaySessionProtocol,
|
||||
};
|
||||
|
||||
// Sandbox system
|
||||
pub use sandbox::{
|
||||
ResourceLimits, ResourceUsage, SandboxConfig, SandboxProtocol, SandboxResult, SandboxStatus,
|
||||
SandboxStatusInfo,
|
||||
};
|
||||
|
||||
// Conditions system
|
||||
pub use conditions::{
|
||||
evaluate_condition, ClosureCondition, ConditionProtocol, DictCondition, ExpressionCondition,
|
||||
If, RoutingConditionProtocol,
|
||||
};
|
||||
|
||||
// Bots system
|
||||
pub use bots::{
|
||||
BotChannel, BotConfig, BotMessage as BotsChatMessage, BotProtocol as BotsChatProtocol,
|
||||
BotUser, MessageType,
|
||||
};
|
||||
|
||||
// FastContext types (from context module)
|
||||
pub use context::{
|
||||
FastContextConfig, FastContextResult, FileMatch, LineRange,
|
||||
};
|
||||
|
||||
// Display system
|
||||
pub use display::{
|
||||
// Types
|
||||
ApprovalDecision, ColorPalette, DisplayEvent, DisplayType, RiskLevel,
|
||||
// Callback types (re-exported for convenience)
|
||||
// Functions
|
||||
add_approval_callback, add_display_callback, callback_count, clean_display_content,
|
||||
clear_all_callbacks, clear_display_callbacks, display_error, display_generating,
|
||||
display_instruction, display_interaction, display_reasoning_steps, display_self_reflection,
|
||||
display_tool_call, display_working_status, execute_async_callbacks, execute_callbacks,
|
||||
execute_sync_callbacks, has_callbacks, register_approval_callback,
|
||||
register_async_display_callback, register_display_callback, request_approval,
|
||||
// Constants
|
||||
PRAISON_COLORS, WORKING_FRAMES, WORKING_PHASES,
|
||||
};
|
||||
|
||||
// Presets system
|
||||
pub use presets::{
|
||||
// Preset types
|
||||
AutonomyPreset, CachingPreset, ContextPreset, ExecutionPreset, GuardrailPreset,
|
||||
KnowledgePreset, MemoryPreset, MultiAgentExecutionPreset, MultiAgentOutputPreset,
|
||||
OutputPreset, PlanningPreset, ReflectionPreset, WebPreset, WorkflowStepExecutionPreset,
|
||||
// Static preset maps
|
||||
AUTONOMY_PRESETS, CACHING_PRESETS, CONTEXT_PRESETS, EXECUTION_PRESETS, GUARDRAIL_PRESETS,
|
||||
KNOWLEDGE_PRESETS, MEMORY_PRESETS, MEMORY_URL_SCHEMES, MULTI_AGENT_EXECUTION_PRESETS,
|
||||
MULTI_AGENT_OUTPUT_PRESETS, OUTPUT_PRESETS, PLANNING_PRESETS, REFLECTION_PRESETS,
|
||||
WEB_PRESETS, WORKFLOW_STEP_EXECUTION_PRESETS,
|
||||
// Resolve functions
|
||||
detect_memory_backend, resolve_autonomy_preset, resolve_caching_preset,
|
||||
resolve_context_preset, resolve_execution_preset, resolve_guardrail_preset,
|
||||
resolve_knowledge_preset, resolve_memory_preset, resolve_multi_agent_execution_preset,
|
||||
resolve_multi_agent_output_preset, resolve_output_preset, resolve_planning_preset,
|
||||
resolve_reflection_preset, resolve_web_preset, resolve_workflow_step_execution_preset,
|
||||
// Constants
|
||||
DEFAULT_OUTPUT_MODE,
|
||||
};
|
||||
|
||||
// Specialized agents
|
||||
pub use specialized_agents::{
|
||||
// PromptExpanderAgent
|
||||
ExpandPrompts, ExpandResult, ExpandStrategy, PromptExpanderAgent, PromptExpanderAgentBuilder,
|
||||
PromptExpanderConfig,
|
||||
// QueryRewriterAgent
|
||||
QueryRewriterAgent, QueryRewriterAgentBuilder, QueryRewriterConfig, RewritePrompts,
|
||||
RewriteResult, RewriteStrategy,
|
||||
};
|
||||
|
||||
// Parity module - Python SDK feature parity exports
|
||||
pub use parity::{
|
||||
// UI protocols
|
||||
A2A, AGUI, A2AAgentCard, A2AAgentCapabilities, A2AAgentSkill, A2ATask, A2ATaskState,
|
||||
AGUIEvent, AGUIEventType, AGUIMessage, AGUIRole, AGUIRunInput,
|
||||
// Plugin protocols (ParityPluginType to avoid conflict with existing PluginType)
|
||||
PluginProtocol, ToolPluginProtocol, HookPluginProtocol, AgentPluginProtocol, LLMPluginProtocol,
|
||||
PluginMetadata, ParityPluginType, PluginParseError, PluginRegistry as ParityPluginRegistry,
|
||||
ParityToolDefinition, LLMMessage,
|
||||
parse_plugin_header, parse_plugin_header_from_file, discover_plugins, get_default_plugin_dirs,
|
||||
discover_and_load_plugins, ensure_plugin_dir, get_plugin_template,
|
||||
// Config loader (SessionConfig renamed to avoid conflict)
|
||||
PraisonConfig, PluginsConfig, DefaultsConfig, ManagerConfig,
|
||||
SessionConfig as ParitySessionConfig, ConfigValidationError, PluginsEnabled,
|
||||
ConfigAutoRagConfig, SpecializedAutoRagConfig,
|
||||
get_config, get_config_path, get_plugins_config, get_defaults_config, get_default,
|
||||
is_plugins_enabled, get_enabled_plugins, apply_config_defaults, validate_config,
|
||||
// Parameter resolver
|
||||
ArrayMode, ResolvedValue, ResolveOptions, StringMode,
|
||||
resolve, resolve_memory, resolve_knowledge, resolve_output, resolve_execution,
|
||||
resolve_planning, resolve_reflection, resolve_context, resolve_routing,
|
||||
resolve_hooks, resolve_guardrails, resolve_web, resolve_autonomy, resolve_caching, resolve_skills,
|
||||
// Parse utilities
|
||||
detect_url_scheme, is_path_like, is_numeric_string, is_policy_string, parse_policy_string,
|
||||
suggest_similar, clean_triple_backticks, clean_whitespace, extract_json,
|
||||
validate_keys, make_preset_error, make_array_error,
|
||||
// Workflow aliases (renamed to avoid conflicts with existing types)
|
||||
Workflow, Pipeline, ParityAgents, AgentManager, Agents,
|
||||
loop_step, parallel as parallel_step, repeat as repeat_step, route as route_step, when,
|
||||
ParityHandoffConfig, ParityHandoffFilters, HandoffFilter, handoff, handoff_filters, prompt_with_handoff_instructions,
|
||||
// Telemetry functions
|
||||
MinimalTelemetry, TelemetryContext,
|
||||
get_telemetry, enable_telemetry, disable_telemetry, is_telemetry_enabled,
|
||||
enable_performance_mode, disable_performance_mode, is_performance_mode, cleanup_telemetry_resources,
|
||||
// Display types
|
||||
ErrorLog, DisplayCallback, AsyncDisplayCallback, ParityDisplayEvent,
|
||||
ParityDisplayEventType, ParityApprovalCallback,
|
||||
ParityApprovalDecision, ParityRiskLevel, FlowDisplay, PraisonColors,
|
||||
get_error_logs, add_error_log, clear_error_logs, log_error,
|
||||
parity_register_display_callback, parity_register_async_display_callback,
|
||||
parity_add_display_callback, parity_remove_display_callback,
|
||||
execute_callback, execute_async_callback,
|
||||
parity_register_approval_callback, parity_add_approval_callback,
|
||||
parity_remove_approval_callback, parity_request_approval,
|
||||
PARITY_WORKING_FRAMES, PARITY_WORKING_PHASES,
|
||||
// Specialized types
|
||||
ContextAgent, ContextAgentConfig, ContextStrategy, ContextEntry, create_context_agent, create_context_agent_with_config,
|
||||
PlanningAgent, PlanningAgentConfig, PlanningStep, PlanningStepStatus,
|
||||
FastContext, AutoAgents, AutoAgentsConfig, AutoAgentSpec,
|
||||
AutoRagAgent, TraceSinkProtocol, TraceEvent, ContextTraceSink, TraceSink,
|
||||
MemoryBackend, Tools,
|
||||
// Deep Research types (extras) - renamed to avoid conflicts
|
||||
Citation as DeepResearchCitation, ReasoningStep, WebSearchCall, CodeExecutionStep, FileSearchCall,
|
||||
Provider, DeepResearchResponse,
|
||||
// RAG types (extras)
|
||||
RAGCitation, RetrievalPolicy, RagRetrievalPolicy,
|
||||
// Guardrail types (extras)
|
||||
LLMGuardrail,
|
||||
// Handoff errors (extras)
|
||||
HandoffError, HandoffCycleError, HandoffDepthError, HandoffTimeoutError,
|
||||
// App protocols (extras) - renamed to avoid conflicts
|
||||
AgentAppProtocol, AgentOSProtocol as ParityAgentOSProtocol, AgentAppConfig,
|
||||
// Sandbox types (extras)
|
||||
SecurityPolicy,
|
||||
// Reflection types (extras)
|
||||
ReflectionOutput,
|
||||
// Embedding functions (extras) - renamed to avoid conflicts
|
||||
EmbeddingResult as ParityEmbeddingResult, EmbeddingUsage as ParityEmbeddingUsage,
|
||||
embed, embedding, embeddings, aembed, aembedding, aembeddings,
|
||||
// Display callbacks (extras)
|
||||
sync_display_callbacks, async_display_callbacks, error_logs,
|
||||
// Presets (extras) - renamed to avoid conflicts
|
||||
AUTONOMY_PRESETS as PARITY_AUTONOMY_PRESETS, RECOMMENDED_PROMPT_PREFIX,
|
||||
// Resolver functions (extras)
|
||||
resolve_guardrail_policies,
|
||||
// Trace functions (extras)
|
||||
TraceContextData, trace_context, track_workflow,
|
||||
// Plugin functions (extras)
|
||||
load_plugin,
|
||||
};
|
||||
|
||||
// Note: The following Python SDK items have Rust equivalents:
|
||||
// - `AUTONOMY_PRESETS` - exported from presets module
|
||||
// - `RAGCitation` - exported as Citation from rag module
|
||||
// - `Workflow` - exported from parity module
|
||||
// - `loop` is a reserved keyword in Rust, use `loop_step` instead
|
||||
// - `config`, `db`, `obs`, `memory`, `tools`, `workflows` are modules in Python
|
||||
// that already exist in Rust as pub mod declarations
|
||||
|
||||
// Re-export the tool macro from praisonai-derive
|
||||
pub use praisonai_derive::tool;
|
||||
|
||||
/// Prelude module for convenient imports
|
||||
pub mod prelude {
|
||||
pub use crate::{
|
||||
tool, Agent, AgentBuilder, AgentConfig, AgentFlow, AgentTeam, Error, HookEvent,
|
||||
HookRegistry, HookResult, LlmConfig, LlmProvider, Loop, Memory, MemoryConfig, Message,
|
||||
Parallel, Process, Repeat, Result, Role, Route, Session, Task, TaskOutput, Tool,
|
||||
ToolRegistry, ToolResult,
|
||||
};
|
||||
}
|
||||
@@ -1,520 +0,0 @@
|
||||
//! LLM Provider abstraction for PraisonAI
|
||||
//!
|
||||
//! This module provides the LLM provider trait and implementations.
|
||||
//! Uses rig-core for multi-provider support (OpenAI, Anthropic, Ollama, etc.)
|
||||
|
||||
use async_trait::async_trait;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::tools::ToolDefinition;
|
||||
|
||||
/// Message role in a conversation
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Role {
|
||||
/// System message (instructions)
|
||||
System,
|
||||
/// User message
|
||||
User,
|
||||
/// Assistant message
|
||||
Assistant,
|
||||
/// Tool/function result
|
||||
Tool,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Role {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Role::System => write!(f, "system"),
|
||||
Role::User => write!(f, "user"),
|
||||
Role::Assistant => write!(f, "assistant"),
|
||||
Role::Tool => write!(f, "tool"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A message in a conversation
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Message {
|
||||
/// The role of the message sender
|
||||
pub role: Role,
|
||||
/// The content of the message
|
||||
pub content: String,
|
||||
/// Tool call ID (for tool responses)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub tool_call_id: Option<String>,
|
||||
/// Tool calls made by the assistant
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub tool_calls: Option<Vec<ToolCall>>,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
/// Create a system message
|
||||
pub fn system(content: impl Into<String>) -> Self {
|
||||
Self {
|
||||
role: Role::System,
|
||||
content: content.into(),
|
||||
tool_call_id: None,
|
||||
tool_calls: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a user message
|
||||
pub fn user(content: impl Into<String>) -> Self {
|
||||
Self {
|
||||
role: Role::User,
|
||||
content: content.into(),
|
||||
tool_call_id: None,
|
||||
tool_calls: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create an assistant message
|
||||
pub fn assistant(content: impl Into<String>) -> Self {
|
||||
Self {
|
||||
role: Role::Assistant,
|
||||
content: content.into(),
|
||||
tool_call_id: None,
|
||||
tool_calls: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a tool response message
|
||||
pub fn tool(tool_call_id: impl Into<String>, content: impl Into<String>) -> Self {
|
||||
Self {
|
||||
role: Role::Tool,
|
||||
content: content.into(),
|
||||
tool_call_id: Some(tool_call_id.into()),
|
||||
tool_calls: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Function details within a tool call
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ToolCallFunction {
|
||||
/// The tool/function name
|
||||
pub name: String,
|
||||
/// The arguments as JSON string
|
||||
pub arguments: String,
|
||||
}
|
||||
|
||||
/// A tool call made by the LLM
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ToolCall {
|
||||
/// Unique ID for this tool call
|
||||
pub id: String,
|
||||
/// The type of tool call (always "function" for now)
|
||||
#[serde(rename = "type")]
|
||||
pub call_type: String,
|
||||
/// The function details
|
||||
pub function: ToolCallFunction,
|
||||
}
|
||||
|
||||
impl ToolCall {
|
||||
/// Create a new tool call
|
||||
pub fn new(id: impl Into<String>, name: impl Into<String>, arguments: impl Into<String>) -> Self {
|
||||
Self {
|
||||
id: id.into(),
|
||||
call_type: "function".to_string(),
|
||||
function: ToolCallFunction {
|
||||
name: name.into(),
|
||||
arguments: arguments.into(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the function name (convenience method)
|
||||
pub fn name(&self) -> &str {
|
||||
&self.function.name
|
||||
}
|
||||
|
||||
/// Get the function arguments (convenience method)
|
||||
pub fn arguments(&self) -> &str {
|
||||
&self.function.arguments
|
||||
}
|
||||
}
|
||||
|
||||
/// LLM response
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LlmResponse {
|
||||
/// The response content
|
||||
pub content: String,
|
||||
/// Tool calls (if any)
|
||||
pub tool_calls: Vec<ToolCall>,
|
||||
/// Finish reason
|
||||
pub finish_reason: Option<String>,
|
||||
/// Usage statistics
|
||||
pub usage: Option<Usage>,
|
||||
}
|
||||
|
||||
/// Token usage statistics
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct Usage {
|
||||
/// Prompt tokens
|
||||
pub prompt_tokens: u32,
|
||||
/// Completion tokens
|
||||
pub completion_tokens: u32,
|
||||
/// Total tokens
|
||||
pub total_tokens: u32,
|
||||
}
|
||||
|
||||
/// LLM configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LlmConfig {
|
||||
/// Model name (e.g., "gpt-4o-mini", "claude-3-sonnet")
|
||||
pub model: String,
|
||||
/// API key (optional, can use env var)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub api_key: Option<String>,
|
||||
/// Base URL (optional, for custom endpoints)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub base_url: Option<String>,
|
||||
/// Temperature (0.0 - 2.0)
|
||||
#[serde(default = "default_temperature")]
|
||||
pub temperature: f32,
|
||||
/// Max tokens
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub max_tokens: Option<u32>,
|
||||
}
|
||||
|
||||
fn default_temperature() -> f32 {
|
||||
0.7
|
||||
}
|
||||
|
||||
impl Default for LlmConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
model: "gpt-4o-mini".to_string(),
|
||||
api_key: None,
|
||||
base_url: None,
|
||||
temperature: default_temperature(),
|
||||
max_tokens: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LlmConfig {
|
||||
/// Create a new LLM config with the given model
|
||||
pub fn new(model: impl Into<String>) -> Self {
|
||||
Self {
|
||||
model: model.into(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the API key
|
||||
pub fn api_key(mut self, key: impl Into<String>) -> Self {
|
||||
self.api_key = Some(key.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the base URL
|
||||
pub fn base_url(mut self, url: impl Into<String>) -> Self {
|
||||
self.base_url = Some(url.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the temperature
|
||||
pub fn temperature(mut self, temp: f32) -> Self {
|
||||
self.temperature = temp;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set max tokens
|
||||
pub fn max_tokens(mut self, max: u32) -> Self {
|
||||
self.max_tokens = Some(max);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for LLM providers
|
||||
#[async_trait]
|
||||
pub trait LlmProvider: Send + Sync {
|
||||
/// Send a chat completion request
|
||||
async fn chat(
|
||||
&self,
|
||||
messages: &[Message],
|
||||
tools: Option<&[ToolDefinition]>,
|
||||
) -> Result<LlmResponse>;
|
||||
|
||||
/// Stream a chat completion (returns chunks)
|
||||
async fn chat_stream(
|
||||
&self,
|
||||
messages: &[Message],
|
||||
tools: Option<&[ToolDefinition]>,
|
||||
) -> Result<Box<dyn futures::Stream<Item = Result<String>> + Send + Unpin>>;
|
||||
|
||||
/// Get the model name
|
||||
fn model(&self) -> &str;
|
||||
}
|
||||
|
||||
/// OpenAI-compatible LLM provider
|
||||
pub struct OpenAiProvider {
|
||||
config: LlmConfig,
|
||||
client: reqwest::Client,
|
||||
}
|
||||
|
||||
impl OpenAiProvider {
|
||||
/// Create a new OpenAI provider
|
||||
pub fn new(config: LlmConfig) -> Self {
|
||||
Self {
|
||||
config,
|
||||
client: reqwest::Client::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create with default config
|
||||
pub fn default_model() -> Self {
|
||||
Self::new(LlmConfig::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl LlmProvider for OpenAiProvider {
|
||||
async fn chat(
|
||||
&self,
|
||||
messages: &[Message],
|
||||
tools: Option<&[ToolDefinition]>,
|
||||
) -> Result<LlmResponse> {
|
||||
let api_key = self
|
||||
.config
|
||||
.api_key
|
||||
.clone()
|
||||
.or_else(|| std::env::var("OPENAI_API_KEY").ok())
|
||||
.ok_or_else(|| crate::error::Error::llm("OPENAI_API_KEY not set"))?;
|
||||
|
||||
let base_url = self
|
||||
.config
|
||||
.base_url
|
||||
.as_deref()
|
||||
.unwrap_or("https://api.openai.com/v1");
|
||||
|
||||
let mut body = serde_json::json!({
|
||||
"model": self.config.model,
|
||||
"messages": messages,
|
||||
"temperature": self.config.temperature,
|
||||
});
|
||||
|
||||
if let Some(max_tokens) = self.config.max_tokens {
|
||||
body["max_tokens"] = serde_json::json!(max_tokens);
|
||||
}
|
||||
|
||||
if let Some(tools) = tools {
|
||||
if !tools.is_empty() {
|
||||
let tool_defs: Vec<_> = tools
|
||||
.iter()
|
||||
.map(|t| {
|
||||
serde_json::json!({
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": t.name,
|
||||
"description": t.description,
|
||||
"parameters": t.parameters,
|
||||
}
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
body["tools"] = serde_json::json!(tool_defs);
|
||||
}
|
||||
}
|
||||
|
||||
let response = self
|
||||
.client
|
||||
.post(format!("{}/chat/completions", base_url))
|
||||
.header("Authorization", format!("Bearer {}", api_key))
|
||||
.header("Content-Type", "application/json")
|
||||
.json(&body)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
let error_text = response.text().await.unwrap_or_default();
|
||||
return Err(crate::error::Error::llm(format!(
|
||||
"API error: {}",
|
||||
error_text
|
||||
)));
|
||||
}
|
||||
|
||||
let data: serde_json::Value = response.json().await?;
|
||||
|
||||
let choice = data["choices"]
|
||||
.get(0)
|
||||
.ok_or_else(|| crate::error::Error::llm("No choices in response"))?;
|
||||
|
||||
let message = &choice["message"];
|
||||
let content = message["content"].as_str().unwrap_or("").to_string();
|
||||
|
||||
let tool_calls = if let Some(calls) = message["tool_calls"].as_array() {
|
||||
calls
|
||||
.iter()
|
||||
.filter_map(|call| {
|
||||
Some(ToolCall::new(
|
||||
call["id"].as_str()?.to_string(),
|
||||
call["function"]["name"].as_str()?.to_string(),
|
||||
call["function"]["arguments"].as_str()?.to_string(),
|
||||
))
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
|
||||
let usage = data["usage"].as_object().map(|u| Usage {
|
||||
prompt_tokens: u["prompt_tokens"].as_u64().unwrap_or(0) as u32,
|
||||
completion_tokens: u["completion_tokens"].as_u64().unwrap_or(0) as u32,
|
||||
total_tokens: u["total_tokens"].as_u64().unwrap_or(0) as u32,
|
||||
});
|
||||
|
||||
Ok(LlmResponse {
|
||||
content,
|
||||
tool_calls,
|
||||
finish_reason: choice["finish_reason"].as_str().map(|s| s.to_string()),
|
||||
usage,
|
||||
})
|
||||
}
|
||||
|
||||
async fn chat_stream(
|
||||
&self,
|
||||
_messages: &[Message],
|
||||
_tools: Option<&[ToolDefinition]>,
|
||||
) -> Result<Box<dyn futures::Stream<Item = Result<String>> + Send + Unpin>> {
|
||||
// TODO: Implement streaming
|
||||
Err(crate::error::Error::llm("Streaming not yet implemented"))
|
||||
}
|
||||
|
||||
fn model(&self) -> &str {
|
||||
&self.config.model
|
||||
}
|
||||
}
|
||||
|
||||
/// Mock LLM provider for testing (no API calls)
|
||||
pub struct MockLlmProvider {
|
||||
model: String,
|
||||
responses: std::sync::Mutex<Vec<String>>,
|
||||
tool_calls: std::sync::Mutex<Vec<Vec<ToolCall>>>,
|
||||
}
|
||||
|
||||
impl MockLlmProvider {
|
||||
/// Create a new mock provider
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
model: "mock-model".to_string(),
|
||||
responses: std::sync::Mutex::new(vec![]),
|
||||
tool_calls: std::sync::Mutex::new(vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
/// Add a response to return (FIFO queue)
|
||||
pub fn add_response(&self, response: impl Into<String>) {
|
||||
self.responses.lock().unwrap().push(response.into());
|
||||
}
|
||||
|
||||
/// Add tool calls to return with next response
|
||||
pub fn add_tool_calls(&self, calls: Vec<ToolCall>) {
|
||||
self.tool_calls.lock().unwrap().push(calls);
|
||||
}
|
||||
|
||||
/// Create with a single response
|
||||
pub fn with_response(response: impl Into<String>) -> Self {
|
||||
let provider = Self::new();
|
||||
provider.add_response(response);
|
||||
provider
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for MockLlmProvider {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl LlmProvider for MockLlmProvider {
|
||||
async fn chat(
|
||||
&self,
|
||||
_messages: &[Message],
|
||||
_tools: Option<&[ToolDefinition]>,
|
||||
) -> Result<LlmResponse> {
|
||||
let content = self
|
||||
.responses
|
||||
.lock()
|
||||
.unwrap()
|
||||
.pop()
|
||||
.unwrap_or_else(|| "Mock response".to_string());
|
||||
|
||||
let tool_calls = self.tool_calls.lock().unwrap().pop().unwrap_or_default();
|
||||
|
||||
Ok(LlmResponse {
|
||||
content,
|
||||
tool_calls,
|
||||
finish_reason: Some("stop".to_string()),
|
||||
usage: Some(Usage {
|
||||
prompt_tokens: 10,
|
||||
completion_tokens: 20,
|
||||
total_tokens: 30,
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
async fn chat_stream(
|
||||
&self,
|
||||
_messages: &[Message],
|
||||
_tools: Option<&[ToolDefinition]>,
|
||||
) -> Result<Box<dyn futures::Stream<Item = Result<String>> + Send + Unpin>> {
|
||||
Err(crate::error::Error::llm("Streaming not supported in mock"))
|
||||
}
|
||||
|
||||
fn model(&self) -> &str {
|
||||
&self.model
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_message_creation() {
|
||||
let msg = Message::user("Hello");
|
||||
assert_eq!(msg.role, Role::User);
|
||||
assert_eq!(msg.content, "Hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_llm_config() {
|
||||
let config = LlmConfig::new("gpt-4").temperature(0.5).max_tokens(1000);
|
||||
|
||||
assert_eq!(config.model, "gpt-4");
|
||||
assert_eq!(config.temperature, 0.5);
|
||||
assert_eq!(config.max_tokens, Some(1000));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_mock_provider() {
|
||||
let provider = MockLlmProvider::with_response("Hello from mock!");
|
||||
|
||||
let response = provider.chat(&[Message::user("Hi")], None).await.unwrap();
|
||||
assert_eq!(response.content, "Hello from mock!");
|
||||
assert!(response.tool_calls.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_mock_provider_with_tool_calls() {
|
||||
let provider = MockLlmProvider::new();
|
||||
provider.add_response("I'll search for that");
|
||||
provider.add_tool_calls(vec![ToolCall::new(
|
||||
"call_1",
|
||||
"search",
|
||||
r#"{"query": "rust"}"#,
|
||||
)]);
|
||||
|
||||
let response = provider
|
||||
.chat(&[Message::user("Search for rust")], None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.tool_calls.len(), 1);
|
||||
assert_eq!(response.tool_calls[0].name(), "search");
|
||||
}
|
||||
}
|
||||
@@ -1,268 +0,0 @@
|
||||
//! Memory system for PraisonAI
|
||||
//!
|
||||
//! This module provides memory abstractions for agents.
|
||||
//! Memory stores conversation history and can be extended with long-term storage.
|
||||
|
||||
use async_trait::async_trait;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::config::MemoryConfig;
|
||||
use crate::error::Result;
|
||||
use crate::llm::Message;
|
||||
|
||||
/// Conversation history storage
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct ConversationHistory {
|
||||
messages: VecDeque<Message>,
|
||||
max_messages: usize,
|
||||
}
|
||||
|
||||
impl ConversationHistory {
|
||||
/// Create a new conversation history
|
||||
pub fn new(max_messages: usize) -> Self {
|
||||
Self {
|
||||
messages: VecDeque::new(),
|
||||
max_messages,
|
||||
}
|
||||
}
|
||||
|
||||
/// Add a message to the history
|
||||
pub fn add(&mut self, message: Message) {
|
||||
self.messages.push_back(message);
|
||||
|
||||
// Trim if over limit (keep system messages)
|
||||
while self.messages.len() > self.max_messages {
|
||||
// Find first non-system message to remove
|
||||
if let Some(idx) = self
|
||||
.messages
|
||||
.iter()
|
||||
.position(|m| m.role != crate::llm::Role::System)
|
||||
{
|
||||
self.messages.remove(idx);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get all messages
|
||||
pub fn messages(&self) -> Vec<Message> {
|
||||
self.messages.iter().cloned().collect()
|
||||
}
|
||||
|
||||
/// Clear the history
|
||||
pub fn clear(&mut self) {
|
||||
self.messages.clear();
|
||||
}
|
||||
|
||||
/// Get the number of messages
|
||||
pub fn len(&self) -> usize {
|
||||
self.messages.len()
|
||||
}
|
||||
|
||||
/// Check if empty
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.messages.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for memory adapters
|
||||
#[async_trait]
|
||||
pub trait MemoryAdapter: Send + Sync {
|
||||
/// Store a message in short-term memory
|
||||
async fn store_short_term(&mut self, message: Message) -> Result<()>;
|
||||
|
||||
/// Search short-term memory
|
||||
async fn search_short_term(&self, query: &str, limit: usize) -> Result<Vec<Message>>;
|
||||
|
||||
/// Get all short-term messages
|
||||
async fn get_short_term(&self) -> Result<Vec<Message>>;
|
||||
|
||||
/// Clear short-term memory
|
||||
async fn clear_short_term(&mut self) -> Result<()>;
|
||||
|
||||
/// Store in long-term memory (optional)
|
||||
async fn store_long_term(
|
||||
&mut self,
|
||||
_text: &str,
|
||||
_metadata: Option<serde_json::Value>,
|
||||
) -> Result<()> {
|
||||
Ok(()) // Default no-op
|
||||
}
|
||||
|
||||
/// Search long-term memory (optional)
|
||||
async fn search_long_term(&self, _query: &str, _limit: usize) -> Result<Vec<String>> {
|
||||
Ok(vec![]) // Default empty
|
||||
}
|
||||
}
|
||||
|
||||
/// In-memory adapter (default)
|
||||
pub struct InMemoryAdapter {
|
||||
history: ConversationHistory,
|
||||
}
|
||||
|
||||
impl InMemoryAdapter {
|
||||
/// Create a new in-memory adapter
|
||||
pub fn new(max_messages: usize) -> Self {
|
||||
Self {
|
||||
history: ConversationHistory::new(max_messages),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for InMemoryAdapter {
|
||||
fn default() -> Self {
|
||||
Self::new(100)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl MemoryAdapter for InMemoryAdapter {
|
||||
async fn store_short_term(&mut self, message: Message) -> Result<()> {
|
||||
self.history.add(message);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn search_short_term(&self, query: &str, limit: usize) -> Result<Vec<Message>> {
|
||||
// Simple substring search
|
||||
let query_lower = query.to_lowercase();
|
||||
let results: Vec<_> = self
|
||||
.history
|
||||
.messages()
|
||||
.into_iter()
|
||||
.filter(|m| m.content.to_lowercase().contains(&query_lower))
|
||||
.take(limit)
|
||||
.collect();
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
async fn get_short_term(&self) -> Result<Vec<Message>> {
|
||||
Ok(self.history.messages())
|
||||
}
|
||||
|
||||
async fn clear_short_term(&mut self) -> Result<()> {
|
||||
self.history.clear();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Memory manager for agents
|
||||
pub struct Memory {
|
||||
adapter: Box<dyn MemoryAdapter>,
|
||||
config: MemoryConfig,
|
||||
}
|
||||
|
||||
impl Memory {
|
||||
/// Create a new memory with the given adapter
|
||||
pub fn new(adapter: impl MemoryAdapter + 'static, config: MemoryConfig) -> Self {
|
||||
Self {
|
||||
adapter: Box::new(adapter),
|
||||
config,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new in-memory memory
|
||||
pub fn in_memory(config: MemoryConfig) -> Self {
|
||||
Self::new(InMemoryAdapter::new(config.max_messages), config)
|
||||
}
|
||||
|
||||
/// Create with default config
|
||||
pub fn default_memory() -> Self {
|
||||
Self::in_memory(MemoryConfig::default())
|
||||
}
|
||||
|
||||
/// Store a message
|
||||
pub async fn store(&mut self, message: Message) -> Result<()> {
|
||||
if self.config.use_short_term {
|
||||
self.adapter.store_short_term(message).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get conversation history
|
||||
pub async fn history(&self) -> Result<Vec<Message>> {
|
||||
self.adapter.get_short_term().await
|
||||
}
|
||||
|
||||
/// Search memory
|
||||
pub async fn search(&self, query: &str, limit: usize) -> Result<Vec<Message>> {
|
||||
self.adapter.search_short_term(query, limit).await
|
||||
}
|
||||
|
||||
/// Clear memory
|
||||
pub async fn clear(&mut self) -> Result<()> {
|
||||
self.adapter.clear_short_term().await
|
||||
}
|
||||
|
||||
/// Get the config
|
||||
pub fn config(&self) -> &MemoryConfig {
|
||||
&self.config
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Memory {
|
||||
fn default() -> Self {
|
||||
Self::default_memory()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Memory {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Memory")
|
||||
.field("config", &self.config)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::llm::Role;
|
||||
|
||||
#[test]
|
||||
fn test_conversation_history() {
|
||||
let mut history = ConversationHistory::new(5);
|
||||
|
||||
history.add(Message::user("Hello"));
|
||||
history.add(Message::assistant("Hi there!"));
|
||||
|
||||
assert_eq!(history.len(), 2);
|
||||
assert!(!history.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_history_trimming() {
|
||||
let mut history = ConversationHistory::new(3);
|
||||
|
||||
history.add(Message::system("You are helpful"));
|
||||
history.add(Message::user("1"));
|
||||
history.add(Message::assistant("1"));
|
||||
history.add(Message::user("2"));
|
||||
history.add(Message::assistant("2"));
|
||||
|
||||
// Should keep system message and trim oldest non-system
|
||||
assert_eq!(history.len(), 3);
|
||||
assert_eq!(history.messages()[0].role, Role::System);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_in_memory_adapter() {
|
||||
let mut adapter = InMemoryAdapter::default();
|
||||
|
||||
adapter
|
||||
.store_short_term(Message::user("Hello world"))
|
||||
.await
|
||||
.unwrap();
|
||||
adapter
|
||||
.store_short_term(Message::assistant("Hi!"))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let messages = adapter.get_short_term().await.unwrap();
|
||||
assert_eq!(messages.len(), 2);
|
||||
|
||||
let search = adapter.search_short_term("world", 10).await.unwrap();
|
||||
assert_eq!(search.len(), 1);
|
||||
}
|
||||
}
|
||||
@@ -1,248 +0,0 @@
|
||||
//! Tool system for PraisonAI
|
||||
//!
|
||||
//! This module provides the tool abstraction for agents.
|
||||
//! Tools are functions that agents can call to perform actions.
|
||||
//!
|
||||
//! # Example
|
||||
//!
|
||||
//! ```rust,ignore
|
||||
//! use praisonai_core::{tool, Tool};
|
||||
//!
|
||||
//! #[tool(description = "Search the web")]
|
||||
//! async fn search(query: String) -> String {
|
||||
//! format!("Results for: {}", query)
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
use async_trait::async_trait;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
|
||||
/// Result of a tool execution
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ToolResult {
|
||||
/// The tool name
|
||||
pub name: String,
|
||||
/// The result value (JSON)
|
||||
pub value: Value,
|
||||
/// Whether the execution was successful
|
||||
pub success: bool,
|
||||
/// Error message if failed
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
impl ToolResult {
|
||||
/// Create a successful result
|
||||
pub fn success(name: impl Into<String>, value: Value) -> Self {
|
||||
Self {
|
||||
name: name.into(),
|
||||
value,
|
||||
success: true,
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a failed result
|
||||
pub fn failure(name: impl Into<String>, error: impl Into<String>) -> Self {
|
||||
Self {
|
||||
name: name.into(),
|
||||
value: Value::Null,
|
||||
success: false,
|
||||
error: Some(error.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for tools that can be used by agents
|
||||
///
|
||||
/// This trait defines the interface for tools. Tools can be created
|
||||
/// using the `#[tool]` macro or by implementing this trait directly.
|
||||
#[async_trait]
|
||||
pub trait Tool: Send + Sync {
|
||||
/// Get the tool name
|
||||
fn name(&self) -> &str;
|
||||
|
||||
/// Get the tool description
|
||||
fn description(&self) -> &str;
|
||||
|
||||
/// Get the parameter schema as JSON Schema
|
||||
fn parameters_schema(&self) -> Value;
|
||||
|
||||
/// Execute the tool with the given arguments
|
||||
async fn execute(&self, args: Value) -> Result<Value>;
|
||||
|
||||
/// Get the tool definition for LLM function calling
|
||||
fn definition(&self) -> ToolDefinition {
|
||||
ToolDefinition {
|
||||
name: self.name().to_string(),
|
||||
description: self.description().to_string(),
|
||||
parameters: self.parameters_schema(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Tool definition for LLM function calling
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ToolDefinition {
|
||||
/// Tool name
|
||||
pub name: String,
|
||||
/// Tool description
|
||||
pub description: String,
|
||||
/// Parameter schema (JSON Schema)
|
||||
pub parameters: Value,
|
||||
}
|
||||
|
||||
/// Registry for managing tools
|
||||
#[derive(Default)]
|
||||
pub struct ToolRegistry {
|
||||
tools: HashMap<String, Arc<dyn Tool>>,
|
||||
}
|
||||
|
||||
impl ToolRegistry {
|
||||
/// Create a new empty registry
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Register a tool
|
||||
pub fn register(&mut self, tool: impl Tool + 'static) {
|
||||
let name = tool.name().to_string();
|
||||
self.tools.insert(name, Arc::new(tool));
|
||||
}
|
||||
|
||||
/// Get a tool by name
|
||||
pub fn get(&self, name: &str) -> Option<Arc<dyn Tool>> {
|
||||
self.tools.get(name).cloned()
|
||||
}
|
||||
|
||||
/// Check if a tool exists
|
||||
pub fn has(&self, name: &str) -> bool {
|
||||
self.tools.contains_key(name)
|
||||
}
|
||||
|
||||
/// List all tool names
|
||||
pub fn list(&self) -> Vec<&str> {
|
||||
self.tools.keys().map(|s| s.as_str()).collect()
|
||||
}
|
||||
|
||||
/// Get all tool definitions
|
||||
pub fn definitions(&self) -> Vec<ToolDefinition> {
|
||||
self.tools.values().map(|t| t.definition()).collect()
|
||||
}
|
||||
|
||||
/// Execute a tool by name
|
||||
pub async fn execute(&self, name: &str, args: Value) -> Result<ToolResult> {
|
||||
match self.get(name) {
|
||||
Some(tool) => match tool.execute(args).await {
|
||||
Ok(value) => Ok(ToolResult::success(name, value)),
|
||||
Err(e) => Ok(ToolResult::failure(name, e.to_string())),
|
||||
},
|
||||
None => Err(Error::tool(format!("Tool not found: {}", name))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the number of registered tools
|
||||
pub fn len(&self) -> usize {
|
||||
self.tools.len()
|
||||
}
|
||||
|
||||
/// Check if the registry is empty
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.tools.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for ToolRegistry {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("ToolRegistry")
|
||||
.field("tools", &self.list())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// A simple function-based tool
|
||||
pub struct FunctionTool<F, Fut>
|
||||
where
|
||||
F: Fn(Value) -> Fut + Send + Sync,
|
||||
Fut: std::future::Future<Output = Result<Value>> + Send,
|
||||
{
|
||||
name: String,
|
||||
description: String,
|
||||
parameters: Value,
|
||||
func: F,
|
||||
}
|
||||
|
||||
impl<F, Fut> FunctionTool<F, Fut>
|
||||
where
|
||||
F: Fn(Value) -> Fut + Send + Sync,
|
||||
Fut: std::future::Future<Output = Result<Value>> + Send,
|
||||
{
|
||||
/// Create a new function tool
|
||||
pub fn new(
|
||||
name: impl Into<String>,
|
||||
description: impl Into<String>,
|
||||
parameters: Value,
|
||||
func: F,
|
||||
) -> Self {
|
||||
Self {
|
||||
name: name.into(),
|
||||
description: description.into(),
|
||||
parameters,
|
||||
func,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<F, Fut> Tool for FunctionTool<F, Fut>
|
||||
where
|
||||
F: Fn(Value) -> Fut + Send + Sync,
|
||||
Fut: std::future::Future<Output = Result<Value>> + Send,
|
||||
{
|
||||
fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
fn description(&self) -> &str {
|
||||
&self.description
|
||||
}
|
||||
|
||||
fn parameters_schema(&self) -> Value {
|
||||
self.parameters.clone()
|
||||
}
|
||||
|
||||
async fn execute(&self, args: Value) -> Result<Value> {
|
||||
(self.func)(args).await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_tool_result_success() {
|
||||
let result = ToolResult::success("test", serde_json::json!("hello"));
|
||||
assert!(result.success);
|
||||
assert_eq!(result.name, "test");
|
||||
assert!(result.error.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tool_result_failure() {
|
||||
let result = ToolResult::failure("test", "something went wrong");
|
||||
assert!(!result.success);
|
||||
assert_eq!(result.error, Some("something went wrong".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tool_registry() {
|
||||
let registry = ToolRegistry::new();
|
||||
assert!(registry.is_empty());
|
||||
assert_eq!(registry.len(), 0);
|
||||
}
|
||||
}
|
||||
@@ -1,464 +0,0 @@
|
||||
//! Workflow system for PraisonAI
|
||||
//!
|
||||
//! This module provides multi-agent workflow patterns:
|
||||
//! - AgentTeam: Coordinates multiple agents
|
||||
//! - AgentFlow: Defines workflow execution patterns
|
||||
//! - Route, Parallel, Loop, Repeat: Workflow patterns
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::agent::Agent;
|
||||
use crate::error::{Error, Result};
|
||||
|
||||
/// Process type for workflow execution
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Process {
|
||||
/// Execute agents sequentially
|
||||
#[default]
|
||||
Sequential,
|
||||
/// Execute agents in parallel
|
||||
Parallel,
|
||||
/// Use hierarchical manager-based execution
|
||||
Hierarchical,
|
||||
}
|
||||
|
||||
/// Step result from workflow execution
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct StepResult {
|
||||
/// Agent name that produced this result
|
||||
pub agent: String,
|
||||
/// The output content
|
||||
pub output: String,
|
||||
/// Whether the step succeeded
|
||||
pub success: bool,
|
||||
/// Error message if failed
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
impl StepResult {
|
||||
/// Create a successful step result
|
||||
pub fn success(agent: impl Into<String>, output: impl Into<String>) -> Self {
|
||||
Self {
|
||||
agent: agent.into(),
|
||||
output: output.into(),
|
||||
success: true,
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a failed step result
|
||||
pub fn failure(agent: impl Into<String>, error: impl Into<String>) -> Self {
|
||||
Self {
|
||||
agent: agent.into(),
|
||||
output: String::new(),
|
||||
success: false,
|
||||
error: Some(error.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Workflow context passed between agents
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct WorkflowContext {
|
||||
/// Variables available to all agents
|
||||
pub variables: std::collections::HashMap<String, String>,
|
||||
/// Results from previous steps
|
||||
pub results: Vec<StepResult>,
|
||||
}
|
||||
|
||||
impl WorkflowContext {
|
||||
/// Create a new empty context
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Set a variable
|
||||
pub fn set(&mut self, key: impl Into<String>, value: impl Into<String>) {
|
||||
self.variables.insert(key.into(), value.into());
|
||||
}
|
||||
|
||||
/// Get a variable
|
||||
pub fn get(&self, key: &str) -> Option<&String> {
|
||||
self.variables.get(key)
|
||||
}
|
||||
|
||||
/// Add a step result
|
||||
pub fn add_result(&mut self, result: StepResult) {
|
||||
// Also store as variable for next agent
|
||||
let var_name = format!("{}_output", result.agent);
|
||||
self.variables.insert(var_name, result.output.clone());
|
||||
self.results.push(result);
|
||||
}
|
||||
|
||||
/// Get the last result
|
||||
pub fn last_result(&self) -> Option<&StepResult> {
|
||||
self.results.last()
|
||||
}
|
||||
}
|
||||
|
||||
/// Agent team for multi-agent workflows
|
||||
///
|
||||
/// Coordinates multiple agents to work together on tasks.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// let team = AgentTeam::new()
|
||||
/// .agent(researcher)
|
||||
/// .agent(writer)
|
||||
/// .process(Process::Sequential)
|
||||
/// .build();
|
||||
///
|
||||
/// let result = team.start("Research and write about AI").await?;
|
||||
/// ```
|
||||
pub struct AgentTeam {
|
||||
agents: Vec<Arc<Agent>>,
|
||||
process: Process,
|
||||
verbose: bool,
|
||||
}
|
||||
|
||||
impl AgentTeam {
|
||||
/// Check if verbose mode is enabled
|
||||
pub fn is_verbose(&self) -> bool {
|
||||
self.verbose
|
||||
}
|
||||
}
|
||||
|
||||
impl AgentTeam {
|
||||
/// Create a new agent team builder
|
||||
#[allow(clippy::new_ret_no_self)]
|
||||
pub fn new() -> AgentTeamBuilder {
|
||||
AgentTeamBuilder::new()
|
||||
}
|
||||
|
||||
/// Run the team with a task
|
||||
pub async fn start(&self, task: &str) -> Result<String> {
|
||||
match self.process {
|
||||
Process::Sequential => self.run_sequential(task).await,
|
||||
Process::Parallel => self.run_parallel(task).await,
|
||||
Process::Hierarchical => self.run_hierarchical(task).await,
|
||||
}
|
||||
}
|
||||
|
||||
/// Alias for start
|
||||
pub async fn run(&self, task: &str) -> Result<String> {
|
||||
self.start(task).await
|
||||
}
|
||||
|
||||
async fn run_sequential(&self, task: &str) -> Result<String> {
|
||||
let mut context = WorkflowContext::new();
|
||||
context.set("task", task);
|
||||
|
||||
let mut current_input = task.to_string();
|
||||
|
||||
for agent in &self.agents {
|
||||
// Build prompt with context
|
||||
let prompt = if context.results.is_empty() {
|
||||
current_input.clone()
|
||||
} else {
|
||||
let prev_output = context
|
||||
.last_result()
|
||||
.map(|r| r.output.as_str())
|
||||
.unwrap_or("");
|
||||
format!("{}\n\nPrevious output:\n{}", current_input, prev_output)
|
||||
};
|
||||
|
||||
match agent.chat(&prompt).await {
|
||||
Ok(output) => {
|
||||
context.add_result(StepResult::success(agent.name(), &output));
|
||||
current_input = output;
|
||||
}
|
||||
Err(e) => {
|
||||
context.add_result(StepResult::failure(agent.name(), e.to_string()));
|
||||
return Err(Error::workflow(format!(
|
||||
"Agent {} failed: {}",
|
||||
agent.name(),
|
||||
e
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return the last output
|
||||
context
|
||||
.last_result()
|
||||
.map(|r| r.output.clone())
|
||||
.ok_or_else(|| Error::workflow("No results from workflow"))
|
||||
}
|
||||
|
||||
async fn run_parallel(&self, task: &str) -> Result<String> {
|
||||
use futures::future::join_all;
|
||||
|
||||
let futures: Vec<_> = self
|
||||
.agents
|
||||
.iter()
|
||||
.map(|agent| {
|
||||
let agent = Arc::clone(agent);
|
||||
let task = task.to_string();
|
||||
async move {
|
||||
agent
|
||||
.chat(&task)
|
||||
.await
|
||||
.map(|output| StepResult::success(agent.name(), output))
|
||||
.unwrap_or_else(|e| StepResult::failure(agent.name(), e.to_string()))
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let results = join_all(futures).await;
|
||||
|
||||
// Combine results
|
||||
let combined: Vec<String> = results
|
||||
.iter()
|
||||
.filter(|r| r.success)
|
||||
.map(|r| format!("## {}\n{}", r.agent, r.output))
|
||||
.collect();
|
||||
|
||||
if combined.is_empty() {
|
||||
Err(Error::workflow("All agents failed"))
|
||||
} else {
|
||||
Ok(combined.join("\n\n"))
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_hierarchical(&self, task: &str) -> Result<String> {
|
||||
// For now, hierarchical is similar to sequential with validation
|
||||
// A manager agent would validate each step
|
||||
self.run_sequential(task).await
|
||||
}
|
||||
|
||||
/// Get the number of agents
|
||||
pub fn len(&self) -> usize {
|
||||
self.agents.len()
|
||||
}
|
||||
|
||||
/// Check if empty
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.agents.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for AgentTeam {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
agents: Vec::new(),
|
||||
process: Process::Sequential,
|
||||
verbose: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Builder for AgentTeam
|
||||
pub struct AgentTeamBuilder {
|
||||
agents: Vec<Arc<Agent>>,
|
||||
process: Process,
|
||||
verbose: bool,
|
||||
}
|
||||
|
||||
impl AgentTeamBuilder {
|
||||
/// Create a new builder
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
agents: Vec::new(),
|
||||
process: Process::Sequential,
|
||||
verbose: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Add an agent
|
||||
pub fn agent(mut self, agent: Agent) -> Self {
|
||||
self.agents.push(Arc::new(agent));
|
||||
self
|
||||
}
|
||||
|
||||
/// Add an agent (Arc version)
|
||||
pub fn agent_arc(mut self, agent: Arc<Agent>) -> Self {
|
||||
self.agents.push(agent);
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the process type
|
||||
pub fn process(mut self, process: Process) -> Self {
|
||||
self.process = process;
|
||||
self
|
||||
}
|
||||
|
||||
/// Enable verbose output
|
||||
pub fn verbose(mut self, enabled: bool) -> Self {
|
||||
self.verbose = enabled;
|
||||
self
|
||||
}
|
||||
|
||||
/// Build the team
|
||||
pub fn build(self) -> AgentTeam {
|
||||
AgentTeam {
|
||||
agents: self.agents,
|
||||
process: self.process,
|
||||
verbose: self.verbose,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for AgentTeamBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// AgentFlow - Workflow definition with patterns
|
||||
///
|
||||
/// Defines complex workflow patterns like Route, Parallel, Loop.
|
||||
pub struct AgentFlow {
|
||||
steps: Vec<FlowStep>,
|
||||
}
|
||||
|
||||
/// A step in a workflow
|
||||
pub enum FlowStep {
|
||||
/// Execute a single agent
|
||||
Agent(Arc<Agent>),
|
||||
/// Route to different agents based on condition
|
||||
Route(Route),
|
||||
/// Execute agents in parallel
|
||||
Parallel(Parallel),
|
||||
/// Loop over items
|
||||
Loop(Loop),
|
||||
/// Repeat a step
|
||||
Repeat(Repeat),
|
||||
}
|
||||
|
||||
/// Route pattern - conditional branching
|
||||
pub struct Route {
|
||||
/// Condition function
|
||||
pub condition: Box<dyn Fn(&str) -> bool + Send + Sync>,
|
||||
/// Agent to use if condition is true
|
||||
pub if_true: Arc<Agent>,
|
||||
/// Agent to use if condition is false
|
||||
pub if_false: Option<Arc<Agent>>,
|
||||
}
|
||||
|
||||
/// Parallel pattern - concurrent execution
|
||||
pub struct Parallel {
|
||||
/// Agents to run in parallel
|
||||
pub agents: Vec<Arc<Agent>>,
|
||||
}
|
||||
|
||||
/// Loop pattern - iterate over items
|
||||
pub struct Loop {
|
||||
/// Agent to execute for each item
|
||||
pub agent: Arc<Agent>,
|
||||
/// Items to iterate over
|
||||
pub items: Vec<String>,
|
||||
}
|
||||
|
||||
/// Repeat pattern - repeat execution
|
||||
pub struct Repeat {
|
||||
/// Agent to repeat
|
||||
pub agent: Arc<Agent>,
|
||||
/// Number of times to repeat
|
||||
pub times: usize,
|
||||
}
|
||||
|
||||
impl AgentFlow {
|
||||
/// Create a new workflow
|
||||
pub fn new() -> Self {
|
||||
Self { steps: Vec::new() }
|
||||
}
|
||||
|
||||
/// Add a step
|
||||
pub fn step(mut self, step: FlowStep) -> Self {
|
||||
self.steps.push(step);
|
||||
self
|
||||
}
|
||||
|
||||
/// Add an agent step
|
||||
pub fn agent(self, agent: Agent) -> Self {
|
||||
self.step(FlowStep::Agent(Arc::new(agent)))
|
||||
}
|
||||
|
||||
/// Execute the workflow
|
||||
pub async fn run(&self, input: &str) -> Result<String> {
|
||||
let mut current = input.to_string();
|
||||
|
||||
for step in &self.steps {
|
||||
current = match step {
|
||||
FlowStep::Agent(agent) => agent.chat(¤t).await?,
|
||||
FlowStep::Route(route) => {
|
||||
if (route.condition)(¤t) {
|
||||
route.if_true.chat(¤t).await?
|
||||
} else if let Some(agent) = &route.if_false {
|
||||
agent.chat(¤t).await?
|
||||
} else {
|
||||
current
|
||||
}
|
||||
}
|
||||
FlowStep::Parallel(parallel) => {
|
||||
use futures::future::join_all;
|
||||
|
||||
let futures: Vec<_> =
|
||||
parallel.agents.iter().map(|a| a.chat(¤t)).collect();
|
||||
|
||||
let results = join_all(futures).await;
|
||||
let outputs: Vec<String> = results.into_iter().filter_map(|r| r.ok()).collect();
|
||||
|
||||
outputs.join("\n\n")
|
||||
}
|
||||
FlowStep::Loop(loop_step) => {
|
||||
let mut outputs = Vec::new();
|
||||
for item in &loop_step.items {
|
||||
let prompt = format!("{}\n\nItem: {}", current, item);
|
||||
outputs.push(loop_step.agent.chat(&prompt).await?);
|
||||
}
|
||||
outputs.join("\n\n")
|
||||
}
|
||||
FlowStep::Repeat(repeat) => {
|
||||
let mut output = current.clone();
|
||||
for _ in 0..repeat.times {
|
||||
output = repeat.agent.chat(&output).await?;
|
||||
}
|
||||
output
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Ok(current)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for AgentFlow {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_workflow_context() {
|
||||
let mut ctx = WorkflowContext::new();
|
||||
ctx.set("key", "value");
|
||||
assert_eq!(ctx.get("key"), Some(&"value".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_step_result() {
|
||||
let success = StepResult::success("agent1", "output");
|
||||
assert!(success.success);
|
||||
|
||||
let failure = StepResult::failure("agent1", "error");
|
||||
assert!(!failure.success);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_agent_team_builder() {
|
||||
let team = AgentTeam::new()
|
||||
.process(Process::Parallel)
|
||||
.verbose(true)
|
||||
.build();
|
||||
|
||||
assert!(team.is_empty());
|
||||
}
|
||||
}
|
||||
@@ -1,955 +0,0 @@
|
||||
# AGENTS.md - PraisonAI TypeScript SDK Comprehensive Guide
|
||||
|
||||
> **For AI Agents and Developers**: This document provides the complete context needed to work with the PraisonAI TypeScript SDK, including design principles, architecture, repository structure, and implementation guidelines.
|
||||
|
||||
---
|
||||
|
||||
## 1. What is PraisonAI TypeScript?
|
||||
|
||||
PraisonAI TypeScript is a **high-performance, agentic AI framework** for Node.js/JavaScript, designed for building production-ready AI agents and multi-agent workflows.
|
||||
|
||||
### Core Philosophy
|
||||
|
||||
```
|
||||
Simpler than competitors • More extensible • Faster • Agent-centric
|
||||
```
|
||||
|
||||
| Principle | Description |
|
||||
|-----------|-------------|
|
||||
| **Agent-Centric** | Every design decision centers on Agents, workflows, sessions, tools, and memory |
|
||||
| **TypeScript-First** | Full type safety with excellent IntelliSense support |
|
||||
| **AI SDK Integration** | First-class support for Vercel AI SDK providers |
|
||||
| **Minimal API** | Fewer parameters, sensible defaults, explicit overrides |
|
||||
| **Performance-First** | Lazy loading, optional dependencies, no hot-path regressions |
|
||||
| **Multi-Agent Safe** | Concurrent execution, no shared mutable state issues |
|
||||
| **TDD Mandatory** | Tests first; failing tests prove gaps; passing tests prove fixes |
|
||||
| **Production-Ready** | Safe by default, async-safe, observable |
|
||||
|
||||
---
|
||||
|
||||
## 2. Repository Structure
|
||||
|
||||
### 2.1 Canonical Paths
|
||||
|
||||
```
|
||||
/Users/praison/praisonai-package/
|
||||
├── src/
|
||||
│ ├── praisonai-agents/ # Python SDK (praisonaiagents)
|
||||
│ │ ├── praisonaiagents/ # Python package
|
||||
│ │ ├── tests/ # Unit & integration tests
|
||||
│ │ └── examples/ # Package-level examples
|
||||
│ │
|
||||
│ ├── praisonai-ts/ # TypeScript SDK (this package)
|
||||
│ │ ├── src/ # Source code
|
||||
│ │ ├── tests/ # Unit & integration tests
|
||||
│ │ ├── examples/ # TypeScript examples
|
||||
│ │ └── package.json # Package config
|
||||
│ │
|
||||
│ └── praisonai/ # Wrapper (praisonai CLI)
|
||||
│
|
||||
├── examples/ # Main examples directory
|
||||
│ ├── python/ # Python examples
|
||||
│ ├── yaml/ # YAML configuration examples
|
||||
│ └── js/ # JavaScript/TypeScript examples
|
||||
│
|
||||
/Users/praison/PraisonAIDocs/ # Documentation (Mintlify)
|
||||
```
|
||||
|
||||
### 2.2 TypeScript SDK Source Structure
|
||||
|
||||
```
|
||||
src/
|
||||
├── agent/ # Core Agent implementations (20 files)
|
||||
│ ├── simple.ts # Main Agent class
|
||||
│ ├── audio.ts # AudioAgent
|
||||
│ ├── video.ts # VideoAgent
|
||||
│ ├── vision.ts # VisionAgent
|
||||
│ ├── code.ts # CodeAgent
|
||||
│ ├── research.ts # DeepResearchAgent
|
||||
│ ├── handoff.ts # Agent handoff system
|
||||
│ └── router.ts # RouterAgent
|
||||
│
|
||||
├── tools/ # Tool SDK (27 files)
|
||||
│ ├── decorator.ts # Tool function & FunctionTool class
|
||||
│ ├── registry.ts # Tool registry with middleware
|
||||
│ ├── tools.ts # Tools facade (40+ built-ins)
|
||||
│ └── builtins/ # Built-in tools (13 modules)
|
||||
│
|
||||
├── workflows/ # Workflow engine (4 files)
|
||||
│ ├── index.ts # Workflow, Task, Pipeline
|
||||
│ ├── loop.ts # Loop pattern
|
||||
│ └── repeat.ts # Repeat pattern
|
||||
│
|
||||
├── memory/ # Memory systems (7 files)
|
||||
│ ├── memory.ts # Base Memory class
|
||||
│ ├── auto-memory.ts # AutoMemory with auto-detection
|
||||
│ ├── file-memory.ts # FileMemory for persistence
|
||||
│ └── rules-manager.ts # Rules management
|
||||
│
|
||||
├── hooks/ # Hooks & middleware (4 files)
|
||||
│ ├── manager.ts # HookManager
|
||||
│ ├── callbacks.ts # Callbacks system
|
||||
│ └── workflow-hooks.ts # Workflow-specific hooks
|
||||
│
|
||||
├── llm/ # LLM providers (18 files)
|
||||
│ ├── openai.ts # OpenAI client
|
||||
│ ├── providers/ # Provider implementations
|
||||
│ │ ├── ai-sdk/ # Vercel AI SDK integration
|
||||
│ │ ├── anthropic.ts
|
||||
│ │ ├── google.ts
|
||||
│ │ └── openai.ts
|
||||
│ └── backend-resolver.ts # Automatic backend selection
|
||||
│
|
||||
├── knowledge/ # Knowledge & RAG (10 files)
|
||||
│ ├── knowledge.ts # Knowledge base
|
||||
│ ├── rag.ts # RAG implementation
|
||||
│ ├── graph-rag.ts # Graph-based RAG
|
||||
│ └── query-engine.ts # Query processing
|
||||
│
|
||||
├── mcp/ # Model Context Protocol (5 files)
|
||||
│ ├── index.ts # MCP client
|
||||
│ ├── server.ts # MCP server
|
||||
│ ├── session.ts # Session management
|
||||
│ └── transports.ts # Transport layer (stdio, SSE, WebSocket)
|
||||
│
|
||||
├── observability/ # Observability adapters (21 files)
|
||||
│ ├── adapters/ # External integrations
|
||||
│ │ └── external/ # Langfuse, LangSmith, Helicone, etc.
|
||||
│ └── types.ts # Trace types
|
||||
│
|
||||
├── eval/ # Evaluation framework (4 files)
|
||||
│ ├── judge.ts # LLM-based evaluation
|
||||
│ ├── base.ts # Evaluator base
|
||||
│ └── results.ts # Results handling
|
||||
│
|
||||
├── guardrails/ # Input/output guardrails (2 files)
|
||||
├── session/ # Session management (5 files)
|
||||
├── context/ # Context optimization (5 files)
|
||||
├── telemetry/ # Telemetry collection (3 files)
|
||||
├── integrations/ # External integrations (15 files)
|
||||
│ ├── computer-use.ts # Computer Use
|
||||
│ ├── postgres.ts # PostgreSQL
|
||||
│ ├── slack.ts # Slack
|
||||
│ └── voice/ # Voice integrations
|
||||
│
|
||||
├── ai/ # AI SDK helpers (19 files)
|
||||
│ ├── generate-text.ts
|
||||
│ ├── generate-object.ts
|
||||
│ ├── generate-image.ts
|
||||
│ └── speech.ts
|
||||
│
|
||||
├── cli/ # CLI (78 files)
|
||||
│ ├── commands/ # 51 command modules
|
||||
│ └── features/ # 16 feature modules
|
||||
│
|
||||
└── index.ts # Main exports (938 lines)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. Core Classes & Types
|
||||
|
||||
### 3.1 Agent
|
||||
|
||||
The primary class for creating AI agents:
|
||||
|
||||
```typescript
|
||||
import { Agent } from 'praisonai';
|
||||
|
||||
interface SimpleAgentConfig {
|
||||
name?: string; // Agent identifier
|
||||
instructions: string; // System prompt/instructions
|
||||
llm?: string; // Model: 'gpt-4o-mini', 'claude-3-opus', etc.
|
||||
verbose?: boolean; // Enable logging
|
||||
stream?: boolean; // Enable streaming
|
||||
tools?: any[]; // Tool functions or FunctionTool instances
|
||||
db?: DbAdapter; // Database adapter for persistence
|
||||
sessionId?: string; // Session identifier
|
||||
memory?: boolean; // Enable memory
|
||||
cache?: boolean; // Enable response caching
|
||||
telemetry?: boolean; // Enable telemetry
|
||||
role?: string; // Agent role
|
||||
goal?: string; // Agent goal
|
||||
backstory?: string; // Agent backstory
|
||||
}
|
||||
```
|
||||
|
||||
### 3.2 AgentTeam (Agents)
|
||||
|
||||
Multi-agent orchestration:
|
||||
|
||||
```typescript
|
||||
import { AgentTeam, Agent } from 'praisonai';
|
||||
|
||||
interface AgentTeamConfig {
|
||||
agents?: Agent[]; // List of agents
|
||||
tasks?: (Task | string)[]; // Tasks to execute
|
||||
verbose?: boolean; // Enable logging
|
||||
process?: 'sequential' | 'parallel' | 'hierarchical';
|
||||
manager_llm?: string; // Manager LLM for hierarchical
|
||||
}
|
||||
|
||||
// Aliases for Python parity
|
||||
export type Agents = AgentTeam;
|
||||
export type PraisonAIAgents = AgentTeam;
|
||||
```
|
||||
|
||||
### 3.3 Task
|
||||
|
||||
Task definition for workflows:
|
||||
|
||||
```typescript
|
||||
import { Task, Agent } from 'praisonai';
|
||||
|
||||
interface TaskConfig {
|
||||
name: string;
|
||||
description: string;
|
||||
expected_output?: string;
|
||||
agent?: Agent;
|
||||
dependencies?: Task[];
|
||||
}
|
||||
```
|
||||
|
||||
### 3.4 FunctionTool
|
||||
|
||||
Type-safe tool creation:
|
||||
|
||||
```typescript
|
||||
import { tool, FunctionTool } from 'praisonai';
|
||||
|
||||
const myTool = tool({
|
||||
name: 'search',
|
||||
description: 'Search the web',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
query: { type: 'string', description: 'Search query' }
|
||||
},
|
||||
required: ['query']
|
||||
},
|
||||
async execute({ query }) {
|
||||
return `Results for: ${query}`;
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. Core Engineering Principles
|
||||
|
||||
### 4.1 TypeScript-First Design
|
||||
|
||||
```typescript
|
||||
// ✅ CORRECT: Full type safety
|
||||
interface ToolConfig<TParams = any, TResult = any> {
|
||||
name: string;
|
||||
description?: string;
|
||||
parameters?: ToolParameters | any;
|
||||
execute: (params: TParams, context?: ToolContext) => Promise<TResult> | TResult;
|
||||
}
|
||||
|
||||
// ❌ WRONG: Using any without generics
|
||||
function createTool(config: any): any;
|
||||
```
|
||||
|
||||
### 4.2 AI SDK Integration
|
||||
|
||||
PraisonAI TypeScript provides first-class support for Vercel AI SDK:
|
||||
|
||||
```typescript
|
||||
// Automatic backend resolution
|
||||
const agent = new Agent({
|
||||
instructions: "Be helpful",
|
||||
llm: "gpt-4o-mini" // Resolves to AI SDK or native OpenAI
|
||||
});
|
||||
|
||||
// Get backend info
|
||||
const backend = await agent.getBackend();
|
||||
const source = agent.getBackendSource(); // 'ai-sdk' | 'native' | 'custom'
|
||||
```
|
||||
|
||||
### 4.3 Lazy Import Pattern
|
||||
|
||||
```typescript
|
||||
// ✅ CORRECT: Lazy import for optional dependencies
|
||||
async function useFirecrawl() {
|
||||
const { Firecrawl } = await import('@mendable/firecrawl-js');
|
||||
return new Firecrawl();
|
||||
}
|
||||
|
||||
// ❌ WRONG: Top-level import of optional dependency
|
||||
import { Firecrawl } from '@mendable/firecrawl-js';
|
||||
```
|
||||
|
||||
### 4.4 Async/Await Pattern
|
||||
|
||||
```typescript
|
||||
// Entry points: Always async
|
||||
async start(prompt: string): Promise<string>;
|
||||
async chat(prompt: string): Promise<string>;
|
||||
|
||||
// Internal: Prefer async
|
||||
async processToolCalls(toolCalls: any[]): Promise<ToolResult[]>;
|
||||
|
||||
// Utilities: Support both
|
||||
function parseArgs(args: string[]): ParsedArgs;
|
||||
async function loadConfig(path: string): Promise<Config>;
|
||||
```
|
||||
|
||||
### 4.5 Error Handling
|
||||
|
||||
```typescript
|
||||
// Custom error classes
|
||||
export class MissingDependencyError extends Error {
|
||||
constructor(packageName: string, installCommand?: string) {
|
||||
super(`Missing dependency: ${packageName}. Install with: ${installCommand}`);
|
||||
this.name = 'MissingDependencyError';
|
||||
}
|
||||
}
|
||||
|
||||
export class MissingEnvVarError extends Error {
|
||||
constructor(envVar: string) {
|
||||
super(`Missing environment variable: ${envVar}`);
|
||||
this.name = 'MissingEnvVarError';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. Built-in Tools
|
||||
|
||||
### 5.1 Tools Facade
|
||||
|
||||
Access all built-in tools through the `tools` object:
|
||||
|
||||
```typescript
|
||||
import { Agent, tools } from 'praisonai';
|
||||
|
||||
const agent = new Agent({
|
||||
instructions: "Research assistant",
|
||||
tools: [
|
||||
tools.tavily(), // Web search
|
||||
tools.codeExecution(), // Code sandbox
|
||||
tools.firecrawl(), // Web scraping
|
||||
tools.perplexity(), // Perplexity search
|
||||
]
|
||||
});
|
||||
```
|
||||
|
||||
### 5.2 Available Built-in Tools
|
||||
|
||||
| Category | Tools |
|
||||
|----------|-------|
|
||||
| **Search** | `tavily()`, `tavilySearch()`, `tavilyExtract()`, `tavilyCrawl()` |
|
||||
| **Search** | `exa()`, `exaSearch()`, `perplexity()`, `perplexitySearch()` |
|
||||
| **Search** | `parallel()`, `parallelSearch()` (multi-provider) |
|
||||
| **Scraping** | `firecrawl()`, `firecrawlScrape()`, `firecrawlCrawl()` |
|
||||
| **Domain Search** | `valyuWebSearch()`, `valyuFinanceSearch()`, `valyuPaperSearch()` |
|
||||
| **Domain Search** | `valyuBioSearch()`, `valyuPatentSearch()`, `valyuSecSearch()` |
|
||||
| **Security** | `guard()`, `redact()`, `verify()` (superagent) |
|
||||
| **Code** | `codeExecution()`, `executeCode()`, `codeMode()` |
|
||||
| **Bedrock** | `bedrockCodeInterpreter()`, `bedrockBrowserNavigate()` |
|
||||
| **Vector** | `airweave()`, `airweaveSearch()` |
|
||||
| **Custom** | `custom()` for custom tool creation |
|
||||
|
||||
---
|
||||
|
||||
## 6. LLM Providers
|
||||
|
||||
### 6.1 Supported Providers
|
||||
|
||||
| Provider | Config Key | Models |
|
||||
|----------|------------|--------|
|
||||
| **OpenAI** | `openai/gpt-4o-mini` | gpt-4o, gpt-4o-mini, gpt-4-turbo |
|
||||
| **Anthropic** | `anthropic/claude-3-opus` | claude-3-opus, claude-3-sonnet, claude-3-haiku |
|
||||
| **Google** | `google/gemini-1.5-pro` | gemini-1.5-pro, gemini-1.5-flash |
|
||||
| **AI SDK** | via peer dependency | All AI SDK providers |
|
||||
|
||||
### 6.2 Backend Resolution
|
||||
|
||||
```typescript
|
||||
// Automatic resolution (AI SDK preferred)
|
||||
const agent = new Agent({ llm: "gpt-4o-mini" });
|
||||
|
||||
// Force native provider
|
||||
const agent = new Agent({ llm: "openai/gpt-4o-mini" });
|
||||
|
||||
// Check backend
|
||||
const backend = await agent.getBackend();
|
||||
console.log(agent.getBackendSource()); // 'ai-sdk' | 'native'
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7. Memory System
|
||||
|
||||
### 7.1 Memory Types
|
||||
|
||||
```typescript
|
||||
import { Memory, FileMemory, AutoMemory } from 'praisonai';
|
||||
|
||||
// Basic memory
|
||||
const memory = new Memory({ sessionId: "user-123" });
|
||||
|
||||
// File-based persistence
|
||||
const fileMemory = new FileMemory({
|
||||
path: './data/memory.json',
|
||||
sessionId: "user-123"
|
||||
});
|
||||
|
||||
// Auto-detecting memory (short-term + long-term)
|
||||
const autoMemory = new AutoMemory({
|
||||
enableShortTerm: true,
|
||||
enableLongTerm: true,
|
||||
provider: 'file' // or 'sqlite', 'chroma'
|
||||
});
|
||||
```
|
||||
|
||||
### 7.2 Memory with Agent
|
||||
|
||||
```typescript
|
||||
import { Agent, db } from 'praisonai';
|
||||
|
||||
const agent = new Agent({
|
||||
instructions: "You are a helpful assistant",
|
||||
db: db("sqlite:./data.db"),
|
||||
sessionId: "conversation-123",
|
||||
memory: true
|
||||
});
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 8. Hooks & Callbacks
|
||||
|
||||
### 8.1 Agent Callbacks
|
||||
|
||||
```typescript
|
||||
import { Agent, AgentCallbacks } from 'praisonai';
|
||||
|
||||
const callbacks: AgentCallbacks = {
|
||||
onStart: (agent, input) => console.log('Starting:', input),
|
||||
onEnd: (agent, output) => console.log('Result:', output),
|
||||
onToolCall: (agent, tool, args) => console.log('Calling:', tool),
|
||||
onError: (agent, error) => console.error('Error:', error)
|
||||
};
|
||||
|
||||
const agent = new Agent({
|
||||
instructions: "Be helpful",
|
||||
callbacks
|
||||
});
|
||||
```
|
||||
|
||||
### 8.2 Workflow Hooks
|
||||
|
||||
```typescript
|
||||
import { WorkflowHooks, createLoggingWorkflowHooks } from 'praisonai';
|
||||
|
||||
const hooks: WorkflowHooks = {
|
||||
onStepStart: (step, context) => { /* ... */ },
|
||||
onStepEnd: (step, result, context) => { /* ... */ },
|
||||
onWorkflowStart: (workflow, context) => { /* ... */ },
|
||||
onWorkflowEnd: (workflow, results, context) => { /* ... */ }
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 9. Workflows
|
||||
|
||||
### 9.1 Task-Based Workflows
|
||||
|
||||
```typescript
|
||||
import { Task, Workflow, createContext } from 'praisonai';
|
||||
|
||||
const researchTask = new Task({
|
||||
name: 'research',
|
||||
async execute(input, context) {
|
||||
return await performResearch(input);
|
||||
}
|
||||
});
|
||||
|
||||
const summaryTask = new Task({
|
||||
name: 'summarize',
|
||||
async execute(input, context) {
|
||||
const research = context.get('research');
|
||||
return summarize(research);
|
||||
}
|
||||
});
|
||||
|
||||
const workflow = new Workflow([researchTask, summaryTask]);
|
||||
const results = await workflow.run(createContext('workflow-1'));
|
||||
```
|
||||
|
||||
### 9.2 Loop Pattern
|
||||
|
||||
```typescript
|
||||
import { Loop, loop } from 'praisonai';
|
||||
|
||||
const iterativeLoop = new Loop({
|
||||
maxIterations: 5,
|
||||
async condition(context) {
|
||||
return context.iteration < 5;
|
||||
},
|
||||
async body(context) {
|
||||
return await processIteration(context);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### 9.3 Repeat Pattern
|
||||
|
||||
```typescript
|
||||
import { Repeat, repeat } from 'praisonai';
|
||||
|
||||
const repeater = new Repeat({
|
||||
times: 3,
|
||||
async action(context) {
|
||||
return await performAction(context);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 10. MCP (Model Context Protocol)
|
||||
|
||||
### 10.1 MCP Client
|
||||
|
||||
```typescript
|
||||
import { MCPClient, createMCPClient } from 'praisonai';
|
||||
|
||||
const client = await createMCPClient({
|
||||
transport: 'stdio',
|
||||
command: 'npx',
|
||||
args: ['-y', '@modelcontextprotocol/server-filesystem', '/path']
|
||||
});
|
||||
|
||||
const tools = await client.listTools();
|
||||
const result = await client.callTool('read_file', { path: '/file.txt' });
|
||||
```
|
||||
|
||||
### 10.2 MCP Server
|
||||
|
||||
```typescript
|
||||
import { MCPServer, createMCPServer } from 'praisonai';
|
||||
|
||||
const server = createMCPServer({
|
||||
name: 'my-server',
|
||||
version: '1.0.0',
|
||||
tools: [myTool1, myTool2]
|
||||
});
|
||||
|
||||
await server.start({ transport: 'stdio' });
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 11. Observability
|
||||
|
||||
### 11.1 External Adapters
|
||||
|
||||
```typescript
|
||||
import { createLangfuseAdapter, createLangSmithAdapter } from 'praisonai';
|
||||
|
||||
// Langfuse
|
||||
const langfuseAdapter = createLangfuseAdapter({
|
||||
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
|
||||
secretKey: process.env.LANGFUSE_SECRET_KEY
|
||||
});
|
||||
|
||||
// LangSmith
|
||||
const langsmithAdapter = createLangSmithAdapter({
|
||||
apiKey: process.env.LANGSMITH_API_KEY
|
||||
});
|
||||
```
|
||||
|
||||
### 11.2 Supported Platforms
|
||||
|
||||
- Langfuse, LangSmith, LangWatch
|
||||
- Arize, Axiom, Braintrust
|
||||
- Helicone, Laminar, Maxim
|
||||
- Patronus, Scorecard, SignOz
|
||||
- Traceloop, Weave
|
||||
|
||||
---
|
||||
|
||||
## 12. Guardrails
|
||||
|
||||
```typescript
|
||||
import { createGuardrail, GuardrailAction } from 'praisonai';
|
||||
|
||||
const contentGuardrail = createGuardrail({
|
||||
name: 'content-filter',
|
||||
async check(input) {
|
||||
if (containsHarmfulContent(input)) {
|
||||
return { action: GuardrailAction.BLOCK, reason: 'Harmful content' };
|
||||
}
|
||||
return { action: GuardrailAction.ALLOW };
|
||||
}
|
||||
});
|
||||
|
||||
const agent = new Agent({
|
||||
instructions: "Be helpful",
|
||||
guardrails: [contentGuardrail]
|
||||
});
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 13. Specialized Agents
|
||||
|
||||
### 13.1 Available Agent Types
|
||||
|
||||
| Agent | Purpose |
|
||||
|-------|---------|
|
||||
| `Agent` | Base agent for general tasks |
|
||||
| `AudioAgent` | Speech synthesis and transcription |
|
||||
| `VideoAgent` | Video generation and analysis |
|
||||
| `VisionAgent` | Image analysis and understanding |
|
||||
| `CodeAgent` | Code generation and execution |
|
||||
| `DeepResearchAgent` | Multi-step research workflows |
|
||||
| `RouterAgent` | Route requests to appropriate agents |
|
||||
| `ContextAgent` | Context-aware responses |
|
||||
|
||||
### 13.2 Example: AudioAgent
|
||||
|
||||
```typescript
|
||||
import { AudioAgent, createAudioAgent } from 'praisonai';
|
||||
|
||||
const audioAgent = createAudioAgent({
|
||||
voiceModel: 'alloy',
|
||||
transcriptionModel: 'whisper-1'
|
||||
});
|
||||
|
||||
// Text to speech
|
||||
const audio = await audioAgent.speak("Hello, world!");
|
||||
|
||||
// Speech to text
|
||||
const text = await audioAgent.transcribe(audioBuffer);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 14. CLI Commands
|
||||
|
||||
### 14.1 Basic Usage
|
||||
|
||||
```bash
|
||||
# Install globally
|
||||
npm install -g praisonai
|
||||
|
||||
# Run agent
|
||||
praisonai-ts agent run --prompt "Hello"
|
||||
|
||||
# Multi-agent
|
||||
praisonai-ts agents run --config agents.yaml
|
||||
|
||||
# Start server
|
||||
praisonai-ts serve --port 8000
|
||||
```
|
||||
|
||||
### 14.2 Command Categories
|
||||
|
||||
| Category | Commands |
|
||||
|----------|----------|
|
||||
| **Agent** | `agent run`, `agent chat` |
|
||||
| **Multi-Agent** | `agents run`, `agents config` |
|
||||
| **Tools** | `tools list`, `tools install` |
|
||||
| **Memory** | `memory list`, `memory clear` |
|
||||
| **MCP** | `mcp connect`, `mcp list` |
|
||||
| **Server** | `serve`, `gateway` |
|
||||
|
||||
---
|
||||
|
||||
## 15. Testing
|
||||
|
||||
### 15.1 Test Structure
|
||||
|
||||
```
|
||||
tests/
|
||||
├── unit/ # Unit tests
|
||||
├── integration/ # Integration tests
|
||||
└── development/ # Development test scripts
|
||||
```
|
||||
|
||||
### 15.2 Run Tests
|
||||
|
||||
```bash
|
||||
# All tests
|
||||
npm test
|
||||
|
||||
# With coverage
|
||||
npm test -- --coverage
|
||||
|
||||
# Specific test file
|
||||
npm test -- tests/unit/agent.test.ts
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 16. Dependencies
|
||||
|
||||
### 16.1 Required Dependencies
|
||||
|
||||
```json
|
||||
{
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/sdk": "^1.12.1",
|
||||
"axios": "^1.7.9",
|
||||
"dotenv": "^16.4.7",
|
||||
"openai": "^4.81.0"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 16.2 Peer Dependencies (Optional)
|
||||
|
||||
```json
|
||||
{
|
||||
"peerDependencies": {
|
||||
"@ai-sdk/anthropic": ">=1.0.0",
|
||||
"@ai-sdk/google": ">=1.0.0",
|
||||
"@ai-sdk/openai": ">=1.0.0",
|
||||
"ai": ">=4.0.0"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 16.3 Optional Dependencies
|
||||
|
||||
```json
|
||||
{
|
||||
"optionalDependencies": {
|
||||
"@mendable/firecrawl-js": ">=1.0.0",
|
||||
"better-sqlite3": "^12.6.2",
|
||||
"bedrock-agentcore": ">=0.1.0"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 17. Quick Reference
|
||||
|
||||
### 17.1 Core Imports
|
||||
|
||||
```typescript
|
||||
// Most common
|
||||
import { Agent, AgentTeam, Task, tool, tools } from 'praisonai';
|
||||
|
||||
// Memory
|
||||
import { Memory, FileMemory, AutoMemory, db } from 'praisonai';
|
||||
|
||||
// Hooks
|
||||
import { HookManager, AgentCallbacks, WorkflowHooks } from 'praisonai';
|
||||
|
||||
// MCP
|
||||
import { MCPClient, MCPServer, createMCPClient } from 'praisonai';
|
||||
|
||||
// Observability
|
||||
import { createLangfuseAdapter, createLangSmithAdapter } from 'praisonai';
|
||||
|
||||
// Specialized agents
|
||||
import { AudioAgent, VideoAgent, VisionAgent, CodeAgent } from 'praisonai';
|
||||
|
||||
// Workflows
|
||||
import { Workflow, Loop, Repeat, createContext } from 'praisonai';
|
||||
|
||||
// RAG
|
||||
import { RAG, createRAG, Knowledge } from 'praisonai';
|
||||
|
||||
// Eval
|
||||
import { Evaluator, EvalSuite, createEvaluator } from 'praisonai';
|
||||
```
|
||||
|
||||
### 17.2 File Locations
|
||||
|
||||
| What | Where |
|
||||
|------|-------|
|
||||
| Agent class | `src/agent/simple.ts` |
|
||||
| Tool decorator | `src/tools/decorator.ts` |
|
||||
| Tool registry | `src/tools/registry/index.ts` |
|
||||
| Built-in tools | `src/tools/builtins/` |
|
||||
| Hook system | `src/hooks/` |
|
||||
| Memory | `src/memory/` |
|
||||
| Workflows | `src/workflows/` |
|
||||
| MCP | `src/mcp/` |
|
||||
| LLM providers | `src/llm/providers/` |
|
||||
| Observability | `src/observability/` |
|
||||
|
||||
---
|
||||
|
||||
## 18. Naming Conventions
|
||||
|
||||
```typescript
|
||||
// Registration/Mutation
|
||||
registerTool() // Register tool to registry
|
||||
addHook() // Add hook to manager
|
||||
|
||||
// Retrieval
|
||||
getTool() // Single tool by name
|
||||
getRegistry() // Get global registry
|
||||
listTools() // All tools
|
||||
|
||||
// Creation
|
||||
createAgent() // Factory function
|
||||
createMCPClient() // Factory function
|
||||
|
||||
// Types & Interfaces
|
||||
interface AgentConfig { } // Configuration
|
||||
interface AgentCallbacks { } // Callbacks
|
||||
type AgentResult = { } // Result type
|
||||
|
||||
// Classes
|
||||
class Agent { } // Main class
|
||||
class FunctionTool { } // Tool class
|
||||
class ToolRegistry { } // Registry class
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 19. CLI Parity Requirement
|
||||
|
||||
Every feature implemented must have a corresponding CLI representation:
|
||||
|
||||
```typescript
|
||||
// Pattern: If you add an API, add a CLI command
|
||||
// Feature: Agent memory
|
||||
// API: await agent.clearMemory()
|
||||
// CLI: praisonai-ts memory clear --session <id>
|
||||
```
|
||||
|
||||
| Feature | API | CLI Equivalent |
|
||||
|---------|-----|----------------|
|
||||
| Chat | `agent.chat(prompt)` | `praisonai-ts agent chat` |
|
||||
| Multi-agent | `team.start(task)` | `praisonai-ts agents run` |
|
||||
| Server | `createServer()` | `praisonai-ts serve` |
|
||||
|
||||
---
|
||||
|
||||
## 20. Verification Checklist
|
||||
|
||||
For every feature/change, verify:
|
||||
|
||||
- [ ] **Tests pass**: `npm test`
|
||||
- [ ] **TypeScript compiles**: `npm run build`
|
||||
- [ ] **Lint clean**: `npm run lint`
|
||||
- [ ] **No heavy imports at top-level**: Check lazy loading
|
||||
- [ ] **Async-safe**: Proper Promise handling
|
||||
- [ ] **Multi-agent safe**: No shared mutable state
|
||||
- [ ] **CLI works**: Command appears in `--help`
|
||||
- [ ] **Docs updated**: JSDoc with examples
|
||||
|
||||
---
|
||||
|
||||
## 21. Documentation Standards
|
||||
|
||||
### 21.1 JSDoc Comments
|
||||
|
||||
All public APIs must have JSDoc with examples:
|
||||
|
||||
```typescript
|
||||
/**
|
||||
* Creates an agent with the given configuration.
|
||||
*
|
||||
* @param config - Agent configuration
|
||||
* @returns Configured agent instance
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const agent = new Agent({
|
||||
* instructions: "Be helpful"
|
||||
* });
|
||||
* const response = await agent.chat("Hello");
|
||||
* ```
|
||||
*/
|
||||
export class Agent { }
|
||||
```
|
||||
|
||||
### 21.2 Mermaid Diagrams
|
||||
|
||||
Use two-color scheme for architecture diagrams:
|
||||
- **Dark Red (#8B0000)**: Agents, inputs, outputs
|
||||
- **Teal (#189AB4)**: Tools, utilities
|
||||
|
||||
```mermaid
|
||||
graph LR
|
||||
A[Agent]:::agent --> B[Tool]:::tool
|
||||
B --> C[Result]:::agent
|
||||
classDef agent fill:#8B0000,color:#fff
|
||||
classDef tool fill:#189AB4,color:#fff
|
||||
```
|
||||
|
||||
### 21.3 Beginner-Friendly
|
||||
|
||||
Documentation should make users feel: *"With just a few lines of code, I can do this!"*
|
||||
|
||||
```typescript
|
||||
// 3 lines to chat with AI
|
||||
const agent = new Agent({ instructions: "Be helpful" });
|
||||
const response = await agent.chat("Hello");
|
||||
console.log(response);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 22. Free Core / Paid Upgrade Path
|
||||
|
||||
Design features so that:
|
||||
|
||||
| Aspect | Guideline |
|
||||
|--------|------------|
|
||||
| **Core remains free** | Essential agent functionality always open source |
|
||||
| **Clear upgrade path** | Support, cloud, managed services as paid options |
|
||||
| **Reduces friction** | Features reduce production risk, not just add functionality |
|
||||
| **Safe by default** | Simple to adopt, hard to misuse |
|
||||
|
||||
---
|
||||
|
||||
## 23. Implementation Checklist
|
||||
|
||||
For every feature/change:
|
||||
|
||||
- [ ] **TypeScript-first**: Full type definitions with generics
|
||||
- [ ] **No new deps**: Use optional dependencies only
|
||||
- [ ] **Lazy imports**: Heavy deps imported inside functions
|
||||
- [ ] **Naming**: Follow conventions (create*, get*, XConfig)
|
||||
- [ ] **Tests**: Write unit tests in `tests/`
|
||||
- [ ] **Docs**: Update this file and Mintlify docs
|
||||
- [ ] **Examples**: Add to `examples/` directory
|
||||
- [ ] **Async-safe**: Support async/await patterns
|
||||
- [ ] **Error handling**: Use custom error classes
|
||||
|
||||
---
|
||||
|
||||
## 24. Design Goals
|
||||
|
||||
> Make PraisonAI the **best TypeScript agent framework in the world**
|
||||
|
||||
1. **Simpler** than competitors (fewer concepts, cleaner API)
|
||||
2. **More extensible** (registry-driven, plugin-ready)
|
||||
3. **Faster** (lazy loading, AI SDK integration)
|
||||
4. **Type-safe** (full TypeScript support)
|
||||
5. **Production-ready** (observability, guardrails, MCP)
|
||||
|
||||
---
|
||||
|
||||
## 25. Python Parity
|
||||
|
||||
This TypeScript SDK maintains feature parity with the Python SDK:
|
||||
|
||||
| Python | TypeScript | Notes |
|
||||
|--------|------------|-------|
|
||||
| `Agent` | `Agent` | Same API |
|
||||
| `Agents` | `AgentTeam` | `Agents` is alias |
|
||||
| `Task` | `Task` | Same API |
|
||||
| `@tool` | `tool()` | Function vs decorator |
|
||||
| `Memory` | `Memory`, `FileMemory` | Same concepts |
|
||||
| `Hooks` | `HookManager` | Same events |
|
||||
| `Knowledge` | `Knowledge`, `RAG` | Same features |
|
||||
|
||||
---
|
||||
|
||||
*This document is the source of truth for the PraisonAI TypeScript SDK architecture and design principles.*
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "praisonai",
|
||||
"version": "1.7.1",
|
||||
"version": "1.6.0",
|
||||
"description": "PraisonAI TypeScript AI Agents Framework - Node.js, npm, and Javascript AI Agents Framework",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
|
||||
@@ -1,338 +0,0 @@
|
||||
/**
|
||||
* CodeAgent - Code generation, execution, review, and refactoring
|
||||
*
|
||||
* Python parity with praisonaiagents/agent/code_agent.py
|
||||
*/
|
||||
|
||||
import { Agent, SimpleAgentConfig } from './simple';
|
||||
|
||||
// ============================================================================
|
||||
// Configuration Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Configuration for CodeAgent.
|
||||
* Python parity with CodeConfig dataclass.
|
||||
*/
|
||||
export interface CodeConfig {
|
||||
/** Enable sandboxed execution (default: true for safety) */
|
||||
sandbox?: boolean;
|
||||
/** Execution timeout in seconds */
|
||||
timeout?: number;
|
||||
/** List of allowed programming languages */
|
||||
allowedLanguages?: string[];
|
||||
/** Maximum output length in characters */
|
||||
maxOutputLength?: number;
|
||||
/** Working directory for code execution */
|
||||
workingDirectory?: string;
|
||||
/** Environment variables for execution */
|
||||
environment?: Record<string, string>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Result of code execution.
|
||||
*/
|
||||
export interface CodeExecutionResult {
|
||||
/** Whether execution was successful */
|
||||
success: boolean;
|
||||
/** Standard output from execution */
|
||||
output: string;
|
||||
/** Error message if execution failed */
|
||||
error?: string;
|
||||
/** Exit code from execution */
|
||||
exitCode: number;
|
||||
/** Execution time in seconds */
|
||||
executionTime: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration for creating a CodeAgent.
|
||||
*/
|
||||
export interface CodeAgentConfig {
|
||||
/** Agent name */
|
||||
name?: string;
|
||||
/** LLM model (default: gpt-4o-mini) */
|
||||
llm?: string;
|
||||
/** Code configuration (bool, object, or CodeConfig) */
|
||||
code?: boolean | CodeConfig;
|
||||
/** System instructions */
|
||||
instructions?: string;
|
||||
/** Enable verbose output */
|
||||
verbose?: boolean;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Default Configuration
|
||||
// ============================================================================
|
||||
|
||||
const DEFAULT_CODE_CONFIG: Required<CodeConfig> = {
|
||||
sandbox: true,
|
||||
timeout: 30,
|
||||
allowedLanguages: ['python'],
|
||||
maxOutputLength: 10000,
|
||||
workingDirectory: process.cwd(),
|
||||
environment: {},
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// CodeAgent Class
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Agent for code generation, execution, review, and refactoring.
|
||||
*
|
||||
* This agent provides capabilities for:
|
||||
* - Generating code from natural language descriptions
|
||||
* - Executing code in a sandboxed environment
|
||||
* - Reviewing code for issues and improvements
|
||||
* - Refactoring and fixing code
|
||||
* - Explaining code functionality
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { CodeAgent } from 'praisonai';
|
||||
*
|
||||
* const agent = new CodeAgent({ name: 'Coder' });
|
||||
*
|
||||
* // Generate code
|
||||
* const code = await agent.generate('Write a function to calculate fibonacci');
|
||||
*
|
||||
* // Execute code
|
||||
* const result = await agent.execute("console.log('Hello, World!')");
|
||||
*
|
||||
* // Review code
|
||||
* const review = await agent.review(code);
|
||||
* ```
|
||||
*/
|
||||
export class CodeAgent {
|
||||
readonly name: string;
|
||||
private readonly llm: string;
|
||||
private readonly instructions?: string;
|
||||
private readonly verbose: boolean;
|
||||
private readonly codeConfig: Required<CodeConfig>;
|
||||
private readonly agent: Agent;
|
||||
|
||||
constructor(config: CodeAgentConfig) {
|
||||
this.name = config.name || 'CodeAgent';
|
||||
this.llm = config.llm || process.env.OPENAI_MODEL_NAME || 'gpt-4o-mini';
|
||||
this.instructions = config.instructions;
|
||||
this.verbose = config.verbose ?? true;
|
||||
|
||||
// Resolve code configuration
|
||||
if (config.code === undefined || config.code === true) {
|
||||
this.codeConfig = { ...DEFAULT_CODE_CONFIG };
|
||||
} else if (typeof config.code === 'object') {
|
||||
this.codeConfig = { ...DEFAULT_CODE_CONFIG, ...config.code };
|
||||
} else {
|
||||
this.codeConfig = { ...DEFAULT_CODE_CONFIG };
|
||||
}
|
||||
|
||||
// Create underlying agent for LLM calls
|
||||
this.agent = new Agent({
|
||||
name: this.name,
|
||||
instructions: this.buildSystemPrompt(),
|
||||
llm: this.llm,
|
||||
verbose: this.verbose,
|
||||
});
|
||||
}
|
||||
|
||||
private buildSystemPrompt(): string {
|
||||
let prompt = `You are an expert programmer and code assistant.
|
||||
You can generate, review, refactor, fix, and explain code.
|
||||
Follow best practices and coding standards.
|
||||
Write clean, well-documented, production-ready code.`;
|
||||
|
||||
if (this.instructions) {
|
||||
prompt += `\n\nAdditional instructions: ${this.instructions}`;
|
||||
}
|
||||
|
||||
return prompt;
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Code Generation Methods
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Generate code from natural language description.
|
||||
*
|
||||
* @param prompt - Natural language description of desired code
|
||||
* @param language - Target programming language (default: python)
|
||||
* @returns Generated code as string
|
||||
*/
|
||||
async generate(prompt: string, language: string = 'python'): Promise<string> {
|
||||
const systemPrompt = `You are an expert ${language} programmer.
|
||||
Generate clean, well-documented, production-ready code.
|
||||
Only output the code, no explanations unless asked.
|
||||
Follow best practices and coding standards.`;
|
||||
|
||||
const response = await this.agent.chat(
|
||||
`Generate ${language} code for: ${prompt}`,
|
||||
);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for generate() method.
|
||||
*/
|
||||
async generateCode(prompt: string, language: string = 'python'): Promise<string> {
|
||||
return this.generate(prompt, language);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Code Execution Methods
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Execute code in a sandboxed environment.
|
||||
*
|
||||
* @param code - Code to execute
|
||||
* @param language - Programming language (default: python)
|
||||
* @returns Execution result
|
||||
*/
|
||||
async execute(code: string, language: string = 'python'): Promise<CodeExecutionResult> {
|
||||
// Check if language is allowed
|
||||
if (!this.codeConfig.allowedLanguages.includes(language)) {
|
||||
return {
|
||||
success: false,
|
||||
output: '',
|
||||
error: `Language '${language}' is not allowed. Allowed: ${this.codeConfig.allowedLanguages.join(', ')}`,
|
||||
exitCode: 1,
|
||||
executionTime: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const startTime = Date.now();
|
||||
|
||||
try {
|
||||
// For safety, we use the sandbox executor if available
|
||||
// This is a simplified implementation - full sandbox would use child_process
|
||||
if (language === 'javascript' || language === 'typescript') {
|
||||
// Use eval for simple JS (NOT SAFE FOR PRODUCTION - use sandbox)
|
||||
if (!this.codeConfig.sandbox) {
|
||||
const result = eval(code);
|
||||
return {
|
||||
success: true,
|
||||
output: String(result ?? ''),
|
||||
exitCode: 0,
|
||||
executionTime: (Date.now() - startTime) / 1000,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// For other languages or sandboxed execution, return placeholder
|
||||
return {
|
||||
success: false,
|
||||
output: '',
|
||||
error: `Sandboxed execution for '${language}' requires additional setup. Use sandbox executor.`,
|
||||
exitCode: 1,
|
||||
executionTime: (Date.now() - startTime) / 1000,
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
output: '',
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
exitCode: 1,
|
||||
executionTime: (Date.now() - startTime) / 1000,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for execute() method.
|
||||
*/
|
||||
async executeCode(code: string, language: string = 'python'): Promise<CodeExecutionResult> {
|
||||
return this.execute(code, language);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Code Review Methods
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Review code for issues and improvements.
|
||||
*
|
||||
* @param code - Code to review
|
||||
* @param language - Programming language
|
||||
* @returns Review feedback
|
||||
*/
|
||||
async review(code: string, language?: string): Promise<string> {
|
||||
const langHint = language ? ` (${language})` : '';
|
||||
const response = await this.agent.chat(
|
||||
`Review the following code${langHint} for issues, bugs, and improvements:\n\n\`\`\`\n${code}\n\`\`\``,
|
||||
);
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for review() method.
|
||||
*/
|
||||
async reviewCode(code: string, language?: string): Promise<string> {
|
||||
return this.review(code, language);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Code Refactoring Methods
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Refactor code for better quality.
|
||||
*
|
||||
* @param code - Code to refactor
|
||||
* @param instructions - Specific refactoring instructions
|
||||
* @returns Refactored code
|
||||
*/
|
||||
async refactor(code: string, instructions?: string): Promise<string> {
|
||||
const prompt = instructions
|
||||
? `Refactor the following code according to these instructions: ${instructions}\n\n\`\`\`\n${code}\n\`\`\``
|
||||
: `Refactor the following code for better readability, performance, and maintainability:\n\n\`\`\`\n${code}\n\`\`\``;
|
||||
|
||||
const response = await this.agent.chat(prompt);
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix bugs in code.
|
||||
*
|
||||
* @param code - Code with bugs
|
||||
* @param errorMessage - Optional error message to help identify the bug
|
||||
* @returns Fixed code
|
||||
*/
|
||||
async fix(code: string, errorMessage?: string): Promise<string> {
|
||||
const prompt = errorMessage
|
||||
? `Fix the bug in the following code. Error: ${errorMessage}\n\n\`\`\`\n${code}\n\`\`\``
|
||||
: `Fix any bugs in the following code:\n\n\`\`\`\n${code}\n\`\`\``;
|
||||
|
||||
const response = await this.agent.chat(prompt);
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Explain code functionality.
|
||||
*
|
||||
* @param code - Code to explain
|
||||
* @returns Explanation of the code
|
||||
*/
|
||||
async explain(code: string): Promise<string> {
|
||||
const response = await this.agent.chat(
|
||||
`Explain what the following code does in detail:\n\n\`\`\`\n${code}\n\`\`\``,
|
||||
);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Factory Function
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Create a CodeAgent instance.
|
||||
*
|
||||
* @param config - CodeAgent configuration
|
||||
* @returns CodeAgent instance
|
||||
*/
|
||||
export function createCodeAgent(config: CodeAgentConfig): CodeAgent {
|
||||
return new CodeAgent(config);
|
||||
}
|
||||
@@ -1,238 +0,0 @@
|
||||
/**
|
||||
* EmbeddingAgent - Text embedding generation agent
|
||||
*
|
||||
* Python parity with praisonaiagents/agent/embedding_agent.py
|
||||
* Generates embeddings for text using embedding models.
|
||||
*/
|
||||
|
||||
// ============================================================================
|
||||
// Configuration Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Configuration for Embedding settings.
|
||||
*/
|
||||
export interface EmbeddingConfig {
|
||||
/** Embedding model to use */
|
||||
model?: string;
|
||||
/** Dimensions for the embedding */
|
||||
dimensions?: number;
|
||||
/** Batch size for processing multiple texts */
|
||||
batchSize?: number;
|
||||
/** Timeout in seconds */
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Result of embedding generation.
|
||||
*/
|
||||
export interface EmbeddingResult {
|
||||
/** The embedding vector */
|
||||
embedding: number[];
|
||||
/** Model used */
|
||||
model: string;
|
||||
/** Token usage */
|
||||
usage?: {
|
||||
promptTokens: number;
|
||||
totalTokens: number;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Result of batch embedding generation.
|
||||
*/
|
||||
export interface BatchEmbeddingResult {
|
||||
/** Array of embeddings */
|
||||
embeddings: number[][];
|
||||
/** Model used */
|
||||
model: string;
|
||||
/** Total token usage */
|
||||
usage?: {
|
||||
promptTokens: number;
|
||||
totalTokens: number;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration for creating an EmbeddingAgent.
|
||||
*/
|
||||
export interface EmbeddingAgentConfig {
|
||||
/** Agent name */
|
||||
name?: string;
|
||||
/** Embedding model */
|
||||
llm?: string;
|
||||
/** Alias for llm */
|
||||
model?: string;
|
||||
/** Embedding configuration */
|
||||
embedding?: boolean | EmbeddingConfig;
|
||||
/** Enable verbose output */
|
||||
verbose?: boolean;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Default Configuration
|
||||
// ============================================================================
|
||||
|
||||
const DEFAULT_EMBEDDING_CONFIG: Required<EmbeddingConfig> = {
|
||||
model: 'text-embedding-3-small',
|
||||
dimensions: 1536,
|
||||
batchSize: 100,
|
||||
timeout: 60,
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// EmbeddingAgent Class
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Agent for generating text embeddings.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { EmbeddingAgent } from 'praisonai';
|
||||
*
|
||||
* const agent = new EmbeddingAgent({});
|
||||
*
|
||||
* // Generate embedding for text
|
||||
* const result = await agent.embed('Hello, world!');
|
||||
* console.log(result.embedding.length); // 1536
|
||||
*
|
||||
* // Generate embeddings for multiple texts
|
||||
* const results = await agent.embedMany(['Hello', 'World']);
|
||||
* ```
|
||||
*/
|
||||
export class EmbeddingAgent {
|
||||
static readonly DEFAULT_MODEL = 'text-embedding-3-small';
|
||||
|
||||
readonly name: string;
|
||||
private readonly model: string;
|
||||
private readonly verbose: boolean;
|
||||
private readonly embeddingConfig: Required<EmbeddingConfig>;
|
||||
|
||||
constructor(config: EmbeddingAgentConfig) {
|
||||
this.name = config.name || 'EmbeddingAgent';
|
||||
this.model = config.llm || config.model || process.env.OPENAI_EMBEDDING_MODEL || EmbeddingAgent.DEFAULT_MODEL;
|
||||
this.verbose = config.verbose ?? true;
|
||||
|
||||
// Resolve embedding configuration
|
||||
if (config.embedding === undefined || config.embedding === true || config.embedding === false) {
|
||||
this.embeddingConfig = { ...DEFAULT_EMBEDDING_CONFIG };
|
||||
} else {
|
||||
this.embeddingConfig = { ...DEFAULT_EMBEDDING_CONFIG, ...config.embedding };
|
||||
}
|
||||
}
|
||||
|
||||
private log(message: string): void {
|
||||
if (this.verbose) {
|
||||
console.log(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate embedding for a single text.
|
||||
*
|
||||
* @param text - Text to embed
|
||||
* @returns EmbeddingResult with the embedding vector
|
||||
*/
|
||||
async embed(text: string): Promise<EmbeddingResult> {
|
||||
this.log(`Generating embedding for text (${text.length} chars)...`);
|
||||
|
||||
// Placeholder implementation - real implementation would call OpenAI
|
||||
// For now, return a mock embedding
|
||||
const embedding = new Array(this.embeddingConfig.dimensions).fill(0).map(() => Math.random() - 0.5);
|
||||
|
||||
return {
|
||||
embedding,
|
||||
model: this.model,
|
||||
usage: {
|
||||
promptTokens: Math.ceil(text.length / 4),
|
||||
totalTokens: Math.ceil(text.length / 4),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate embeddings for multiple texts.
|
||||
*
|
||||
* @param texts - Array of texts to embed
|
||||
* @returns BatchEmbeddingResult with all embeddings
|
||||
*/
|
||||
async embedMany(texts: string[]): Promise<BatchEmbeddingResult> {
|
||||
this.log(`Generating embeddings for ${texts.length} texts...`);
|
||||
|
||||
const embeddings: number[][] = [];
|
||||
let totalTokens = 0;
|
||||
|
||||
for (const text of texts) {
|
||||
const result = await this.embed(text);
|
||||
embeddings.push(result.embedding);
|
||||
totalTokens += result.usage?.totalTokens || 0;
|
||||
}
|
||||
|
||||
return {
|
||||
embeddings,
|
||||
model: this.model,
|
||||
usage: {
|
||||
promptTokens: totalTokens,
|
||||
totalTokens,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate cosine similarity between two embeddings.
|
||||
*/
|
||||
cosineSimilarity(a: number[], b: number[]): number {
|
||||
if (a.length !== b.length) {
|
||||
throw new Error('Embeddings must have the same dimensions');
|
||||
}
|
||||
|
||||
let dotProduct = 0;
|
||||
let normA = 0;
|
||||
let normB = 0;
|
||||
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
dotProduct += a[i] * b[i];
|
||||
normA += a[i] * a[i];
|
||||
normB += b[i] * b[i];
|
||||
}
|
||||
|
||||
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the most similar text from a list.
|
||||
*/
|
||||
async findMostSimilar(query: string, candidates: string[]): Promise<{ text: string; similarity: number; index: number }> {
|
||||
const queryResult = await this.embed(query);
|
||||
const candidateResults = await this.embedMany(candidates);
|
||||
|
||||
let bestIndex = 0;
|
||||
let bestSimilarity = -1;
|
||||
|
||||
for (let i = 0; i < candidateResults.embeddings.length; i++) {
|
||||
const similarity = this.cosineSimilarity(queryResult.embedding, candidateResults.embeddings[i]);
|
||||
if (similarity > bestSimilarity) {
|
||||
bestSimilarity = similarity;
|
||||
bestIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
text: candidates[bestIndex],
|
||||
similarity: bestSimilarity,
|
||||
index: bestIndex,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Factory Function
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Create an EmbeddingAgent instance.
|
||||
*/
|
||||
export function createEmbeddingAgent(config: EmbeddingAgentConfig): EmbeddingAgent {
|
||||
return new EmbeddingAgent(config);
|
||||
}
|
||||
@@ -1,199 +1,15 @@
|
||||
/**
|
||||
* Agent Handoff - Transfer conversations between agents
|
||||
*
|
||||
* Python parity with praisonaiagents/agent/handoff.py:
|
||||
* - HandoffError, HandoffCycleError, HandoffDepthError, HandoffTimeoutError
|
||||
* - ContextPolicy enum
|
||||
* - HandoffConfig, HandoffInputData, HandoffResult
|
||||
* - Handoff class with safety checks
|
||||
* - RECOMMENDED_PROMPT_PREFIX, promptWithHandoffInstructions
|
||||
*/
|
||||
|
||||
import type { EnhancedAgent } from './enhanced';
|
||||
|
||||
// ============================================================================
|
||||
// Constants (Python Parity)
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Recommended prompt prefix for agents that support handoffs.
|
||||
* Python parity with praisonaiagents/agent/handoff.py
|
||||
*/
|
||||
export const RECOMMENDED_PROMPT_PREFIX = `You are a helpful assistant that can delegate tasks to specialized agents when appropriate.
|
||||
When you determine that a task would be better handled by another agent, use the appropriate handoff tool.
|
||||
Always explain to the user why you are transferring them to another agent.`;
|
||||
|
||||
/**
|
||||
* Build a prompt with handoff instructions appended.
|
||||
* Python parity with praisonaiagents/agent/handoff.py
|
||||
*
|
||||
* @param basePrompt - The base system prompt
|
||||
* @param handoffs - Array of Handoff objects
|
||||
* @returns Combined prompt with handoff instructions
|
||||
*/
|
||||
export function promptWithHandoffInstructions(
|
||||
basePrompt: string,
|
||||
handoffs: Handoff[]
|
||||
): string {
|
||||
if (handoffs.length === 0) {
|
||||
return basePrompt;
|
||||
}
|
||||
|
||||
const handoffList = handoffs
|
||||
.map(h => `- **${h.name}**: ${h.description}`)
|
||||
.join('\n');
|
||||
|
||||
return `${basePrompt}
|
||||
|
||||
## Available Agents for Handoff
|
||||
|
||||
You can transfer the conversation to the following specialized agents when appropriate:
|
||||
|
||||
${handoffList}
|
||||
|
||||
When transferring, always:
|
||||
1. Explain to the user why you're transferring them
|
||||
2. Provide context about what the specialized agent can help with
|
||||
3. Use the appropriate handoff tool`;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Error Types (Python Parity)
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Base exception for handoff errors.
|
||||
*/
|
||||
export class HandoffError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'HandoffError';
|
||||
// Maintains proper stack trace for where error was thrown (V8 only)
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this, HandoffError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Raised when a cycle is detected in handoff chain.
|
||||
*/
|
||||
export class HandoffCycleError extends HandoffError {
|
||||
readonly chain: string[];
|
||||
|
||||
constructor(chain: string[]) {
|
||||
super(`Handoff cycle detected: ${chain.join(' -> ')}`);
|
||||
this.name = 'HandoffCycleError';
|
||||
this.chain = chain;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Raised when max handoff depth is exceeded.
|
||||
*/
|
||||
export class HandoffDepthError extends HandoffError {
|
||||
readonly depth: number;
|
||||
readonly maxDepth: number;
|
||||
|
||||
constructor(depth: number, maxDepth: number) {
|
||||
super(`Max handoff depth exceeded: ${depth} > ${maxDepth}`);
|
||||
this.name = 'HandoffDepthError';
|
||||
this.depth = depth;
|
||||
this.maxDepth = maxDepth;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Raised when handoff times out.
|
||||
*/
|
||||
export class HandoffTimeoutError extends HandoffError {
|
||||
readonly timeout: number;
|
||||
readonly agentName: string;
|
||||
|
||||
constructor(timeout: number, agentName: string) {
|
||||
super(`Handoff to ${agentName} timed out after ${timeout}s`);
|
||||
this.name = 'HandoffTimeoutError';
|
||||
this.timeout = timeout;
|
||||
this.agentName = agentName;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Context Policy (Python Parity)
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Policy for context sharing during handoff.
|
||||
*/
|
||||
export const ContextPolicy = {
|
||||
FULL: 'full' as const, // Share full conversation history
|
||||
SUMMARY: 'summary' as const, // Share summarized context (default - safe)
|
||||
NONE: 'none' as const, // No context sharing
|
||||
LAST_N: 'last_n' as const, // Share last N messages
|
||||
} as const;
|
||||
|
||||
export type ContextPolicyType = typeof ContextPolicy[keyof typeof ContextPolicy];
|
||||
|
||||
// ============================================================================
|
||||
// Data Types (Python Parity)
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Data passed to a handoff target agent.
|
||||
*/
|
||||
export interface HandoffInputData {
|
||||
messages: any[];
|
||||
context: Record<string, any>;
|
||||
sourceAgent?: string;
|
||||
handoffDepth?: number;
|
||||
handoffChain?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Unified configuration for handoff behavior.
|
||||
* Python parity with HandoffConfig dataclass.
|
||||
*/
|
||||
export interface HandoffConfig {
|
||||
/** The target agent to hand off to */
|
||||
agent: EnhancedAgent;
|
||||
/** Custom tool name (defaults to transfer_to_<agent_name>) */
|
||||
name?: string;
|
||||
/** Custom tool description */
|
||||
description?: string;
|
||||
/** Condition function to determine if handoff should trigger */
|
||||
condition?: (context: HandoffContext) => boolean;
|
||||
/** Function to filter/transform input before passing to target agent */
|
||||
transformContext?: (messages: any[]) => any[];
|
||||
|
||||
// Context control (Python parity)
|
||||
/** How to share context during handoff (default: summary for safety) */
|
||||
contextPolicy?: ContextPolicyType;
|
||||
/** Maximum tokens to include in context */
|
||||
maxContextTokens?: number;
|
||||
/** Maximum messages to include (for LAST_N policy) */
|
||||
maxContextMessages?: number;
|
||||
/** Whether to preserve system messages in context */
|
||||
preserveSystem?: boolean;
|
||||
|
||||
// Execution control
|
||||
/** Timeout for handoff execution in seconds */
|
||||
timeoutSeconds?: number;
|
||||
/** Maximum concurrent handoffs (0 = unlimited) */
|
||||
maxConcurrent?: number;
|
||||
|
||||
// Safety
|
||||
/** Enable cycle detection to prevent infinite loops */
|
||||
detectCycles?: boolean;
|
||||
/** Maximum handoff chain depth */
|
||||
maxDepth?: number;
|
||||
|
||||
// Callbacks
|
||||
/** Callback when handoff starts */
|
||||
onHandoff?: (context: HandoffContext) => void | Promise<void>;
|
||||
/** Callback when handoff completes */
|
||||
onComplete?: (result: HandoffResult) => void | Promise<void>;
|
||||
/** Callback when handoff fails */
|
||||
onError?: (error: Error) => void | Promise<void>;
|
||||
}
|
||||
|
||||
export interface HandoffContext {
|
||||
|
||||
@@ -16,45 +16,6 @@ export type { SimpleAgentConfig, AgentTeamConfig, PraisonAIAgentsConfig } from '
|
||||
export { AudioAgent, createAudioAgent } from './audio';
|
||||
export type { AudioAgentConfig, SpeakOptions, TranscribeOptions, SpeakResult, TranscribeResult, AudioProvider } from './audio';
|
||||
|
||||
// CodeAgent - Code generation, execution, review, refactoring (Python parity)
|
||||
export { CodeAgent, createCodeAgent } from './code';
|
||||
export type { CodeConfig, CodeAgentConfig, CodeExecutionResult } from './code';
|
||||
|
||||
// OCRAgent - Optical Character Recognition (Python parity)
|
||||
export { OCRAgent, createOCRAgent } from './ocr';
|
||||
export type { OCRConfig, OCRAgentConfig, OCRResult, OCRPage } from './ocr';
|
||||
|
||||
// VisionAgent - Image analysis (Python parity)
|
||||
export { VisionAgent, createVisionAgent } from './vision';
|
||||
export type { VisionConfig, VisionAgentConfig, VisionResult } from './vision';
|
||||
|
||||
// VideoAgent - Video analysis (Python parity)
|
||||
export { VideoAgent, createVideoAgent } from './video';
|
||||
export type { VideoConfig, VideoAgentConfig, VideoResult } from './video';
|
||||
|
||||
// EmbeddingAgent - Text embeddings (Python parity)
|
||||
export { EmbeddingAgent, createEmbeddingAgent } from './embedding';
|
||||
export type { EmbeddingConfig, EmbeddingAgentConfig, EmbeddingResult, BatchEmbeddingResult } from './embedding';
|
||||
|
||||
// RealtimeAgent - Real-time voice (Python parity)
|
||||
export { RealtimeAgent, createRealtimeAgent } from './realtime';
|
||||
export type { RealtimeConfig, RealtimeAgentConfig, RealtimeEvent, RealtimeEventType } from './realtime';
|
||||
|
||||
// DeepResearchAgent - Research with citations (Python parity enhanced)
|
||||
export { DeepResearchAgent, createDeepResearchAgent } from './research';
|
||||
export type {
|
||||
DeepResearchConfig,
|
||||
DeepResearchResponse,
|
||||
ResearchResponse,
|
||||
Citation,
|
||||
ReasoningStep,
|
||||
CodeExecutionStep,
|
||||
FileSearchCall,
|
||||
MCPCall,
|
||||
WebSearchCall,
|
||||
Provider,
|
||||
} from './research';
|
||||
|
||||
// Router exports
|
||||
export { Router, RouterAgent, createRouter, routeConditions } from './router';
|
||||
export type { RouterConfig, RouteConfig, RouteContext, SimpleRouterConfig, SimpleRouteConfig } from './router';
|
||||
|
||||
@@ -1,276 +0,0 @@
|
||||
/**
|
||||
* OCRAgent - Optical Character Recognition agent
|
||||
*
|
||||
* Python parity with praisonaiagents/agent/ocr_agent.py
|
||||
* Extracts text from documents and images using AI models.
|
||||
*/
|
||||
|
||||
// ============================================================================
|
||||
// Configuration Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Configuration for OCR settings.
|
||||
* Python parity with OCRConfig dataclass.
|
||||
*/
|
||||
export interface OCRConfig {
|
||||
/** Include base64 images in response */
|
||||
includeImageBase64?: boolean;
|
||||
/** Specific pages to extract (for PDFs) */
|
||||
pages?: number[];
|
||||
/** Maximum images per page */
|
||||
imageLimit?: number;
|
||||
/** Timeout in seconds */
|
||||
timeout?: number;
|
||||
/** Custom API endpoint URL */
|
||||
apiBase?: string;
|
||||
/** API key for the provider */
|
||||
apiKey?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Page result from OCR extraction.
|
||||
*/
|
||||
export interface OCRPage {
|
||||
/** Page index (0-based) */
|
||||
index: number;
|
||||
/** Extracted text as markdown */
|
||||
markdown: string;
|
||||
/** Base64 images if requested */
|
||||
images?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Result of OCR extraction.
|
||||
*/
|
||||
export interface OCRResult {
|
||||
/** Combined extracted text */
|
||||
text: string;
|
||||
/** Per-page results */
|
||||
pages: OCRPage[];
|
||||
/** Additional metadata */
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration for creating an OCRAgent.
|
||||
*/
|
||||
export interface OCRAgentConfig {
|
||||
/** Agent name */
|
||||
name?: string;
|
||||
/** Optional instructions */
|
||||
instructions?: string;
|
||||
/** LLM model (e.g., "mistral/mistral-ocr-latest") */
|
||||
llm?: string;
|
||||
/** Alias for llm parameter */
|
||||
model?: string;
|
||||
/** Custom API endpoint URL */
|
||||
baseUrl?: string;
|
||||
/** API key for the provider */
|
||||
apiKey?: string;
|
||||
/** OCR configuration */
|
||||
ocr?: boolean | OCRConfig;
|
||||
/** Verbosity level for output */
|
||||
verbose?: boolean | number;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Default Configuration
|
||||
// ============================================================================
|
||||
|
||||
const DEFAULT_OCR_CONFIG: Required<OCRConfig> = {
|
||||
includeImageBase64: false,
|
||||
pages: [],
|
||||
imageLimit: 0,
|
||||
timeout: 600,
|
||||
apiBase: '',
|
||||
apiKey: '',
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// OCRAgent Class
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* A specialized agent for OCR (Optical Character Recognition).
|
||||
*
|
||||
* Extracts text from documents (PDFs) and images using AI models.
|
||||
*
|
||||
* Supported Providers:
|
||||
* - Mistral: `mistral/mistral-ocr-latest`
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { OCRAgent } from 'praisonai';
|
||||
*
|
||||
* const agent = new OCRAgent({ llm: 'mistral/mistral-ocr-latest' });
|
||||
*
|
||||
* // Extract from PDF URL
|
||||
* const result = await agent.extract('https://example.com/document.pdf');
|
||||
* console.log(result.text);
|
||||
*
|
||||
* // Extract from image URL
|
||||
* const result2 = await agent.extract('https://example.com/image.png');
|
||||
* for (const page of result2.pages) {
|
||||
* console.log(page.markdown);
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export class OCRAgent {
|
||||
static readonly DEFAULT_MODEL = 'mistral/mistral-ocr-latest';
|
||||
|
||||
readonly name: string;
|
||||
private readonly instructions?: string;
|
||||
private readonly llm: string;
|
||||
private readonly baseUrl?: string;
|
||||
private readonly apiKey?: string;
|
||||
private readonly ocrConfig: Required<OCRConfig>;
|
||||
private readonly verbose: boolean | number;
|
||||
|
||||
constructor(config: OCRAgentConfig) {
|
||||
// Handle model alias
|
||||
const llm = config.llm ?? config.model;
|
||||
|
||||
this.name = config.name || 'OCRAgent';
|
||||
this.instructions = config.instructions;
|
||||
this.llm = llm || process.env.PRAISONAI_OCR_MODEL || OCRAgent.DEFAULT_MODEL;
|
||||
this.baseUrl = config.baseUrl;
|
||||
this.apiKey = config.apiKey;
|
||||
this.verbose = config.verbose ?? true;
|
||||
|
||||
// Resolve OCR configuration
|
||||
this.ocrConfig = this.resolveOCRConfig(config.ocr);
|
||||
}
|
||||
|
||||
private resolveOCRConfig(ocr?: boolean | OCRConfig): Required<OCRConfig> {
|
||||
if (ocr === undefined || ocr === true || ocr === false) {
|
||||
return { ...DEFAULT_OCR_CONFIG };
|
||||
}
|
||||
return { ...DEFAULT_OCR_CONFIG, ...ocr };
|
||||
}
|
||||
|
||||
private buildDocument(source: string): { type: string; [key: string]: string } {
|
||||
const sourceLower = source.toLowerCase();
|
||||
const imageExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.webp'];
|
||||
|
||||
if (imageExtensions.some(ext => sourceLower.includes(ext))) {
|
||||
return { type: 'image_url', image_url: source };
|
||||
}
|
||||
return { type: 'document_url', document_url: source };
|
||||
}
|
||||
|
||||
private log(message: string): void {
|
||||
if (this.verbose) {
|
||||
console.log(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract text from a document or image.
|
||||
*
|
||||
* @param source - URL or path to document/image
|
||||
* @param options - Override options for this extraction
|
||||
* @returns OCRResult with pages, markdown content, and metadata
|
||||
*/
|
||||
async extract(
|
||||
source: string,
|
||||
options?: {
|
||||
includeImageBase64?: boolean;
|
||||
pages?: number[];
|
||||
imageLimit?: number;
|
||||
model?: string;
|
||||
}
|
||||
): Promise<OCRResult> {
|
||||
const model = options?.model || this.llm;
|
||||
|
||||
this.log(`Extracting text with ${model}...`);
|
||||
|
||||
// Build document reference
|
||||
const document = this.buildDocument(source);
|
||||
|
||||
// In a real implementation, this would call the OCR API
|
||||
// For now, we return a placeholder that indicates the structure
|
||||
// The actual implementation would use OpenAI's vision API or Mistral's OCR
|
||||
|
||||
try {
|
||||
// Placeholder implementation - real implementation would call LLM
|
||||
const result: OCRResult = {
|
||||
text: `[OCR extraction from ${source} - requires API integration]`,
|
||||
pages: [
|
||||
{
|
||||
index: 0,
|
||||
markdown: `[Content from ${source}]`,
|
||||
images: options?.includeImageBase64 ? [] : undefined,
|
||||
},
|
||||
],
|
||||
metadata: {
|
||||
source,
|
||||
model,
|
||||
documentType: document.type,
|
||||
},
|
||||
};
|
||||
|
||||
this.log('✓ OCR complete');
|
||||
return result;
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`OCR extraction failed: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Async version of extract() - same implementation since extract is already async.
|
||||
*/
|
||||
async aextract(
|
||||
source: string,
|
||||
options?: {
|
||||
includeImageBase64?: boolean;
|
||||
pages?: number[];
|
||||
imageLimit?: number;
|
||||
model?: string;
|
||||
}
|
||||
): Promise<OCRResult> {
|
||||
return this.extract(source, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Quick OCR - extract and return markdown text.
|
||||
*
|
||||
* @param source - URL or path to document/image
|
||||
* @returns Extracted text as markdown string
|
||||
*/
|
||||
async read(source: string): Promise<string> {
|
||||
const result = await this.extract(source);
|
||||
|
||||
// Combine all pages into markdown
|
||||
if (result.pages && result.pages.length > 0) {
|
||||
return result.pages
|
||||
.map(page => page.markdown)
|
||||
.filter(Boolean)
|
||||
.join('\n\n');
|
||||
}
|
||||
return result.text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Async version of read().
|
||||
*/
|
||||
async aread(source: string): Promise<string> {
|
||||
return this.read(source);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Factory Function
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Create an OCRAgent instance.
|
||||
*
|
||||
* @param config - OCRAgent configuration
|
||||
* @returns OCRAgent instance
|
||||
*/
|
||||
export function createOCRAgent(config: OCRAgentConfig): OCRAgent {
|
||||
return new OCRAgent(config);
|
||||
}
|
||||
@@ -1,308 +0,0 @@
|
||||
/**
|
||||
* RealtimeAgent - Real-time voice/audio interaction agent
|
||||
*
|
||||
* Python parity with praisonaiagents/agent/realtime_agent.py
|
||||
* Enables real-time voice conversations using WebSocket connections.
|
||||
*/
|
||||
|
||||
// ============================================================================
|
||||
// Configuration Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Configuration for Realtime settings.
|
||||
*/
|
||||
export interface RealtimeConfig {
|
||||
/** Voice to use for responses */
|
||||
voice?: 'alloy' | 'echo' | 'fable' | 'onyx' | 'nova' | 'shimmer';
|
||||
/** Audio input format */
|
||||
inputFormat?: 'pcm16' | 'g711_ulaw' | 'g711_alaw';
|
||||
/** Audio output format */
|
||||
outputFormat?: 'pcm16' | 'g711_ulaw' | 'g711_alaw';
|
||||
/** Sample rate in Hz */
|
||||
sampleRate?: number;
|
||||
/** Enable voice activity detection */
|
||||
vadEnabled?: boolean;
|
||||
/** VAD threshold */
|
||||
vadThreshold?: number;
|
||||
/** Timeout in seconds */
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event types for realtime sessions.
|
||||
*/
|
||||
export type RealtimeEventType =
|
||||
| 'session.created'
|
||||
| 'session.updated'
|
||||
| 'input_audio_buffer.append'
|
||||
| 'input_audio_buffer.commit'
|
||||
| 'input_audio_buffer.clear'
|
||||
| 'response.create'
|
||||
| 'response.done'
|
||||
| 'response.audio.delta'
|
||||
| 'response.audio.done'
|
||||
| 'response.text.delta'
|
||||
| 'response.text.done'
|
||||
| 'error';
|
||||
|
||||
/**
|
||||
* Realtime event.
|
||||
*/
|
||||
export interface RealtimeEvent {
|
||||
type: RealtimeEventType;
|
||||
data?: any;
|
||||
timestamp?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration for creating a RealtimeAgent.
|
||||
*/
|
||||
export interface RealtimeAgentConfig {
|
||||
/** Agent name */
|
||||
name?: string;
|
||||
/** Model to use */
|
||||
llm?: string;
|
||||
/** Realtime configuration */
|
||||
realtime?: boolean | RealtimeConfig;
|
||||
/** System instructions */
|
||||
instructions?: string;
|
||||
/** Enable verbose output */
|
||||
verbose?: boolean;
|
||||
/** API key */
|
||||
apiKey?: string;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Default Configuration
|
||||
// ============================================================================
|
||||
|
||||
const DEFAULT_REALTIME_CONFIG: Required<RealtimeConfig> = {
|
||||
voice: 'alloy',
|
||||
inputFormat: 'pcm16',
|
||||
outputFormat: 'pcm16',
|
||||
sampleRate: 24000,
|
||||
vadEnabled: true,
|
||||
vadThreshold: 0.5,
|
||||
timeout: 300,
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// RealtimeAgent Class
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Agent for real-time voice conversations.
|
||||
*
|
||||
* Uses WebSocket connections for low-latency audio streaming.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { RealtimeAgent } from 'praisonai';
|
||||
*
|
||||
* const agent = new RealtimeAgent({
|
||||
* instructions: 'You are a helpful voice assistant',
|
||||
* realtime: { voice: 'nova' }
|
||||
* });
|
||||
*
|
||||
* // Connect to realtime session
|
||||
* await agent.connect();
|
||||
*
|
||||
* // Send audio
|
||||
* agent.sendAudio(audioBuffer);
|
||||
*
|
||||
* // Listen for responses
|
||||
* agent.on('response.audio.delta', (event) => {
|
||||
* playAudio(event.data);
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
export class RealtimeAgent {
|
||||
static readonly DEFAULT_MODEL = 'gpt-4o-realtime-preview';
|
||||
|
||||
readonly name: string;
|
||||
private readonly llm: string;
|
||||
private readonly instructions?: string;
|
||||
private readonly verbose: boolean;
|
||||
private readonly realtimeConfig: Required<RealtimeConfig>;
|
||||
private readonly apiKey?: string;
|
||||
private connected: boolean = false;
|
||||
private eventHandlers: Map<RealtimeEventType, Array<(event: RealtimeEvent) => void>> = new Map();
|
||||
|
||||
constructor(config: RealtimeAgentConfig) {
|
||||
this.name = config.name || 'RealtimeAgent';
|
||||
this.llm = config.llm || process.env.OPENAI_REALTIME_MODEL || RealtimeAgent.DEFAULT_MODEL;
|
||||
this.instructions = config.instructions;
|
||||
this.verbose = config.verbose ?? true;
|
||||
this.apiKey = config.apiKey || process.env.OPENAI_API_KEY;
|
||||
|
||||
// Resolve realtime configuration
|
||||
if (config.realtime === undefined || config.realtime === true || config.realtime === false) {
|
||||
this.realtimeConfig = { ...DEFAULT_REALTIME_CONFIG };
|
||||
} else {
|
||||
this.realtimeConfig = { ...DEFAULT_REALTIME_CONFIG, ...config.realtime };
|
||||
}
|
||||
}
|
||||
|
||||
private log(message: string): void {
|
||||
if (this.verbose) {
|
||||
console.log(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to the realtime session.
|
||||
*/
|
||||
async connect(): Promise<void> {
|
||||
this.log('Connecting to realtime session...');
|
||||
|
||||
// Placeholder - real implementation would establish WebSocket connection
|
||||
this.connected = true;
|
||||
this.emit({ type: 'session.created', timestamp: Date.now() });
|
||||
|
||||
this.log('Connected to realtime session');
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect from the realtime session.
|
||||
*/
|
||||
async disconnect(): Promise<void> {
|
||||
this.log('Disconnecting from realtime session...');
|
||||
this.connected = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if connected.
|
||||
*/
|
||||
isConnected(): boolean {
|
||||
return this.connected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send audio data to the session.
|
||||
*/
|
||||
sendAudio(audioData: ArrayBuffer | Uint8Array): void {
|
||||
if (!this.connected) {
|
||||
throw new Error('Not connected to realtime session');
|
||||
}
|
||||
|
||||
this.emit({
|
||||
type: 'input_audio_buffer.append',
|
||||
data: audioData,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Commit the audio buffer for processing.
|
||||
*/
|
||||
commitAudio(): void {
|
||||
if (!this.connected) {
|
||||
throw new Error('Not connected to realtime session');
|
||||
}
|
||||
|
||||
this.emit({
|
||||
type: 'input_audio_buffer.commit',
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the audio buffer.
|
||||
*/
|
||||
clearAudio(): void {
|
||||
if (!this.connected) {
|
||||
throw new Error('Not connected to realtime session');
|
||||
}
|
||||
|
||||
this.emit({
|
||||
type: 'input_audio_buffer.clear',
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a text message.
|
||||
*/
|
||||
async sendText(text: string): Promise<void> {
|
||||
if (!this.connected) {
|
||||
throw new Error('Not connected to realtime session');
|
||||
}
|
||||
|
||||
this.log(`Sending text: ${text}`);
|
||||
|
||||
this.emit({
|
||||
type: 'response.create',
|
||||
data: { text },
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an event handler.
|
||||
*/
|
||||
on(eventType: RealtimeEventType, handler: (event: RealtimeEvent) => void): void {
|
||||
if (!this.eventHandlers.has(eventType)) {
|
||||
this.eventHandlers.set(eventType, []);
|
||||
}
|
||||
this.eventHandlers.get(eventType)!.push(handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an event handler.
|
||||
*/
|
||||
off(eventType: RealtimeEventType, handler: (event: RealtimeEvent) => void): void {
|
||||
const handlers = this.eventHandlers.get(eventType);
|
||||
if (handlers) {
|
||||
const index = handlers.indexOf(handler);
|
||||
if (index !== -1) {
|
||||
handlers.splice(index, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emit an event to handlers.
|
||||
*/
|
||||
private emit(event: RealtimeEvent): void {
|
||||
const handlers = this.eventHandlers.get(event.type);
|
||||
if (handlers) {
|
||||
for (const handler of handlers) {
|
||||
handler(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current configuration.
|
||||
*/
|
||||
getConfig(): Required<RealtimeConfig> {
|
||||
return { ...this.realtimeConfig };
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the session configuration.
|
||||
*/
|
||||
async updateConfig(config: Partial<RealtimeConfig>): Promise<void> {
|
||||
Object.assign(this.realtimeConfig, config);
|
||||
|
||||
if (this.connected) {
|
||||
this.emit({
|
||||
type: 'session.updated',
|
||||
data: this.realtimeConfig,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Factory Function
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Create a RealtimeAgent instance.
|
||||
*/
|
||||
export function createRealtimeAgent(config: RealtimeAgentConfig): RealtimeAgent {
|
||||
return new RealtimeAgent(config);
|
||||
}
|
||||
@@ -25,94 +25,6 @@ export interface ResearchResponse {
|
||||
confidence: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Python parity: DeepResearchResponse with additional fields
|
||||
*/
|
||||
export interface DeepResearchResponse extends ResearchResponse {
|
||||
/** Query that was researched */
|
||||
query: string;
|
||||
/** Total iterations performed */
|
||||
iterations: number;
|
||||
/** Time taken in seconds */
|
||||
duration?: number;
|
||||
/** Model used */
|
||||
model?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Python parity: Code execution step in research
|
||||
*/
|
||||
export interface CodeExecutionStep {
|
||||
/** Code that was executed */
|
||||
code: string;
|
||||
/** Language of the code */
|
||||
language: string;
|
||||
/** Output from execution */
|
||||
output?: string;
|
||||
/** Error if execution failed */
|
||||
error?: string;
|
||||
/** Whether execution succeeded */
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Python parity: File search call during research
|
||||
*/
|
||||
export interface FileSearchCall {
|
||||
/** Query used for file search */
|
||||
query: string;
|
||||
/** Files that matched */
|
||||
files: string[];
|
||||
/** Snippets from matched files */
|
||||
snippets?: Array<{ file: string; content: string; line?: number }>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Python parity: MCP (Model Context Protocol) call during research
|
||||
*/
|
||||
export interface MCPCall {
|
||||
/** Server name */
|
||||
server: string;
|
||||
/** Tool name */
|
||||
tool: string;
|
||||
/** Arguments passed */
|
||||
arguments: Record<string, any>;
|
||||
/** Result from the call */
|
||||
result?: any;
|
||||
/** Error if call failed */
|
||||
error?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Python parity: Web search call during research
|
||||
*/
|
||||
export interface WebSearchCall {
|
||||
/** Search query */
|
||||
query: string;
|
||||
/** Search engine used */
|
||||
engine?: string;
|
||||
/** Results returned */
|
||||
results: Citation[];
|
||||
/** Number of results */
|
||||
count: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Python parity: Provider configuration for research
|
||||
*/
|
||||
export interface Provider {
|
||||
/** Provider name (openai, anthropic, etc.) */
|
||||
name: string;
|
||||
/** Model to use */
|
||||
model: string;
|
||||
/** API key (optional, uses env var if not provided) */
|
||||
apiKey?: string;
|
||||
/** Base URL for API */
|
||||
baseUrl?: string;
|
||||
/** Additional options */
|
||||
options?: Record<string, any>;
|
||||
}
|
||||
|
||||
export interface DeepResearchConfig {
|
||||
name?: string;
|
||||
llm?: string;
|
||||
|
||||
@@ -197,24 +197,6 @@ export class Agent {
|
||||
// Auto-generate tool definition
|
||||
this.addAutoGeneratedToolDefinition(randomName, tool);
|
||||
}
|
||||
} else if (tool && typeof tool === 'object' && typeof tool.execute === 'function' && tool.name) {
|
||||
// If it's a FunctionTool instance (has execute method and name)
|
||||
const funcName = tool.name;
|
||||
|
||||
// Register the execute function with args as object (not spread)
|
||||
this.registerToolFunction(funcName, async (args: any) => {
|
||||
return tool.execute(args);
|
||||
});
|
||||
|
||||
// Add the tool definition from FunctionTool
|
||||
processedTools.push(tool.toOpenAITool ? tool.toOpenAITool() : {
|
||||
type: 'function',
|
||||
function: {
|
||||
name: tool.name,
|
||||
description: tool.description || `Function ${tool.name}`,
|
||||
parameters: tool.parameters || { type: 'object', properties: {} },
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// If it's already a tool definition, add it as is
|
||||
processedTools.push(tool);
|
||||
@@ -418,8 +400,8 @@ export class Agent {
|
||||
throw new Error(`Function ${name} not registered`);
|
||||
}
|
||||
|
||||
// Call the function - pass args as object (for FunctionTool) or spread (for legacy functions)
|
||||
const result = await this.toolFunctions[name](args);
|
||||
// Call the function
|
||||
const result = await this.toolFunctions[name](...Object.values(args));
|
||||
|
||||
// Add result to messages
|
||||
results.push({
|
||||
|
||||
@@ -1,210 +0,0 @@
|
||||
/**
|
||||
* VideoAgent - Video analysis and processing agent
|
||||
*
|
||||
* Python parity with praisonaiagents/agent/video_agent.py
|
||||
* Analyzes videos using AI models.
|
||||
*/
|
||||
|
||||
import { Agent } from './simple';
|
||||
|
||||
// ============================================================================
|
||||
// Configuration Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Configuration for Video settings.
|
||||
*/
|
||||
export interface VideoConfig {
|
||||
/** Maximum video duration in seconds */
|
||||
maxDuration?: number;
|
||||
/** Frame extraction rate (frames per second) */
|
||||
frameRate?: number;
|
||||
/** Maximum frames to analyze */
|
||||
maxFrames?: number;
|
||||
/** Timeout in seconds */
|
||||
timeout?: number;
|
||||
/** Enable audio transcription */
|
||||
transcribeAudio?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Result of video analysis.
|
||||
*/
|
||||
export interface VideoResult {
|
||||
/** Overall description */
|
||||
description: string;
|
||||
/** Per-frame analysis */
|
||||
frames?: Array<{
|
||||
timestamp: number;
|
||||
description: string;
|
||||
}>;
|
||||
/** Audio transcription if available */
|
||||
transcript?: string;
|
||||
/** Duration in seconds */
|
||||
duration?: number;
|
||||
/** Additional metadata */
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration for creating a VideoAgent.
|
||||
*/
|
||||
export interface VideoAgentConfig {
|
||||
/** Agent name */
|
||||
name?: string;
|
||||
/** LLM model */
|
||||
llm?: string;
|
||||
/** Video configuration */
|
||||
video?: boolean | VideoConfig;
|
||||
/** System instructions */
|
||||
instructions?: string;
|
||||
/** Enable verbose output */
|
||||
verbose?: boolean;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Default Configuration
|
||||
// ============================================================================
|
||||
|
||||
const DEFAULT_VIDEO_CONFIG: Required<VideoConfig> = {
|
||||
maxDuration: 300,
|
||||
frameRate: 1,
|
||||
maxFrames: 30,
|
||||
timeout: 600,
|
||||
transcribeAudio: true,
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// VideoAgent Class
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Agent for video analysis and processing.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { VideoAgent } from 'praisonai';
|
||||
*
|
||||
* const agent = new VideoAgent({ llm: 'gpt-4o' });
|
||||
*
|
||||
* // Analyze a video
|
||||
* const result = await agent.analyze('https://example.com/video.mp4');
|
||||
* console.log(result.description);
|
||||
* ```
|
||||
*/
|
||||
export class VideoAgent {
|
||||
static readonly DEFAULT_MODEL = 'gpt-4o';
|
||||
|
||||
readonly name: string;
|
||||
private readonly llm: string;
|
||||
private readonly instructions?: string;
|
||||
private readonly verbose: boolean;
|
||||
private readonly videoConfig: Required<VideoConfig>;
|
||||
private readonly agent: Agent;
|
||||
|
||||
constructor(config: VideoAgentConfig) {
|
||||
this.name = config.name || 'VideoAgent';
|
||||
this.llm = config.llm || process.env.OPENAI_MODEL_NAME || VideoAgent.DEFAULT_MODEL;
|
||||
this.instructions = config.instructions;
|
||||
this.verbose = config.verbose ?? true;
|
||||
|
||||
// Resolve video configuration
|
||||
if (config.video === undefined || config.video === true || config.video === false) {
|
||||
this.videoConfig = { ...DEFAULT_VIDEO_CONFIG };
|
||||
} else {
|
||||
this.videoConfig = { ...DEFAULT_VIDEO_CONFIG, ...config.video };
|
||||
}
|
||||
|
||||
this.agent = new Agent({
|
||||
name: this.name,
|
||||
instructions: this.buildSystemPrompt(),
|
||||
llm: this.llm,
|
||||
verbose: this.verbose,
|
||||
});
|
||||
}
|
||||
|
||||
private buildSystemPrompt(): string {
|
||||
let prompt = `You are an expert video analyst.
|
||||
You can analyze videos, describe their contents, identify actions and events, and answer questions about video content.`;
|
||||
|
||||
if (this.instructions) {
|
||||
prompt += `\n\nAdditional instructions: ${this.instructions}`;
|
||||
}
|
||||
|
||||
return prompt;
|
||||
}
|
||||
|
||||
private log(message: string): void {
|
||||
if (this.verbose) {
|
||||
console.log(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Analyze a video and return a detailed description.
|
||||
*/
|
||||
async analyze(videoUrl: string): Promise<VideoResult> {
|
||||
this.log(`Analyzing video: ${videoUrl}`);
|
||||
|
||||
const response = await this.agent.chat(
|
||||
`Analyze this video in detail: ${videoUrl}`
|
||||
);
|
||||
|
||||
return {
|
||||
description: response,
|
||||
metadata: { videoUrl, model: this.llm },
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask a question about a video.
|
||||
*/
|
||||
async ask(question: string, videoUrl: string): Promise<string> {
|
||||
this.log(`Asking about video: ${question}`);
|
||||
|
||||
const response = await this.agent.chat(
|
||||
`Looking at this video (${videoUrl}), ${question}`
|
||||
);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Describe the contents of a video.
|
||||
*/
|
||||
async describe(videoUrl: string): Promise<string> {
|
||||
const result = await this.analyze(videoUrl);
|
||||
return result.description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarize a video.
|
||||
*/
|
||||
async summarize(videoUrl: string): Promise<string> {
|
||||
const response = await this.agent.chat(
|
||||
`Provide a concise summary of this video: ${videoUrl}`
|
||||
);
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract key moments from a video.
|
||||
*/
|
||||
async extractKeyMoments(videoUrl: string): Promise<string[]> {
|
||||
const response = await this.agent.chat(
|
||||
`List the key moments or events in this video (${videoUrl}). Return as a numbered list.`
|
||||
);
|
||||
return response.split('\n').filter(Boolean);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Factory Function
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Create a VideoAgent instance.
|
||||
*/
|
||||
export function createVideoAgent(config: VideoAgentConfig): VideoAgent {
|
||||
return new VideoAgent(config);
|
||||
}
|
||||
@@ -1,229 +0,0 @@
|
||||
/**
|
||||
* VisionAgent - Image analysis and understanding agent
|
||||
*
|
||||
* Python parity with praisonaiagents/agent/vision_agent.py
|
||||
* Analyzes images using vision-capable LLMs.
|
||||
*/
|
||||
|
||||
import { Agent } from './simple';
|
||||
|
||||
// ============================================================================
|
||||
// Configuration Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Configuration for Vision settings.
|
||||
*/
|
||||
export interface VisionConfig {
|
||||
/** Maximum image size in pixels */
|
||||
maxImageSize?: number;
|
||||
/** Image quality for processing */
|
||||
quality?: 'low' | 'high' | 'auto';
|
||||
/** Detail level for analysis */
|
||||
detail?: 'low' | 'high' | 'auto';
|
||||
/** Timeout in seconds */
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Result of vision analysis.
|
||||
*/
|
||||
export interface VisionResult {
|
||||
/** Analysis description */
|
||||
description: string;
|
||||
/** Detected objects */
|
||||
objects?: string[];
|
||||
/** Detected text (if any) */
|
||||
text?: string;
|
||||
/** Confidence score */
|
||||
confidence?: number;
|
||||
/** Additional metadata */
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration for creating a VisionAgent.
|
||||
*/
|
||||
export interface VisionAgentConfig {
|
||||
/** Agent name */
|
||||
name?: string;
|
||||
/** LLM model (must support vision) */
|
||||
llm?: string;
|
||||
/** Vision configuration */
|
||||
vision?: boolean | VisionConfig;
|
||||
/** System instructions */
|
||||
instructions?: string;
|
||||
/** Enable verbose output */
|
||||
verbose?: boolean;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Default Configuration
|
||||
// ============================================================================
|
||||
|
||||
const DEFAULT_VISION_CONFIG: Required<VisionConfig> = {
|
||||
maxImageSize: 4096,
|
||||
quality: 'auto',
|
||||
detail: 'auto',
|
||||
timeout: 60,
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// VisionAgent Class
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Agent for image analysis and understanding.
|
||||
*
|
||||
* Uses vision-capable LLMs to analyze images and answer questions about them.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { VisionAgent } from 'praisonai';
|
||||
*
|
||||
* const agent = new VisionAgent({ llm: 'gpt-4o' });
|
||||
*
|
||||
* // Analyze an image
|
||||
* const result = await agent.analyze('https://example.com/image.jpg');
|
||||
* console.log(result.description);
|
||||
*
|
||||
* // Ask a question about an image
|
||||
* const answer = await agent.ask('What objects are in this image?', 'https://example.com/image.jpg');
|
||||
* ```
|
||||
*/
|
||||
export class VisionAgent {
|
||||
static readonly DEFAULT_MODEL = 'gpt-4o';
|
||||
|
||||
readonly name: string;
|
||||
private readonly llm: string;
|
||||
private readonly instructions?: string;
|
||||
private readonly verbose: boolean;
|
||||
private readonly visionConfig: Required<VisionConfig>;
|
||||
private readonly agent: Agent;
|
||||
|
||||
constructor(config: VisionAgentConfig) {
|
||||
this.name = config.name || 'VisionAgent';
|
||||
this.llm = config.llm || process.env.OPENAI_MODEL_NAME || VisionAgent.DEFAULT_MODEL;
|
||||
this.instructions = config.instructions;
|
||||
this.verbose = config.verbose ?? true;
|
||||
|
||||
// Resolve vision configuration
|
||||
if (config.vision === undefined || config.vision === true || config.vision === false) {
|
||||
this.visionConfig = { ...DEFAULT_VISION_CONFIG };
|
||||
} else {
|
||||
this.visionConfig = { ...DEFAULT_VISION_CONFIG, ...config.vision };
|
||||
}
|
||||
|
||||
// Create underlying agent
|
||||
this.agent = new Agent({
|
||||
name: this.name,
|
||||
instructions: this.buildSystemPrompt(),
|
||||
llm: this.llm,
|
||||
verbose: this.verbose,
|
||||
});
|
||||
}
|
||||
|
||||
private buildSystemPrompt(): string {
|
||||
let prompt = `You are an expert image analyst with vision capabilities.
|
||||
You can analyze images, describe their contents, identify objects, read text, and answer questions about visual content.
|
||||
Provide detailed and accurate descriptions.`;
|
||||
|
||||
if (this.instructions) {
|
||||
prompt += `\n\nAdditional instructions: ${this.instructions}`;
|
||||
}
|
||||
|
||||
return prompt;
|
||||
}
|
||||
|
||||
private log(message: string): void {
|
||||
if (this.verbose) {
|
||||
console.log(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Analyze an image and return a detailed description.
|
||||
*
|
||||
* @param imageUrl - URL of the image to analyze
|
||||
* @returns VisionResult with description and detected elements
|
||||
*/
|
||||
async analyze(imageUrl: string): Promise<VisionResult> {
|
||||
this.log(`Analyzing image: ${imageUrl}`);
|
||||
|
||||
const response = await this.agent.chat(
|
||||
`Analyze this image in detail: ${imageUrl}`
|
||||
);
|
||||
|
||||
return {
|
||||
description: response,
|
||||
metadata: { imageUrl, model: this.llm },
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask a question about an image.
|
||||
*
|
||||
* @param question - Question to ask about the image
|
||||
* @param imageUrl - URL of the image
|
||||
* @returns Answer to the question
|
||||
*/
|
||||
async ask(question: string, imageUrl: string): Promise<string> {
|
||||
this.log(`Asking about image: ${question}`);
|
||||
|
||||
const response = await this.agent.chat(
|
||||
`Looking at this image (${imageUrl}), ${question}`
|
||||
);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Describe the contents of an image.
|
||||
*
|
||||
* @param imageUrl - URL of the image
|
||||
* @returns Description of the image
|
||||
*/
|
||||
async describe(imageUrl: string): Promise<string> {
|
||||
const result = await this.analyze(imageUrl);
|
||||
return result.description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect objects in an image.
|
||||
*
|
||||
* @param imageUrl - URL of the image
|
||||
* @returns List of detected objects
|
||||
*/
|
||||
async detectObjects(imageUrl: string): Promise<string[]> {
|
||||
const response = await this.agent.chat(
|
||||
`List all objects you can identify in this image (${imageUrl}). Return as a comma-separated list.`
|
||||
);
|
||||
|
||||
return response.split(',').map(s => s.trim()).filter(Boolean);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract text from an image.
|
||||
*
|
||||
* @param imageUrl - URL of the image
|
||||
* @returns Extracted text
|
||||
*/
|
||||
async extractText(imageUrl: string): Promise<string> {
|
||||
const response = await this.agent.chat(
|
||||
`Extract and return all visible text from this image (${imageUrl}).`
|
||||
);
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Factory Function
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Create a VisionAgent instance.
|
||||
*/
|
||||
export function createVisionAgent(config: VisionAgentConfig): VisionAgent {
|
||||
return new VisionAgent(config);
|
||||
}
|
||||
@@ -29,126 +29,6 @@ export interface FastContextResult {
|
||||
latencyMs: number;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// LineRange & FileMatch (Python parity with praisonaiagents/context/fast/result.py)
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Represents a range of lines in a file.
|
||||
*
|
||||
* Python parity: praisonaiagents/context/fast/result.py:13-52
|
||||
*/
|
||||
export interface LineRange {
|
||||
/** Starting line number (1-indexed) */
|
||||
start: number;
|
||||
/** Ending line number (1-indexed, inclusive) */
|
||||
end: number;
|
||||
/** Optional content of the lines */
|
||||
content?: string;
|
||||
/** Score indicating relevance (0.0-1.0) */
|
||||
relevanceScore: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a LineRange with defaults.
|
||||
*/
|
||||
export function createLineRange(config: Partial<LineRange> & { start: number; end: number }): LineRange {
|
||||
return {
|
||||
start: Math.max(1, config.start),
|
||||
end: Math.max(config.start, config.end),
|
||||
content: config.content,
|
||||
relevanceScore: config.relevanceScore ?? 1.0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of lines in a range.
|
||||
*/
|
||||
export function getLineCount(range: LineRange): number {
|
||||
return range.end - range.start + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if two line ranges overlap.
|
||||
*/
|
||||
export function rangesOverlap(a: LineRange, b: LineRange): boolean {
|
||||
return !(a.end < b.start || a.start > b.end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge two overlapping line ranges.
|
||||
*/
|
||||
export function mergeRanges(a: LineRange, b: LineRange): LineRange {
|
||||
if (!rangesOverlap(a, b)) {
|
||||
throw new Error('Cannot merge non-overlapping ranges');
|
||||
}
|
||||
return {
|
||||
start: Math.min(a.start, b.start),
|
||||
end: Math.max(a.end, b.end),
|
||||
content: undefined, // Content needs to be re-fetched
|
||||
relevanceScore: Math.max(a.relevanceScore, b.relevanceScore),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a file match from Fast Context search.
|
||||
*
|
||||
* Python parity: praisonaiagents/context/fast/result.py:55-92
|
||||
*/
|
||||
export interface FileMatch {
|
||||
/** Absolute or relative path to the file */
|
||||
path: string;
|
||||
/** List of relevant line ranges in the file */
|
||||
lineRanges: LineRange[];
|
||||
/** Overall relevance score (0.0-1.0) */
|
||||
relevanceScore: number;
|
||||
/** Number of pattern matches found */
|
||||
matchCount: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a FileMatch with defaults.
|
||||
*/
|
||||
export function createFileMatch(config: Partial<FileMatch> & { path: string }): FileMatch {
|
||||
return {
|
||||
path: config.path,
|
||||
lineRanges: config.lineRanges ?? [],
|
||||
relevanceScore: config.relevanceScore ?? 1.0,
|
||||
matchCount: config.matchCount ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a line range to a file match, merging with existing overlapping ranges.
|
||||
*/
|
||||
export function addLineRangeToFileMatch(fileMatch: FileMatch, lineRange: LineRange): void {
|
||||
const overlapping: LineRange[] = [];
|
||||
const nonOverlapping: LineRange[] = [];
|
||||
|
||||
for (const existing of fileMatch.lineRanges) {
|
||||
if (rangesOverlap(existing, lineRange)) {
|
||||
overlapping.push(existing);
|
||||
} else {
|
||||
nonOverlapping.push(existing);
|
||||
}
|
||||
}
|
||||
|
||||
// Merge all overlapping ranges
|
||||
let merged = lineRange;
|
||||
for (const overlap of overlapping) {
|
||||
merged = mergeRanges(merged, overlap);
|
||||
}
|
||||
|
||||
fileMatch.lineRanges = [...nonOverlapping, merged].sort((a, b) => a.start - b.start);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total number of lines across all ranges in a file match.
|
||||
*/
|
||||
export function getTotalLines(fileMatch: FileMatch): number {
|
||||
return fileMatch.lineRanges.reduce((sum, r) => sum + getLineCount(r), 0);
|
||||
}
|
||||
|
||||
export interface CacheEntry {
|
||||
result: FastContextResult;
|
||||
timestamp: number;
|
||||
|
||||
@@ -168,22 +168,12 @@ export {
|
||||
type N8NWorkflowNode
|
||||
} from './n8n-integration';
|
||||
|
||||
// Fast Context (Python parity with praisonaiagents/context/fast)
|
||||
// Fast Context
|
||||
export {
|
||||
FastContext,
|
||||
createFastContext,
|
||||
getQuickContext,
|
||||
type FastContextConfig,
|
||||
type ContextSource,
|
||||
type FastContextResult,
|
||||
// Python parity additions
|
||||
type LineRange,
|
||||
createLineRange,
|
||||
getLineCount,
|
||||
rangesOverlap,
|
||||
mergeRanges,
|
||||
type FileMatch,
|
||||
createFileMatch,
|
||||
addLineRangeToFileMatch,
|
||||
getTotalLines,
|
||||
type FastContextResult
|
||||
} from './fast-context';
|
||||
@@ -1,295 +0,0 @@
|
||||
/**
|
||||
* Conditions Module for PraisonAI TypeScript SDK
|
||||
*
|
||||
* Python parity with praisonaiagents condition types
|
||||
*
|
||||
* Provides:
|
||||
* - Condition protocols
|
||||
* - Condition implementations
|
||||
* - Condition evaluation
|
||||
*/
|
||||
|
||||
// ============================================================================
|
||||
// Condition Protocol
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Condition protocol interface.
|
||||
* Python parity: praisonaiagents/conditions
|
||||
*/
|
||||
export interface ConditionProtocol {
|
||||
evaluate(context: Record<string, any>): boolean | Promise<boolean>;
|
||||
describe(): string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Routing condition protocol.
|
||||
* Python parity: praisonaiagents/conditions
|
||||
*/
|
||||
export interface RoutingConditionProtocol extends ConditionProtocol {
|
||||
getTargetAgent(context: Record<string, any>): string | null;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Condition Implementations
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Dictionary-based condition.
|
||||
* Python parity: praisonaiagents/conditions
|
||||
*/
|
||||
export class DictCondition implements ConditionProtocol {
|
||||
private conditions: Record<string, any>;
|
||||
private operator: 'and' | 'or';
|
||||
|
||||
constructor(conditions: Record<string, any>, operator: 'and' | 'or' = 'and') {
|
||||
this.conditions = conditions;
|
||||
this.operator = operator;
|
||||
}
|
||||
|
||||
evaluate(context: Record<string, any>): boolean {
|
||||
const results: boolean[] = [];
|
||||
|
||||
for (const [key, expectedValue] of Object.entries(this.conditions)) {
|
||||
const actualValue = context[key];
|
||||
|
||||
if (typeof expectedValue === 'function') {
|
||||
results.push(expectedValue(actualValue));
|
||||
} else if (expectedValue instanceof RegExp) {
|
||||
results.push(expectedValue.test(String(actualValue)));
|
||||
} else {
|
||||
results.push(actualValue === expectedValue);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.operator === 'and') {
|
||||
return results.every(r => r);
|
||||
} else {
|
||||
return results.some(r => r);
|
||||
}
|
||||
}
|
||||
|
||||
describe(): string {
|
||||
const conditions = Object.entries(this.conditions)
|
||||
.map(([k, v]) => `${k} = ${v}`)
|
||||
.join(` ${this.operator.toUpperCase()} `);
|
||||
return `DictCondition(${conditions})`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Expression-based condition.
|
||||
* Python parity: praisonaiagents/conditions
|
||||
*/
|
||||
export class ExpressionCondition implements ConditionProtocol {
|
||||
private expression: string;
|
||||
private evaluator: (context: Record<string, any>) => boolean;
|
||||
|
||||
constructor(expression: string) {
|
||||
this.expression = expression;
|
||||
this.evaluator = this.compileExpression(expression);
|
||||
}
|
||||
|
||||
private compileExpression(expr: string): (context: Record<string, any>) => boolean {
|
||||
// Simple expression parser for common patterns
|
||||
// Supports: ==, !=, >, <, >=, <=, &&, ||, !
|
||||
return (context: Record<string, any>) => {
|
||||
try {
|
||||
// Create a safe evaluation context
|
||||
const keys = Object.keys(context);
|
||||
const values = Object.values(context);
|
||||
|
||||
// Replace variable names with context values
|
||||
let evalExpr = expr;
|
||||
for (const key of keys) {
|
||||
const value = context[key];
|
||||
const replacement = typeof value === 'string' ? `"${value}"` : String(value);
|
||||
evalExpr = evalExpr.replace(new RegExp(`\\b${key}\\b`, 'g'), replacement);
|
||||
}
|
||||
|
||||
// Evaluate the expression (simplified - in production use a proper parser)
|
||||
// This is a basic implementation for common cases
|
||||
return this.evaluateSimple(evalExpr, context);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private evaluateSimple(expr: string, context: Record<string, any>): boolean {
|
||||
// Handle simple comparisons
|
||||
const comparisonMatch = expr.match(/^(\w+)\s*(==|!=|>=|<=|>|<)\s*(.+)$/);
|
||||
if (comparisonMatch) {
|
||||
const [, varName, op, valueStr] = comparisonMatch;
|
||||
const actualValue = context[varName];
|
||||
let expectedValue: any = valueStr.trim();
|
||||
|
||||
// Parse the expected value
|
||||
if (expectedValue === 'true') expectedValue = true;
|
||||
else if (expectedValue === 'false') expectedValue = false;
|
||||
else if (expectedValue === 'null') expectedValue = null;
|
||||
else if (/^-?\d+$/.test(expectedValue)) expectedValue = parseInt(expectedValue);
|
||||
else if (/^-?\d+\.\d+$/.test(expectedValue)) expectedValue = parseFloat(expectedValue);
|
||||
else if (/^["'].*["']$/.test(expectedValue)) expectedValue = expectedValue.slice(1, -1);
|
||||
|
||||
switch (op) {
|
||||
case '==': return actualValue === expectedValue;
|
||||
case '!=': return actualValue !== expectedValue;
|
||||
case '>': return actualValue > expectedValue;
|
||||
case '<': return actualValue < expectedValue;
|
||||
case '>=': return actualValue >= expectedValue;
|
||||
case '<=': return actualValue <= expectedValue;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle boolean expressions
|
||||
if (expr === 'true') return true;
|
||||
if (expr === 'false') return false;
|
||||
|
||||
// Handle variable lookup
|
||||
if (context[expr] !== undefined) {
|
||||
return Boolean(context[expr]);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
evaluate(context: Record<string, any>): boolean {
|
||||
return this.evaluator(context);
|
||||
}
|
||||
|
||||
describe(): string {
|
||||
return `ExpressionCondition(${this.expression})`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function-based condition.
|
||||
*/
|
||||
export class FunctionCondition implements ConditionProtocol {
|
||||
private fn: (context: Record<string, any>) => boolean | Promise<boolean>;
|
||||
private description: string;
|
||||
|
||||
constructor(
|
||||
fn: (context: Record<string, any>) => boolean | Promise<boolean>,
|
||||
description: string = 'FunctionCondition'
|
||||
) {
|
||||
this.fn = fn;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
evaluate(context: Record<string, any>): boolean | Promise<boolean> {
|
||||
return this.fn(context);
|
||||
}
|
||||
|
||||
describe(): string {
|
||||
return this.description;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Condition Evaluation
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Evaluate a condition.
|
||||
* Python parity: praisonaiagents/conditions
|
||||
*/
|
||||
export function evaluateCondition(
|
||||
condition: ConditionProtocol | Record<string, any> | string | ((ctx: Record<string, any>) => boolean),
|
||||
context: Record<string, any>
|
||||
): boolean | Promise<boolean> {
|
||||
// If it's a ConditionProtocol
|
||||
if (typeof condition === 'object' && 'evaluate' in condition) {
|
||||
return condition.evaluate(context);
|
||||
}
|
||||
|
||||
// If it's a function
|
||||
if (typeof condition === 'function') {
|
||||
return condition(context);
|
||||
}
|
||||
|
||||
// If it's a string expression
|
||||
if (typeof condition === 'string') {
|
||||
const expr = new ExpressionCondition(condition);
|
||||
return expr.evaluate(context);
|
||||
}
|
||||
|
||||
// If it's a dictionary
|
||||
if (typeof condition === 'object') {
|
||||
const dict = new DictCondition(condition);
|
||||
return dict.evaluate(context);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a condition from various input types.
|
||||
*/
|
||||
export function createCondition(
|
||||
input: ConditionProtocol | Record<string, any> | string | ((ctx: Record<string, any>) => boolean)
|
||||
): ConditionProtocol {
|
||||
if (typeof input === 'object' && 'evaluate' in input && 'describe' in input) {
|
||||
return input as ConditionProtocol;
|
||||
}
|
||||
|
||||
if (typeof input === 'function') {
|
||||
return new FunctionCondition(input as (ctx: Record<string, any>) => boolean);
|
||||
}
|
||||
|
||||
if (typeof input === 'string') {
|
||||
return new ExpressionCondition(input);
|
||||
}
|
||||
|
||||
if (typeof input === 'object') {
|
||||
return new DictCondition(input as Record<string, any>);
|
||||
}
|
||||
|
||||
throw new Error('Invalid condition input');
|
||||
}
|
||||
|
||||
/**
|
||||
* Combine conditions with AND.
|
||||
*/
|
||||
export function andConditions(...conditions: ConditionProtocol[]): ConditionProtocol {
|
||||
return new FunctionCondition(
|
||||
async (context) => {
|
||||
for (const condition of conditions) {
|
||||
const result = await condition.evaluate(context);
|
||||
if (!result) return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
`AND(${conditions.map(c => c.describe()).join(', ')})`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Combine conditions with OR.
|
||||
*/
|
||||
export function orConditions(...conditions: ConditionProtocol[]): ConditionProtocol {
|
||||
return new FunctionCondition(
|
||||
async (context) => {
|
||||
for (const condition of conditions) {
|
||||
const result = await condition.evaluate(context);
|
||||
if (result) return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
`OR(${conditions.map(c => c.describe()).join(', ')})`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Negate a condition.
|
||||
*/
|
||||
export function notCondition(condition: ConditionProtocol): ConditionProtocol {
|
||||
return new FunctionCondition(
|
||||
async (context) => {
|
||||
const result = await condition.evaluate(context);
|
||||
return !result;
|
||||
},
|
||||
`NOT(${condition.describe()})`
|
||||
);
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,7 +1,5 @@
|
||||
/**
|
||||
* Context Module Index - Export all context management utilities
|
||||
*
|
||||
* Python parity with praisonaiagents/context/__init__.py
|
||||
*/
|
||||
|
||||
export {
|
||||
@@ -15,7 +13,7 @@ export {
|
||||
export {
|
||||
ContextBudgeter,
|
||||
createContextBudgeter,
|
||||
type BudgetAllocation as ContextBudgeterAllocation,
|
||||
type BudgetAllocation,
|
||||
type ContextBudgeterConfig,
|
||||
} from './budgeter';
|
||||
|
||||
@@ -23,37 +21,10 @@ export {
|
||||
ContextOptimizer,
|
||||
createContextOptimizer,
|
||||
type OptimizableItem,
|
||||
type OptimizationResult as ContextOptimizationResult,
|
||||
type OptimizationResult,
|
||||
type OptimizationStrategy,
|
||||
type ContextOptimizerConfig,
|
||||
} from './optimizer';
|
||||
|
||||
// Python parity models
|
||||
export {
|
||||
ContextSegment,
|
||||
type ContextSegmentType,
|
||||
OptimizerStrategy,
|
||||
type OptimizerStrategyType,
|
||||
type ContextLedger,
|
||||
createContextLedger,
|
||||
getLedgerTotal,
|
||||
type BudgetAllocation,
|
||||
createBudgetAllocation,
|
||||
getUsableBudget,
|
||||
getHistoryBudget,
|
||||
type MonitorConfig,
|
||||
createMonitorConfig,
|
||||
type ContextConfig,
|
||||
createContextConfig,
|
||||
createRecipeContextConfig,
|
||||
type OptimizationResult,
|
||||
createOptimizationResult,
|
||||
getReductionPercent,
|
||||
type ContextSnapshot,
|
||||
createContextSnapshot,
|
||||
type ManagerConfig,
|
||||
createManagerConfig,
|
||||
} from './models';
|
||||
|
||||
// Default export
|
||||
export { default } from './manager';
|
||||
@@ -1,500 +0,0 @@
|
||||
/**
|
||||
* Context Management Data Models for PraisonAI TypeScript SDK.
|
||||
*
|
||||
* Python parity with praisonaiagents/context/models.py:
|
||||
* - ContextSegment enum
|
||||
* - ContextLedger class
|
||||
* - BudgetAllocation class
|
||||
* - ContextSnapshot class
|
||||
* - OptimizerStrategy enum
|
||||
* - OptimizationResult class
|
||||
* - MonitorConfig class
|
||||
* - ContextConfig class
|
||||
*/
|
||||
|
||||
// ============================================================================
|
||||
// Context Segment Enum
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Segments that contribute to context.
|
||||
* Python parity with ContextSegment enum.
|
||||
*/
|
||||
export const ContextSegment = {
|
||||
SYSTEM_PROMPT: 'system_prompt',
|
||||
RULES: 'rules',
|
||||
SKILLS: 'skills',
|
||||
MEMORY: 'memory',
|
||||
TOOLS_SCHEMA: 'tools_schema',
|
||||
HISTORY: 'history',
|
||||
TOOL_OUTPUTS: 'tool_outputs',
|
||||
BUFFER: 'buffer',
|
||||
} as const;
|
||||
|
||||
export type ContextSegmentType = typeof ContextSegment[keyof typeof ContextSegment];
|
||||
|
||||
// ============================================================================
|
||||
// Optimizer Strategy Enum
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Context optimization strategies.
|
||||
* Python parity with OptimizerStrategy enum.
|
||||
*/
|
||||
export const OptimizerStrategy = {
|
||||
TRUNCATE: 'truncate',
|
||||
SLIDING_WINDOW: 'sliding_window',
|
||||
SUMMARIZE: 'summarize',
|
||||
PRUNE_TOOLS: 'prune_tools',
|
||||
NON_DESTRUCTIVE: 'non_destructive',
|
||||
SMART: 'smart',
|
||||
} as const;
|
||||
|
||||
export type OptimizerStrategyType = typeof OptimizerStrategy[keyof typeof OptimizerStrategy];
|
||||
|
||||
// ============================================================================
|
||||
// Context Ledger
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Tracks token usage across context segments.
|
||||
* Python parity with ContextLedger dataclass.
|
||||
*/
|
||||
export interface ContextLedger {
|
||||
systemPrompt: number;
|
||||
rules: number;
|
||||
skills: number;
|
||||
memory: number;
|
||||
toolsSchema: number;
|
||||
history: number;
|
||||
toolOutputs: number;
|
||||
buffer: number;
|
||||
turnCount: number;
|
||||
messageCount: number;
|
||||
toolCallCount: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new ContextLedger with defaults.
|
||||
*/
|
||||
export function createContextLedger(partial?: Partial<ContextLedger>): ContextLedger {
|
||||
return {
|
||||
systemPrompt: 0,
|
||||
rules: 0,
|
||||
skills: 0,
|
||||
memory: 0,
|
||||
toolsSchema: 0,
|
||||
history: 0,
|
||||
toolOutputs: 0,
|
||||
buffer: 0,
|
||||
turnCount: 0,
|
||||
messageCount: 0,
|
||||
toolCallCount: 0,
|
||||
...partial,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total tokens from ledger.
|
||||
*/
|
||||
export function getLedgerTotal(ledger: ContextLedger): number {
|
||||
return (
|
||||
ledger.systemPrompt +
|
||||
ledger.rules +
|
||||
ledger.skills +
|
||||
ledger.memory +
|
||||
ledger.toolsSchema +
|
||||
ledger.history +
|
||||
ledger.toolOutputs +
|
||||
ledger.buffer
|
||||
);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Budget Allocation
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Token budget allocation across segments.
|
||||
* Python parity with BudgetAllocation dataclass.
|
||||
*/
|
||||
export interface BudgetAllocation {
|
||||
modelLimit: number;
|
||||
outputReserve: number;
|
||||
systemPrompt: number;
|
||||
rules: number;
|
||||
skills: number;
|
||||
memory: number;
|
||||
toolsSchema: number;
|
||||
history: number;
|
||||
toolOutputs: number;
|
||||
buffer: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new BudgetAllocation with defaults.
|
||||
*/
|
||||
export function createBudgetAllocation(partial?: Partial<BudgetAllocation>): BudgetAllocation {
|
||||
return {
|
||||
modelLimit: 128000,
|
||||
outputReserve: 8000,
|
||||
systemPrompt: 2000,
|
||||
rules: 500,
|
||||
skills: 500,
|
||||
memory: 1000,
|
||||
toolsSchema: 2000,
|
||||
history: -1, // Dynamic: fills remaining
|
||||
toolOutputs: 20000,
|
||||
buffer: 1000,
|
||||
...partial,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get usable tokens after output reserve.
|
||||
*/
|
||||
export function getUsableBudget(budget: BudgetAllocation): number {
|
||||
return budget.modelLimit - budget.outputReserve;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get computed history budget.
|
||||
*/
|
||||
export function getHistoryBudget(budget: BudgetAllocation): number {
|
||||
if (budget.history > 0) {
|
||||
return budget.history;
|
||||
}
|
||||
const fixedTotal =
|
||||
budget.systemPrompt +
|
||||
budget.rules +
|
||||
budget.skills +
|
||||
budget.memory +
|
||||
budget.toolsSchema +
|
||||
budget.toolOutputs +
|
||||
budget.buffer;
|
||||
return Math.max(0, getUsableBudget(budget) - fixedTotal);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Monitor Config
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Configuration for context monitoring.
|
||||
* Python parity with MonitorConfig dataclass.
|
||||
*/
|
||||
export interface MonitorConfig {
|
||||
enabled: boolean;
|
||||
path: string;
|
||||
format: 'human' | 'json';
|
||||
frequency: 'turn' | 'tool_call' | 'manual' | 'overflow';
|
||||
redactSensitive: boolean;
|
||||
multiAgentFiles: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new MonitorConfig with defaults.
|
||||
*/
|
||||
export function createMonitorConfig(partial?: Partial<MonitorConfig>): MonitorConfig {
|
||||
return {
|
||||
enabled: false,
|
||||
path: './context.txt',
|
||||
format: 'human',
|
||||
frequency: 'turn',
|
||||
redactSensitive: true,
|
||||
multiAgentFiles: true,
|
||||
...partial,
|
||||
};
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Context Config
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Complete context management configuration.
|
||||
* Python parity with ContextConfig dataclass.
|
||||
*/
|
||||
export interface ContextConfig {
|
||||
// Auto-compaction
|
||||
autoCompact: boolean;
|
||||
compactThreshold: number;
|
||||
strategy: OptimizerStrategyType;
|
||||
|
||||
// Budget overrides
|
||||
outputReserve: number;
|
||||
historyRatio: number;
|
||||
toolOutputMax: number;
|
||||
defaultToolOutputMax: number;
|
||||
|
||||
// Pruning
|
||||
pruneAfterTokens: number;
|
||||
protectedTools: string[];
|
||||
|
||||
// Per-tool output limits
|
||||
toolLimits: Record<string, number>;
|
||||
|
||||
// Monitoring
|
||||
monitor: MonitorConfig;
|
||||
|
||||
// Sliding window
|
||||
keepRecentTurns: number;
|
||||
|
||||
// LLM-powered summarization
|
||||
llmSummarize: boolean;
|
||||
smartToolSummarize: boolean;
|
||||
|
||||
// Session tracking (Agno pattern)
|
||||
sessionTracking: boolean;
|
||||
trackSummary: boolean;
|
||||
trackGoal: boolean;
|
||||
trackPlan: boolean;
|
||||
trackProgress: boolean;
|
||||
|
||||
// Multi-memory aggregation (CrewAI pattern)
|
||||
aggregateMemory: boolean;
|
||||
aggregateSources: string[];
|
||||
aggregateMaxTokens: number;
|
||||
|
||||
// Internal fields
|
||||
compressionMinGainPct: number;
|
||||
compressionMaxAttempts: number;
|
||||
toolBudgets: Record<string, any>;
|
||||
toolSummarizeLimits: Record<string, number>;
|
||||
estimationMode: string;
|
||||
logEstimationMismatch: boolean;
|
||||
mismatchThresholdPct: number;
|
||||
monitorEnabled: boolean;
|
||||
monitorPath: string;
|
||||
monitorFormat: string;
|
||||
monitorFrequency: string;
|
||||
monitorWriteMode: string;
|
||||
redactSensitive: boolean;
|
||||
snapshotTiming: string;
|
||||
allowAbsolutePaths: boolean;
|
||||
source: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new ContextConfig with defaults.
|
||||
*/
|
||||
export function createContextConfig(partial?: Partial<ContextConfig>): ContextConfig {
|
||||
return {
|
||||
autoCompact: true,
|
||||
compactThreshold: 0.8,
|
||||
strategy: OptimizerStrategy.SMART,
|
||||
outputReserve: 8000,
|
||||
historyRatio: 0.6,
|
||||
toolOutputMax: 10000,
|
||||
defaultToolOutputMax: 10000,
|
||||
pruneAfterTokens: 40000,
|
||||
protectedTools: [],
|
||||
toolLimits: {},
|
||||
monitor: createMonitorConfig(),
|
||||
keepRecentTurns: 5,
|
||||
llmSummarize: false,
|
||||
smartToolSummarize: true,
|
||||
sessionTracking: false,
|
||||
trackSummary: true,
|
||||
trackGoal: true,
|
||||
trackPlan: true,
|
||||
trackProgress: true,
|
||||
aggregateMemory: false,
|
||||
aggregateSources: ['memory', 'knowledge', 'rag'],
|
||||
aggregateMaxTokens: 4000,
|
||||
compressionMinGainPct: 5.0,
|
||||
compressionMaxAttempts: 3,
|
||||
toolBudgets: {},
|
||||
toolSummarizeLimits: {},
|
||||
estimationMode: 'heuristic',
|
||||
logEstimationMismatch: false,
|
||||
mismatchThresholdPct: 15.0,
|
||||
monitorEnabled: false,
|
||||
monitorPath: './context.txt',
|
||||
monitorFormat: 'human',
|
||||
monitorFrequency: 'turn',
|
||||
monitorWriteMode: 'sync',
|
||||
redactSensitive: true,
|
||||
snapshotTiming: 'post_optimization',
|
||||
allowAbsolutePaths: false,
|
||||
source: 'defaults',
|
||||
...partial,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a recipe-optimized ContextConfig.
|
||||
* Python parity with ContextConfig.for_recipe().
|
||||
*/
|
||||
export function createRecipeContextConfig(): ContextConfig {
|
||||
return createContextConfig({
|
||||
autoCompact: true,
|
||||
compactThreshold: 0.7,
|
||||
strategy: OptimizerStrategy.SMART,
|
||||
toolOutputMax: 2000,
|
||||
keepRecentTurns: 3,
|
||||
pruneAfterTokens: 50000,
|
||||
outputReserve: 8000,
|
||||
historyRatio: 0.6,
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Optimization Result
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Result of context optimization.
|
||||
* Python parity with OptimizationResult dataclass.
|
||||
*/
|
||||
export interface OptimizationResult {
|
||||
originalTokens: number;
|
||||
optimizedTokens: number;
|
||||
tokensSaved: number;
|
||||
strategyUsed: OptimizerStrategyType;
|
||||
messagesRemoved: number;
|
||||
messagesTagged: number;
|
||||
toolOutputsPruned: number;
|
||||
toolOutputsSummarized: number;
|
||||
tokensSavedBySummarization: number;
|
||||
tokensSavedByTruncation: number;
|
||||
summaryAdded: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new OptimizationResult with defaults.
|
||||
*/
|
||||
export function createOptimizationResult(partial?: Partial<OptimizationResult>): OptimizationResult {
|
||||
return {
|
||||
originalTokens: 0,
|
||||
optimizedTokens: 0,
|
||||
tokensSaved: 0,
|
||||
strategyUsed: OptimizerStrategy.SMART,
|
||||
messagesRemoved: 0,
|
||||
messagesTagged: 0,
|
||||
toolOutputsPruned: 0,
|
||||
toolOutputsSummarized: 0,
|
||||
tokensSavedBySummarization: 0,
|
||||
tokensSavedByTruncation: 0,
|
||||
summaryAdded: false,
|
||||
...partial,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get reduction percentage from optimization result.
|
||||
*/
|
||||
export function getReductionPercent(result: OptimizationResult): number {
|
||||
if (result.originalTokens === 0) {
|
||||
return 0;
|
||||
}
|
||||
return (result.tokensSaved / result.originalTokens) * 100;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Context Snapshot
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* A snapshot of the current context state.
|
||||
* Python parity with ContextSnapshot dataclass.
|
||||
*/
|
||||
export interface ContextSnapshot {
|
||||
timestamp: string;
|
||||
sessionId: string;
|
||||
agentName: string;
|
||||
modelName: string;
|
||||
budget: BudgetAllocation | null;
|
||||
ledger: ContextLedger | null;
|
||||
utilization: number;
|
||||
systemPromptContent: string;
|
||||
rulesContent: string;
|
||||
skillsContent: string;
|
||||
memoryContent: string;
|
||||
toolsSchemaContent: string;
|
||||
historyContent: Array<Record<string, any>>;
|
||||
warnings: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new ContextSnapshot with defaults.
|
||||
*/
|
||||
export function createContextSnapshot(partial?: Partial<ContextSnapshot>): ContextSnapshot {
|
||||
return {
|
||||
timestamp: '',
|
||||
sessionId: '',
|
||||
agentName: '',
|
||||
modelName: '',
|
||||
budget: null,
|
||||
ledger: null,
|
||||
utilization: 0,
|
||||
systemPromptContent: '',
|
||||
rulesContent: '',
|
||||
skillsContent: '',
|
||||
memoryContent: '',
|
||||
toolsSchemaContent: '',
|
||||
historyContent: [],
|
||||
warnings: [],
|
||||
...partial,
|
||||
};
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Manager Config (from context/manager.py)
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Configuration for ContextManager.
|
||||
* Python parity with ManagerConfig dataclass.
|
||||
*/
|
||||
export interface ManagerConfig {
|
||||
modelLimit: number;
|
||||
outputReserve: number;
|
||||
defaultToolOutputMax: number;
|
||||
compactThreshold: number;
|
||||
strategy: OptimizerStrategyType;
|
||||
compressionMinGainPct: number;
|
||||
compressionMaxAttempts: number;
|
||||
toolBudgets: Record<string, any>;
|
||||
toolSummarizeLimits: Record<string, number>;
|
||||
estimationMode: string;
|
||||
logEstimationMismatch: boolean;
|
||||
mismatchThresholdPct: number;
|
||||
monitorEnabled: boolean;
|
||||
monitorPath: string;
|
||||
monitorFormat: string;
|
||||
monitorFrequency: string;
|
||||
monitorWriteMode: string;
|
||||
redactSensitive: boolean;
|
||||
snapshotTiming: string;
|
||||
allowAbsolutePaths: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new ManagerConfig with defaults.
|
||||
*/
|
||||
export function createManagerConfig(partial?: Partial<ManagerConfig>): ManagerConfig {
|
||||
return {
|
||||
modelLimit: 128000,
|
||||
outputReserve: 8000,
|
||||
defaultToolOutputMax: 10000,
|
||||
compactThreshold: 0.8,
|
||||
strategy: OptimizerStrategy.SMART,
|
||||
compressionMinGainPct: 5.0,
|
||||
compressionMaxAttempts: 3,
|
||||
toolBudgets: {},
|
||||
toolSummarizeLimits: {},
|
||||
estimationMode: 'heuristic',
|
||||
logEstimationMismatch: false,
|
||||
mismatchThresholdPct: 15.0,
|
||||
monitorEnabled: false,
|
||||
monitorPath: './context.txt',
|
||||
monitorFormat: 'human',
|
||||
monitorFrequency: 'turn',
|
||||
monitorWriteMode: 'sync',
|
||||
redactSensitive: true,
|
||||
snapshotTiming: 'post_optimization',
|
||||
allowAbsolutePaths: false,
|
||||
...partial,
|
||||
};
|
||||
}
|
||||
@@ -1,367 +0,0 @@
|
||||
/**
|
||||
* Display Module for PraisonAI TypeScript SDK
|
||||
*
|
||||
* Python parity with praisonaiagents/display module
|
||||
*
|
||||
* Provides:
|
||||
* - Display callback system
|
||||
* - Console output helpers
|
||||
* - Flow display utilities
|
||||
*/
|
||||
|
||||
// ============================================================================
|
||||
// Display Callback Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Display callback function type.
|
||||
*/
|
||||
export type DisplayCallback = (message: string, context?: DisplayContext) => void;
|
||||
|
||||
/**
|
||||
* Async display callback function type.
|
||||
*/
|
||||
export type AsyncDisplayCallback = (message: string, context?: DisplayContext) => Promise<void>;
|
||||
|
||||
/**
|
||||
* Display context for callbacks.
|
||||
*/
|
||||
export interface DisplayContext {
|
||||
agentName?: string;
|
||||
toolName?: string;
|
||||
level?: 'info' | 'warning' | 'error' | 'debug' | 'trace';
|
||||
timestamp?: Date;
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Callback Registry
|
||||
// ============================================================================
|
||||
|
||||
const _syncCallbacks: DisplayCallback[] = [];
|
||||
const _asyncCallbacks: AsyncDisplayCallback[] = [];
|
||||
|
||||
/**
|
||||
* Register a display callback.
|
||||
* Python parity: praisonaiagents/display
|
||||
*/
|
||||
export function registerDisplayCallback(
|
||||
callback: DisplayCallback | AsyncDisplayCallback,
|
||||
async: boolean = false
|
||||
): void {
|
||||
if (async) {
|
||||
_asyncCallbacks.push(callback as AsyncDisplayCallback);
|
||||
} else {
|
||||
_syncCallbacks.push(callback as DisplayCallback);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sync display callbacks.
|
||||
* Python parity: praisonaiagents/display
|
||||
*/
|
||||
export function syncDisplayCallbacks(): DisplayCallback[] {
|
||||
return [..._syncCallbacks];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get async display callbacks.
|
||||
* Python parity: praisonaiagents/display
|
||||
*/
|
||||
export function asyncDisplayCallbacks(): AsyncDisplayCallback[] {
|
||||
return [..._asyncCallbacks];
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all display callbacks.
|
||||
*/
|
||||
export function clearDisplayCallbacks(): void {
|
||||
_syncCallbacks.length = 0;
|
||||
_asyncCallbacks.length = 0;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Display Functions
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Display an error message.
|
||||
* Python parity: praisonaiagents/display
|
||||
*/
|
||||
export function displayError(message: string, context?: DisplayContext): void {
|
||||
const ctx: DisplayContext = {
|
||||
...context,
|
||||
level: 'error',
|
||||
timestamp: new Date(),
|
||||
};
|
||||
|
||||
// Call sync callbacks
|
||||
for (const callback of _syncCallbacks) {
|
||||
callback(message, ctx);
|
||||
}
|
||||
|
||||
// Default console output
|
||||
console.error(`[ERROR] ${message}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a generating message.
|
||||
* Python parity: praisonaiagents/display
|
||||
*/
|
||||
export function displayGenerating(agentName: string, context?: DisplayContext): void {
|
||||
const message = `${agentName} is generating...`;
|
||||
const ctx: DisplayContext = {
|
||||
...context,
|
||||
agentName,
|
||||
level: 'info',
|
||||
timestamp: new Date(),
|
||||
};
|
||||
|
||||
for (const callback of _syncCallbacks) {
|
||||
callback(message, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display an instruction.
|
||||
* Python parity: praisonaiagents/display
|
||||
*/
|
||||
export function displayInstruction(instruction: string, context?: DisplayContext): void {
|
||||
const ctx: DisplayContext = {
|
||||
...context,
|
||||
level: 'info',
|
||||
timestamp: new Date(),
|
||||
};
|
||||
|
||||
for (const callback of _syncCallbacks) {
|
||||
callback(instruction, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display an interaction.
|
||||
* Python parity: praisonaiagents/display
|
||||
*/
|
||||
export function displayInteraction(
|
||||
agentName: string,
|
||||
input: string,
|
||||
output: string,
|
||||
context?: DisplayContext
|
||||
): void {
|
||||
const message = `[${agentName}] Input: ${input}\nOutput: ${output}`;
|
||||
const ctx: DisplayContext = {
|
||||
...context,
|
||||
agentName,
|
||||
level: 'info',
|
||||
timestamp: new Date(),
|
||||
};
|
||||
|
||||
for (const callback of _syncCallbacks) {
|
||||
callback(message, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display self-reflection.
|
||||
* Python parity: praisonaiagents/display
|
||||
*/
|
||||
export function displaySelfReflection(
|
||||
agentName: string,
|
||||
reflection: string,
|
||||
context?: DisplayContext
|
||||
): void {
|
||||
const message = `[${agentName}] Reflection: ${reflection}`;
|
||||
const ctx: DisplayContext = {
|
||||
...context,
|
||||
agentName,
|
||||
level: 'debug',
|
||||
timestamp: new Date(),
|
||||
};
|
||||
|
||||
for (const callback of _syncCallbacks) {
|
||||
callback(message, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a tool call.
|
||||
* Python parity: praisonaiagents/display
|
||||
*/
|
||||
export function displayToolCall(
|
||||
toolName: string,
|
||||
args: Record<string, any>,
|
||||
result?: any,
|
||||
context?: DisplayContext
|
||||
): void {
|
||||
const argsStr = JSON.stringify(args, null, 2);
|
||||
const resultStr = result !== undefined ? JSON.stringify(result, null, 2) : 'pending';
|
||||
const message = `[Tool: ${toolName}]\nArgs: ${argsStr}\nResult: ${resultStr}`;
|
||||
const ctx: DisplayContext = {
|
||||
...context,
|
||||
toolName,
|
||||
level: 'trace',
|
||||
timestamp: new Date(),
|
||||
};
|
||||
|
||||
for (const callback of _syncCallbacks) {
|
||||
callback(message, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Flow Display
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Flow display configuration.
|
||||
*/
|
||||
export interface FlowDisplayConfig {
|
||||
showTimestamps?: boolean;
|
||||
showAgentNames?: boolean;
|
||||
showToolCalls?: boolean;
|
||||
colorize?: boolean;
|
||||
maxWidth?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flow display class for visualizing agent workflows.
|
||||
* Python parity: praisonaiagents/display
|
||||
*/
|
||||
export class FlowDisplay {
|
||||
private config: FlowDisplayConfig;
|
||||
private steps: Array<{
|
||||
type: string;
|
||||
agent?: string;
|
||||
tool?: string;
|
||||
message: string;
|
||||
timestamp: Date;
|
||||
}> = [];
|
||||
|
||||
constructor(config: FlowDisplayConfig = {}) {
|
||||
this.config = {
|
||||
showTimestamps: true,
|
||||
showAgentNames: true,
|
||||
showToolCalls: true,
|
||||
colorize: true,
|
||||
maxWidth: 80,
|
||||
...config,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an agent step.
|
||||
*/
|
||||
addAgentStep(agentName: string, message: string): void {
|
||||
this.steps.push({
|
||||
type: 'agent',
|
||||
agent: agentName,
|
||||
message,
|
||||
timestamp: new Date(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a tool step.
|
||||
*/
|
||||
addToolStep(toolName: string, message: string): void {
|
||||
this.steps.push({
|
||||
type: 'tool',
|
||||
tool: toolName,
|
||||
message,
|
||||
timestamp: new Date(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a message step.
|
||||
*/
|
||||
addMessageStep(message: string): void {
|
||||
this.steps.push({
|
||||
type: 'message',
|
||||
message,
|
||||
timestamp: new Date(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the flow as text.
|
||||
*/
|
||||
render(): string {
|
||||
const lines: string[] = [];
|
||||
|
||||
for (const step of this.steps) {
|
||||
let line = '';
|
||||
|
||||
if (this.config.showTimestamps) {
|
||||
line += `[${step.timestamp.toISOString()}] `;
|
||||
}
|
||||
|
||||
if (step.type === 'agent' && this.config.showAgentNames) {
|
||||
line += `[Agent: ${step.agent}] `;
|
||||
} else if (step.type === 'tool' && this.config.showToolCalls) {
|
||||
line += `[Tool: ${step.tool}] `;
|
||||
}
|
||||
|
||||
line += step.message;
|
||||
|
||||
if (this.config.maxWidth && line.length > this.config.maxWidth) {
|
||||
line = line.substring(0, this.config.maxWidth - 3) + '...';
|
||||
}
|
||||
|
||||
lines.push(line);
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all steps.
|
||||
*/
|
||||
clear(): void {
|
||||
this.steps = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get step count.
|
||||
*/
|
||||
get stepCount(): number {
|
||||
return this.steps.length;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Error Logs
|
||||
// ============================================================================
|
||||
|
||||
const _errorLogs: Array<{
|
||||
message: string;
|
||||
timestamp: Date;
|
||||
context?: DisplayContext;
|
||||
}> = [];
|
||||
|
||||
/**
|
||||
* Get error logs.
|
||||
* Python parity: praisonaiagents/display
|
||||
*/
|
||||
export function errorLogs(): typeof _errorLogs {
|
||||
return [..._errorLogs];
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an error.
|
||||
*/
|
||||
export function logError(message: string, context?: DisplayContext): void {
|
||||
_errorLogs.push({
|
||||
message,
|
||||
timestamp: new Date(),
|
||||
context,
|
||||
});
|
||||
displayError(message, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear error logs.
|
||||
*/
|
||||
export function clearErrorLogs(): void {
|
||||
_errorLogs.length = 0;
|
||||
}
|
||||
@@ -1,218 +0,0 @@
|
||||
/**
|
||||
* Embeddings Module for PraisonAI TypeScript SDK
|
||||
*
|
||||
* Python parity with praisonaiagents embedding functions
|
||||
*
|
||||
* Provides:
|
||||
* - Embedding generation functions
|
||||
* - Async embedding variants
|
||||
* - Embedding result types
|
||||
*/
|
||||
|
||||
// ============================================================================
|
||||
// Embedding Result Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Embedding result.
|
||||
* Python parity: praisonaiagents/knowledge
|
||||
*/
|
||||
export interface EmbeddingResult {
|
||||
embedding: number[];
|
||||
model: string;
|
||||
dimensions: number;
|
||||
usage?: {
|
||||
promptTokens: number;
|
||||
totalTokens: number;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch embedding result.
|
||||
*/
|
||||
export interface BatchEmbeddingResult {
|
||||
embeddings: number[][];
|
||||
model: string;
|
||||
dimensions: number;
|
||||
usage?: {
|
||||
promptTokens: number;
|
||||
totalTokens: number;
|
||||
};
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Embedding Configuration
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Embedding configuration.
|
||||
*/
|
||||
export interface EmbeddingConfig {
|
||||
model?: string;
|
||||
dimensions?: number;
|
||||
apiKey?: string;
|
||||
baseUrl?: string;
|
||||
}
|
||||
|
||||
const DEFAULT_CONFIG: EmbeddingConfig = {
|
||||
model: 'text-embedding-3-small',
|
||||
dimensions: 1536,
|
||||
};
|
||||
|
||||
let _globalConfig: EmbeddingConfig = { ...DEFAULT_CONFIG };
|
||||
|
||||
/**
|
||||
* Set global embedding configuration.
|
||||
*/
|
||||
export function setEmbeddingConfig(config: Partial<EmbeddingConfig>): void {
|
||||
_globalConfig = { ..._globalConfig, ...config };
|
||||
}
|
||||
|
||||
/**
|
||||
* Get embedding dimensions for a model.
|
||||
* Python parity: praisonaiagents/knowledge
|
||||
*/
|
||||
export function getDimensions(model?: string): number {
|
||||
const modelName = model ?? _globalConfig.model ?? 'text-embedding-3-small';
|
||||
|
||||
const dimensionMap: Record<string, number> = {
|
||||
'text-embedding-3-small': 1536,
|
||||
'text-embedding-3-large': 3072,
|
||||
'text-embedding-ada-002': 1536,
|
||||
'embed-english-v3.0': 1024,
|
||||
'embed-multilingual-v3.0': 1024,
|
||||
};
|
||||
|
||||
return dimensionMap[modelName] ?? _globalConfig.dimensions ?? 1536;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Sync Embedding Functions
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Generate embedding for a single text.
|
||||
* Python parity: praisonaiagents/knowledge
|
||||
*/
|
||||
export function embed(text: string, config?: EmbeddingConfig): EmbeddingResult {
|
||||
const cfg = { ..._globalConfig, ...config };
|
||||
const dimensions = getDimensions(cfg.model);
|
||||
|
||||
// Placeholder implementation - would call OpenAI API
|
||||
// In production, this would use the LLM client
|
||||
const embedding = new Array(dimensions).fill(0).map(() => Math.random() * 2 - 1);
|
||||
|
||||
return {
|
||||
embedding,
|
||||
model: cfg.model ?? 'text-embedding-3-small',
|
||||
dimensions,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for embed.
|
||||
* Python parity: praisonaiagents/knowledge
|
||||
*/
|
||||
export function embedding(text: string, config?: EmbeddingConfig): EmbeddingResult {
|
||||
return embed(text, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate embeddings for multiple texts.
|
||||
* Python parity: praisonaiagents/knowledge
|
||||
*/
|
||||
export function embeddings(texts: string[], config?: EmbeddingConfig): BatchEmbeddingResult {
|
||||
const cfg = { ..._globalConfig, ...config };
|
||||
const dimensions = getDimensions(cfg.model);
|
||||
|
||||
// Placeholder implementation
|
||||
const embeddingsList = texts.map(() =>
|
||||
new Array(dimensions).fill(0).map(() => Math.random() * 2 - 1)
|
||||
);
|
||||
|
||||
return {
|
||||
embeddings: embeddingsList,
|
||||
model: cfg.model ?? 'text-embedding-3-small',
|
||||
dimensions,
|
||||
};
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Async Embedding Functions
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Async generate embedding for a single text.
|
||||
* Python parity: praisonaiagents/knowledge
|
||||
*/
|
||||
export async function aembed(text: string, config?: EmbeddingConfig): Promise<EmbeddingResult> {
|
||||
// In production, this would make async API call
|
||||
return embed(text, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for aembed.
|
||||
* Python parity: praisonaiagents/knowledge
|
||||
*/
|
||||
export async function aembedding(text: string, config?: EmbeddingConfig): Promise<EmbeddingResult> {
|
||||
return aembed(text, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Async generate embeddings for multiple texts.
|
||||
* Python parity: praisonaiagents/knowledge
|
||||
*/
|
||||
export async function aembeddings(texts: string[], config?: EmbeddingConfig): Promise<BatchEmbeddingResult> {
|
||||
// In production, this would make async API call
|
||||
return embeddings(texts, config);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Utility Functions
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Calculate cosine similarity between two embeddings.
|
||||
*/
|
||||
export function cosineSimilarity(a: number[], b: number[]): number {
|
||||
if (a.length !== b.length) {
|
||||
throw new Error('Embeddings must have same dimensions');
|
||||
}
|
||||
|
||||
let dotProduct = 0;
|
||||
let normA = 0;
|
||||
let normB = 0;
|
||||
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
dotProduct += a[i] * b[i];
|
||||
normA += a[i] * a[i];
|
||||
normB += b[i] * b[i];
|
||||
}
|
||||
|
||||
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate euclidean distance between two embeddings.
|
||||
*/
|
||||
export function euclideanDistance(a: number[], b: number[]): number {
|
||||
if (a.length !== b.length) {
|
||||
throw new Error('Embeddings must have same dimensions');
|
||||
}
|
||||
|
||||
let sum = 0;
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
const diff = a[i] - b[i];
|
||||
sum += diff * diff;
|
||||
}
|
||||
|
||||
return Math.sqrt(sum);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize an embedding vector.
|
||||
*/
|
||||
export function normalizeEmbedding(embedding: number[]): number[] {
|
||||
const norm = Math.sqrt(embedding.reduce((sum, val) => sum + val * val, 0));
|
||||
return embedding.map(val => val / norm);
|
||||
}
|
||||
@@ -1,450 +0,0 @@
|
||||
/**
|
||||
* Gateway/Bot Module for PraisonAI TypeScript SDK
|
||||
*
|
||||
* Python parity with praisonaiagents gateway/bot types
|
||||
*
|
||||
* Provides:
|
||||
* - Bot protocols and interfaces
|
||||
* - Gateway protocols and interfaces
|
||||
* - Message types
|
||||
*/
|
||||
|
||||
// ============================================================================
|
||||
// Bot Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Bot configuration.
|
||||
* Python parity: praisonaiagents/bots
|
||||
*/
|
||||
export interface BotConfig {
|
||||
name: string;
|
||||
token?: string;
|
||||
prefix?: string;
|
||||
channels?: string[];
|
||||
allowedUsers?: string[];
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bot user.
|
||||
* Python parity: praisonaiagents/bots
|
||||
*/
|
||||
export interface BotUser {
|
||||
id: string;
|
||||
name: string;
|
||||
displayName?: string;
|
||||
isBot?: boolean;
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bot channel.
|
||||
* Python parity: praisonaiagents/bots
|
||||
*/
|
||||
export interface BotChannel {
|
||||
id: string;
|
||||
name: string;
|
||||
type: 'text' | 'voice' | 'dm' | 'group';
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bot message.
|
||||
* Python parity: praisonaiagents/bots
|
||||
*/
|
||||
export interface BotMessage {
|
||||
id: string;
|
||||
content: string;
|
||||
author: BotUser;
|
||||
channel: BotChannel;
|
||||
timestamp: Date;
|
||||
attachments?: Array<{
|
||||
url: string;
|
||||
type: string;
|
||||
name?: string;
|
||||
}>;
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bot protocol interface.
|
||||
* Python parity: praisonaiagents/bots
|
||||
*/
|
||||
export interface BotProtocol {
|
||||
name: string;
|
||||
config: BotConfig;
|
||||
|
||||
connect(): Promise<void>;
|
||||
disconnect(): Promise<void>;
|
||||
|
||||
sendMessage(channel: string, content: string): Promise<BotMessage>;
|
||||
onMessage(handler: (message: BotMessage) => void | Promise<void>): void;
|
||||
|
||||
getUser(userId: string): Promise<BotUser | null>;
|
||||
getChannel(channelId: string): Promise<BotChannel | null>;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Gateway Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Gateway configuration.
|
||||
* Python parity: praisonaiagents/gateway
|
||||
*/
|
||||
export interface GatewayConfig {
|
||||
url: string;
|
||||
apiKey?: string;
|
||||
timeout?: number;
|
||||
retryAttempts?: number;
|
||||
retryDelay?: number;
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gateway event.
|
||||
* Python parity: praisonaiagents/gateway
|
||||
*/
|
||||
export interface GatewayEvent {
|
||||
type: string;
|
||||
data: any;
|
||||
timestamp: Date;
|
||||
source?: string;
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gateway message.
|
||||
* Python parity: praisonaiagents/gateway
|
||||
*/
|
||||
export interface GatewayMessage {
|
||||
id: string;
|
||||
type: 'request' | 'response' | 'event' | 'error';
|
||||
payload: any;
|
||||
timestamp: Date;
|
||||
correlationId?: string;
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gateway protocol interface.
|
||||
* Python parity: praisonaiagents/gateway
|
||||
*/
|
||||
export interface GatewayProtocol {
|
||||
config: GatewayConfig;
|
||||
|
||||
connect(): Promise<void>;
|
||||
disconnect(): Promise<void>;
|
||||
isConnected(): boolean;
|
||||
|
||||
send(message: GatewayMessage): Promise<void>;
|
||||
receive(): Promise<GatewayMessage | null>;
|
||||
|
||||
onEvent(handler: (event: GatewayEvent) => void | Promise<void>): void;
|
||||
onError(handler: (error: Error) => void): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gateway client protocol.
|
||||
* Python parity: praisonaiagents/gateway
|
||||
*/
|
||||
export interface GatewayClientProtocol extends GatewayProtocol {
|
||||
request(payload: any, timeout?: number): Promise<any>;
|
||||
subscribe(eventType: string, handler: (event: GatewayEvent) => void): () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gateway session protocol.
|
||||
* Python parity: praisonaiagents/gateway
|
||||
*/
|
||||
export interface GatewaySessionProtocol {
|
||||
sessionId: string;
|
||||
userId?: string;
|
||||
startTime: Date;
|
||||
metadata?: Record<string, any>;
|
||||
|
||||
isActive(): boolean;
|
||||
extend(duration: number): void;
|
||||
terminate(): void;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Provider Status
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Provider status.
|
||||
* Python parity: praisonaiagents/gateway
|
||||
*/
|
||||
export interface ProviderStatus {
|
||||
name: string;
|
||||
status: 'online' | 'offline' | 'degraded' | 'unknown';
|
||||
latency?: number;
|
||||
lastCheck: Date;
|
||||
errorCount?: number;
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Failover Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Failover configuration.
|
||||
* Python parity: praisonaiagents/gateway
|
||||
*/
|
||||
export interface FailoverConfig {
|
||||
providers: string[];
|
||||
strategy: 'round-robin' | 'priority' | 'random' | 'least-latency';
|
||||
maxRetries?: number;
|
||||
retryDelay?: number;
|
||||
healthCheckInterval?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Failover manager.
|
||||
* Python parity: praisonaiagents/gateway
|
||||
*/
|
||||
export class FailoverManager {
|
||||
private config: FailoverConfig;
|
||||
private providerStatuses: Map<string, ProviderStatus> = new Map();
|
||||
private currentIndex: number = 0;
|
||||
|
||||
constructor(config: FailoverConfig) {
|
||||
this.config = config;
|
||||
|
||||
// Initialize provider statuses
|
||||
for (const provider of config.providers) {
|
||||
this.providerStatuses.set(provider, {
|
||||
name: provider,
|
||||
status: 'unknown',
|
||||
lastCheck: new Date(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the next provider based on strategy.
|
||||
*/
|
||||
getNextProvider(): string | null {
|
||||
const availableProviders = this.config.providers.filter(p => {
|
||||
const status = this.providerStatuses.get(p);
|
||||
return status?.status !== 'offline';
|
||||
});
|
||||
|
||||
if (availableProviders.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (this.config.strategy) {
|
||||
case 'round-robin':
|
||||
this.currentIndex = (this.currentIndex + 1) % availableProviders.length;
|
||||
return availableProviders[this.currentIndex];
|
||||
|
||||
case 'priority':
|
||||
return availableProviders[0];
|
||||
|
||||
case 'random':
|
||||
return availableProviders[Math.floor(Math.random() * availableProviders.length)];
|
||||
|
||||
case 'least-latency':
|
||||
let minLatency = Infinity;
|
||||
let bestProvider = availableProviders[0];
|
||||
for (const provider of availableProviders) {
|
||||
const status = this.providerStatuses.get(provider);
|
||||
if (status?.latency !== undefined && status.latency < minLatency) {
|
||||
minLatency = status.latency;
|
||||
bestProvider = provider;
|
||||
}
|
||||
}
|
||||
return bestProvider;
|
||||
|
||||
default:
|
||||
return availableProviders[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update provider status.
|
||||
*/
|
||||
updateStatus(provider: string, status: Partial<ProviderStatus>): void {
|
||||
const current = this.providerStatuses.get(provider);
|
||||
if (current) {
|
||||
this.providerStatuses.set(provider, {
|
||||
...current,
|
||||
...status,
|
||||
lastCheck: new Date(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark provider as failed.
|
||||
*/
|
||||
markFailed(provider: string): void {
|
||||
this.updateStatus(provider, {
|
||||
status: 'offline',
|
||||
errorCount: (this.providerStatuses.get(provider)?.errorCount ?? 0) + 1,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark provider as healthy.
|
||||
*/
|
||||
markHealthy(provider: string, latency?: number): void {
|
||||
this.updateStatus(provider, {
|
||||
status: 'online',
|
||||
latency,
|
||||
errorCount: 0,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all provider statuses.
|
||||
*/
|
||||
getStatuses(): ProviderStatus[] {
|
||||
return Array.from(this.providerStatuses.values());
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Auth Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Auth profile.
|
||||
* Python parity: praisonaiagents/auth
|
||||
*/
|
||||
export interface AuthProfile {
|
||||
userId: string;
|
||||
username?: string;
|
||||
email?: string;
|
||||
roles?: string[];
|
||||
permissions?: string[];
|
||||
token?: string;
|
||||
expiresAt?: Date;
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Resource Limits
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Resource limits.
|
||||
* Python parity: praisonaiagents/limits
|
||||
*/
|
||||
export interface ResourceLimits {
|
||||
maxTokens?: number;
|
||||
maxRequests?: number;
|
||||
maxConcurrent?: number;
|
||||
rateLimitPerMinute?: number;
|
||||
rateLimitPerHour?: number;
|
||||
maxMemoryMB?: number;
|
||||
maxExecutionTimeMs?: number;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Sandbox Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Sandbox status.
|
||||
* Python parity: praisonaiagents/sandbox
|
||||
*/
|
||||
export enum SandboxStatus {
|
||||
IDLE = 'idle',
|
||||
RUNNING = 'running',
|
||||
COMPLETED = 'completed',
|
||||
FAILED = 'failed',
|
||||
TIMEOUT = 'timeout',
|
||||
}
|
||||
|
||||
/**
|
||||
* Sandbox result.
|
||||
* Python parity: praisonaiagents/sandbox
|
||||
*/
|
||||
export interface SandboxResult {
|
||||
status: SandboxStatus;
|
||||
output?: string;
|
||||
error?: string;
|
||||
exitCode?: number;
|
||||
duration?: number;
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sandbox protocol.
|
||||
* Python parity: praisonaiagents/sandbox
|
||||
*/
|
||||
export interface SandboxProtocol {
|
||||
execute(code: string, language?: string): Promise<SandboxResult>;
|
||||
executeFile(filePath: string): Promise<SandboxResult>;
|
||||
terminate(): Promise<void>;
|
||||
getStatus(): SandboxStatus;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Autonomy Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Autonomy level.
|
||||
* Python parity: praisonaiagents/autonomy
|
||||
*/
|
||||
export enum AutonomyLevel {
|
||||
NONE = 'none',
|
||||
SUGGEST = 'suggest',
|
||||
AUTO_APPROVE = 'auto_approve',
|
||||
FULL_AUTO = 'full_auto',
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Reflection Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Reflection output.
|
||||
* Python parity: praisonaiagents/reflection
|
||||
*/
|
||||
export interface ReflectionOutput {
|
||||
originalOutput: string;
|
||||
reflectedOutput: string;
|
||||
iterations: number;
|
||||
improvements: string[];
|
||||
score?: number;
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// RAG Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* RAG retrieval policy.
|
||||
* Python parity: praisonaiagents/rag
|
||||
*/
|
||||
export enum RagRetrievalPolicy {
|
||||
SIMILARITY = 'similarity',
|
||||
MMR = 'mmr',
|
||||
HYBRID = 'hybrid',
|
||||
RERANK = 'rerank',
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Auto RAG Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Auto RAG configuration.
|
||||
* Python parity: praisonaiagents/knowledge
|
||||
*/
|
||||
export interface AutoRagConfig {
|
||||
sources: string[];
|
||||
chunkSize?: number;
|
||||
chunkOverlap?: number;
|
||||
topK?: number;
|
||||
rerank?: boolean;
|
||||
retrievalPolicy?: RagRetrievalPolicy;
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
/**
|
||||
* Guardrails - Input/output validation and safety checks
|
||||
*
|
||||
* Python parity with praisonaiagents/guardrails module
|
||||
*/
|
||||
|
||||
export type GuardrailStatus = 'passed' | 'failed' | 'warning';
|
||||
@@ -13,64 +11,6 @@ export interface GuardrailResult {
|
||||
modifiedContent?: string;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// GuardrailValidationResult (Python parity)
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Result of a guardrail validation.
|
||||
*
|
||||
* Python parity: praisonaiagents/guardrails/guardrail_result.py:13-43
|
||||
*
|
||||
* This is the Pydantic-style result that matches the Python SDK.
|
||||
*/
|
||||
export interface GuardrailValidationResult {
|
||||
/** Whether the guardrail check passed */
|
||||
success: boolean;
|
||||
/** The result if modified, or null if unchanged */
|
||||
result: string | null;
|
||||
/** Error message if validation failed */
|
||||
error: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a GuardrailValidationResult with defaults.
|
||||
*/
|
||||
export function createGuardrailValidationResult(
|
||||
config: Partial<GuardrailValidationResult> = {}
|
||||
): GuardrailValidationResult {
|
||||
return {
|
||||
success: config.success ?? false,
|
||||
result: config.result ?? null,
|
||||
error: config.error ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a GuardrailValidationResult from a tuple.
|
||||
*
|
||||
* Python parity: praisonaiagents/guardrails/guardrail_result.py:20-43
|
||||
*/
|
||||
export function guardrailResultFromTuple(
|
||||
tuple: [boolean, string | null]
|
||||
): GuardrailValidationResult {
|
||||
const [success, data] = tuple;
|
||||
|
||||
if (success) {
|
||||
return {
|
||||
success: true,
|
||||
result: data,
|
||||
error: '',
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
result: null,
|
||||
error: data ?? 'Guardrail validation failed',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export type GuardrailFunction = (
|
||||
content: string,
|
||||
context?: GuardrailContext
|
||||
|
||||
@@ -128,19 +128,6 @@ export * from './session';
|
||||
// ============================================================================
|
||||
export * from './knowledge';
|
||||
|
||||
// RAG Module (Python parity with praisonaiagents/rag)
|
||||
export {
|
||||
RAG, createRAG,
|
||||
RetrievalStrategy, type RetrievalStrategyType,
|
||||
type Citation as RAGCitation, createCitation, formatCitation,
|
||||
type ContextPack as RAGContextPack, createContextPack, hasCitations, formatContextPackForPrompt,
|
||||
type RAGResult, createRAGResult, formatAnswerWithCitations,
|
||||
type RAGConfig, DEFAULT_RAG_TEMPLATE, createRAGConfig,
|
||||
RetrievalPolicy, type RetrievalPolicyType,
|
||||
CitationsMode, type CitationsModeType,
|
||||
type RetrievalConfig, createRetrievalConfig, createSimpleRetrievalConfig, createSmartRetrievalConfig,
|
||||
} from './rag';
|
||||
|
||||
// ============================================================================
|
||||
// CONTEXT MANAGEMENT
|
||||
// ============================================================================
|
||||
@@ -172,15 +159,8 @@ export * from './process';
|
||||
// Export guardrails
|
||||
export * from './guardrails';
|
||||
|
||||
// Export handoff (Python parity)
|
||||
export {
|
||||
Handoff, handoff, handoffFilters,
|
||||
RECOMMENDED_PROMPT_PREFIX, promptWithHandoffInstructions,
|
||||
HandoffError, HandoffCycleError, HandoffDepthError, HandoffTimeoutError,
|
||||
ContextPolicy,
|
||||
type HandoffConfig, type HandoffContext, type HandoffResult,
|
||||
type HandoffInputData, type ContextPolicyType
|
||||
} from './agent/handoff';
|
||||
// Export handoff
|
||||
export { Handoff, handoff, handoffFilters, type HandoffConfig, type HandoffContext, type HandoffResult } from './agent/handoff';
|
||||
|
||||
// Export router agent
|
||||
export { RouterAgent, createRouter, routeConditions, type RouterConfig, type RouteConfig, type RouteContext } from './agent/router';
|
||||
@@ -208,14 +188,8 @@ export {
|
||||
|
||||
// Note: Observability exports are at the bottom of this file with the full 14+ integrations
|
||||
|
||||
// Export skills (Python parity with praisonaiagents/skills)
|
||||
export {
|
||||
SkillManager, createSkillManager, parseSkillFile,
|
||||
// Python parity additions
|
||||
SkillLoader, createSkillLoader,
|
||||
type Skill, type SkillMetadata, type SkillDiscoveryOptions,
|
||||
type SkillProperties, createSkillProperties,
|
||||
} from './skills';
|
||||
// Export skills
|
||||
export { SkillManager, createSkillManager, parseSkillFile, type Skill, type SkillMetadata, type SkillDiscoveryOptions } from './skills';
|
||||
|
||||
// Export CLI
|
||||
export { parseArgs, executeCommand, CLI_SPEC_VERSION } from './cli';
|
||||
@@ -287,16 +261,13 @@ export {
|
||||
type WorkflowHooksConfig, type WorkflowRef, type StepContext
|
||||
} from './hooks';
|
||||
|
||||
// Export Telemetry (Agent-focused) - Python parity with praisonaiagents/telemetry
|
||||
// Export Telemetry (Agent-focused)
|
||||
|
||||
export {
|
||||
TelemetryCollector, AgentTelemetry,
|
||||
getTelemetry, enableTelemetry, disableTelemetry, cleanupTelemetry, createAgentTelemetry,
|
||||
PerformanceMonitor, createPerformanceMonitor,
|
||||
TelemetryIntegration, createTelemetryIntegration, createConsoleSink, createHTTPSink,
|
||||
// Python parity additions
|
||||
MinimalTelemetry, getMinimalTelemetry,
|
||||
enablePerformanceMode, disablePerformanceMode, cleanupTelemetryResources,
|
||||
type TelemetryEvent, type TelemetryConfig, type AgentStats,
|
||||
type MetricEntry, type PerformanceStats, type PerformanceMonitorConfig,
|
||||
type TelemetryRecord, type TelemetrySink
|
||||
@@ -332,14 +303,10 @@ export { PromptExpanderAgent, createPromptExpanderAgent, type PromptExpanderConf
|
||||
// Export LLMGuardrail
|
||||
export { LLMGuardrail, createLLMGuardrail, type LLMGuardrailConfig, type LLMGuardrailResult } from './guardrails/llm-guardrail';
|
||||
|
||||
// Export Planning (simplified API) - Python parity with praisonaiagents/planning
|
||||
// Export Planning (simplified API)
|
||||
export {
|
||||
// Core classes
|
||||
Plan, PlanStep, TodoList, TodoItem, PlanStorage,
|
||||
PlanningAgent, TaskAgent,
|
||||
// Python parity additions
|
||||
ApprovalCallback, createApprovalCallback, type ApprovalCallbackConfig,
|
||||
READ_ONLY_TOOLS, RESTRICTED_TOOLS, RESEARCH_TOOLS,
|
||||
createPlan, createTodoList, createPlanStorage, createPlanningAgent, createTaskAgent,
|
||||
type PlanConfig, type PlanStepConfig, type TodoItemConfig, type PlanStatus, type TodoStatus,
|
||||
type PlanningAgentConfig, type PlanResult
|
||||
@@ -348,38 +315,6 @@ export {
|
||||
// Export Cache
|
||||
export { BaseCache, MemoryCache, FileCache, createMemoryCache, createFileCache, type CacheConfig, type CacheEntry } from './cache';
|
||||
|
||||
// Export Config Module (Python parity with praisonaiagents/config)
|
||||
export {
|
||||
// Enums
|
||||
MemoryBackend, ChunkingStrategy, GuardrailAction, WebSearchProvider,
|
||||
OutputPreset, ExecutionPreset, ArrayMode,
|
||||
// Config interfaces (aliased to avoid conflicts with existing exports)
|
||||
type MemoryConfig as FeatureMemoryConfig,
|
||||
type LearnConfig, type KnowledgeConfig, type PlanningConfig,
|
||||
type MultiAgentPlanningConfig, type ReflectionConfig, type GuardrailConfig,
|
||||
type WebConfig, type OutputConfig, type ExecutionConfig, type TemplateConfig,
|
||||
type CachingConfig, type HooksConfig, type SkillsConfig, type SessionConfig,
|
||||
type DefaultsConfig, type PluginsConfig, type PraisonConfig,
|
||||
type MultiAgentExecutionConfig, type MultiAgentHooksConfig,
|
||||
type MultiAgentMemoryConfig, type MultiAgentOutputConfig,
|
||||
// Presets
|
||||
MEMORY_PRESETS, MEMORY_URL_SCHEMES, OUTPUT_PRESETS, EXECUTION_PRESETS,
|
||||
WEB_PRESETS, PLANNING_PRESETS, REFLECTION_PRESETS, GUARDRAIL_PRESETS,
|
||||
CONTEXT_PRESETS, AUTONOMY_PRESETS, CACHING_PRESETS,
|
||||
MULTI_AGENT_OUTPUT_PRESETS, MULTI_AGENT_EXECUTION_PRESETS, KNOWLEDGE_PRESETS,
|
||||
// Resolver functions
|
||||
resolve, resolve_memory, resolve_output, resolve_execution, resolve_web,
|
||||
resolve_planning, resolve_reflection, resolve_knowledge, resolve_context,
|
||||
resolve_autonomy, resolve_caching, resolve_hooks, resolve_skills,
|
||||
resolve_routing, resolve_guardrails,
|
||||
// Parse utilities
|
||||
detect_url_scheme, is_path_like, suggest_similar, clean_triple_backticks,
|
||||
is_policy_string, parse_policy_string, validate_config, apply_config_defaults,
|
||||
get_config, get_config_path, get_default, get_defaults_config, get_plugins_config,
|
||||
// Errors
|
||||
ConfigValidationError,
|
||||
} from './config';
|
||||
|
||||
// Export Events
|
||||
export { PubSub, EventEmitterPubSub, AgentEventBus, AgentEvents, createEventBus, createPubSub, type Event, type EventHandler } from './events';
|
||||
|
||||
@@ -582,12 +517,9 @@ export {
|
||||
// N8N Integration
|
||||
N8NIntegration, createN8NIntegration, triggerN8NWebhook,
|
||||
type N8NConfig, type N8NWebhookPayload, type N8NWorkflow, type N8NWorkflowNode,
|
||||
// Fast Context (Python parity with praisonaiagents/context/fast)
|
||||
// Fast Context
|
||||
FastContext, createFastContext, getQuickContext,
|
||||
type FastContextConfig, type ContextSource, type FastContextResult,
|
||||
// Python parity additions
|
||||
type LineRange, createLineRange, getLineCount, rangesOverlap, mergeRanges,
|
||||
type FileMatch, createFileMatch, addLineRangeToFileMatch, getTotalLines
|
||||
type FastContextConfig, type ContextSource, type FastContextResult
|
||||
} from './cli/features';
|
||||
|
||||
// ============================================================================
|
||||
@@ -817,202 +749,4 @@ export {
|
||||
type ComputerUseTools,
|
||||
type ComputerAction,
|
||||
type ScreenshotResult,
|
||||
} from './integrations/computer-use';
|
||||
|
||||
// Export Plugin Module (Python parity with praisonaiagents/plugins)
|
||||
export {
|
||||
// Enums
|
||||
PluginHook, PluginType,
|
||||
// Interfaces
|
||||
type PluginMetadata, type PluginInfo, type PluginProtocol,
|
||||
type ToolPluginProtocol, type HookPluginProtocol,
|
||||
type AgentPluginProtocol, type LLMPluginProtocol,
|
||||
// Classes
|
||||
Plugin, FunctionPlugin, PluginManager, PluginParseError,
|
||||
// Functions
|
||||
getPluginManager, getDefaultPluginDirs, ensurePluginDir,
|
||||
discoverPlugins, loadPlugin, discoverAndLoadPlugins, getPluginTemplate,
|
||||
parsePluginHeader, parsePluginHeaderFromFile,
|
||||
enable as enablePlugins, disable as disablePlugins,
|
||||
listPlugins, isEnabled as isPluginEnabled,
|
||||
} from './plugins';
|
||||
|
||||
// Export Display Module (Python parity with praisonaiagents/display)
|
||||
export {
|
||||
// Types
|
||||
type DisplayCallback, type AsyncDisplayCallback, type DisplayContext,
|
||||
type FlowDisplayConfig as DisplayFlowConfig,
|
||||
// Functions
|
||||
registerDisplayCallback as registerDisplay, syncDisplayCallbacks, asyncDisplayCallbacks,
|
||||
clearDisplayCallbacks, displayError, displayGenerating, displayInstruction,
|
||||
displayInteraction, displaySelfReflection, displayToolCall,
|
||||
errorLogs, logError, clearErrorLogs,
|
||||
// Classes
|
||||
FlowDisplay as DisplayFlow,
|
||||
} from './display';
|
||||
|
||||
// Export Embeddings Module (Python parity with praisonaiagents/knowledge)
|
||||
export {
|
||||
// Types
|
||||
type EmbeddingResult, type BatchEmbeddingResult, type EmbeddingConfig,
|
||||
// Functions
|
||||
embed, embedding, embeddings, aembed, aembedding, aembeddings,
|
||||
getDimensions, setEmbeddingConfig,
|
||||
cosineSimilarity, euclideanDistance, normalizeEmbedding,
|
||||
} from './embeddings';
|
||||
|
||||
// Export Trace Module (Python parity with praisonaiagents/trace)
|
||||
export {
|
||||
// Enums
|
||||
ContextEventType, EventType, MessageType,
|
||||
// Types
|
||||
type ContextEvent, type TraceSinkProtocol, type ContextTraceSinkProtocol,
|
||||
type TraceContext as TraceCtx,
|
||||
// Classes
|
||||
TraceSink, ContextTraceSink, ContextListSink, ContextNoOpSink,
|
||||
ContextTraceEmitter,
|
||||
// Functions
|
||||
createContextEvent, traceContext, trackWorkflow,
|
||||
} from './trace';
|
||||
|
||||
// Export Conditions Module (Python parity with praisonaiagents/conditions)
|
||||
export {
|
||||
// Types
|
||||
type ConditionProtocol, type RoutingConditionProtocol,
|
||||
// Classes
|
||||
DictCondition, ExpressionCondition, FunctionCondition,
|
||||
// Functions
|
||||
evaluateCondition, createCondition, andConditions, orConditions, notCondition,
|
||||
} from './conditions';
|
||||
|
||||
// Export Gateway/Bot Module (Python parity with praisonaiagents/gateway)
|
||||
export {
|
||||
// Bot types
|
||||
type BotConfig, type BotUser, type BotChannel, type BotMessage, type BotProtocol,
|
||||
// Gateway types
|
||||
type GatewayConfig, type GatewayEvent, type GatewayMessage,
|
||||
type GatewayProtocol, type GatewayClientProtocol, type GatewaySessionProtocol,
|
||||
// Other types
|
||||
type ProviderStatus, type FailoverConfig, type AuthProfile,
|
||||
type ResourceLimits, type SandboxResult, type SandboxProtocol,
|
||||
type ReflectionOutput, type AutoRagConfig,
|
||||
// Enums
|
||||
SandboxStatus, AutonomyLevel, RagRetrievalPolicy,
|
||||
// Classes
|
||||
FailoverManager,
|
||||
} from './gateway';
|
||||
|
||||
// Export Task Module (Python parity with praisonaiagents/task)
|
||||
export {
|
||||
type TaskConfig as AgentTaskConfig, type TaskOutput, type Task as AgentTask,
|
||||
BaseTask, createTaskOutput,
|
||||
} from './task';
|
||||
|
||||
// Export Protocols Module (Python parity with remaining P3 gaps)
|
||||
export {
|
||||
// A2A Protocol
|
||||
A2ATaskState, A2ARole,
|
||||
type A2ATextPart, type A2AFilePart, type A2ADataPart, type A2APart,
|
||||
type A2AMessage, type A2ATaskStatus, type A2ATask, type A2AArtifact,
|
||||
type A2AAgentSkill, type A2AAgentCapabilities, type A2AAgentCard,
|
||||
type A2ASendMessageRequest,
|
||||
A2A,
|
||||
// AGUI Protocol
|
||||
AGUI,
|
||||
// AutoRagAgent
|
||||
RetrievalPolicy as AutoRetrievalPolicy,
|
||||
type AutoRagAgentConfig,
|
||||
DEFAULT_AUTO_KEYWORDS,
|
||||
AutoRagAgent,
|
||||
// Tools class
|
||||
type ToolDefinition,
|
||||
Tools,
|
||||
// Global singletons
|
||||
config, memory, obs, workflows,
|
||||
// Guardrail policies
|
||||
type GuardrailPolicy,
|
||||
resolveGuardrailPolicies,
|
||||
GUARDRAIL_POLICY_PRESETS,
|
||||
// AgentManager alias type
|
||||
type AgentManager,
|
||||
} from './protocols';
|
||||
|
||||
// Export Parity Module (All remaining P0-P3 gaps for full Python SDK parity)
|
||||
// Only export items that don't conflict with existing exports from other modules
|
||||
export {
|
||||
// P0: Specialized Agent Configs (new)
|
||||
type AudioConfig,
|
||||
type CodeConfig,
|
||||
type OCRConfig,
|
||||
type VisionConfig,
|
||||
type VideoConfig,
|
||||
type RealtimeConfig,
|
||||
// P0: Specialized Agents (new)
|
||||
CodeAgent,
|
||||
OCRAgent,
|
||||
VisionAgent,
|
||||
VideoAgent,
|
||||
RealtimeAgent,
|
||||
EmbeddingAgent,
|
||||
// P0: Call Types (new)
|
||||
type MCPCall,
|
||||
type WebSearchCall,
|
||||
type FileSearchCall,
|
||||
type CodeExecutionStep,
|
||||
type DeepResearchResponse,
|
||||
type Provider,
|
||||
// P0: Handoff Functions (new)
|
||||
createContextAgent as create_context_agent,
|
||||
handoffFilters as handoff_filters,
|
||||
promptWithHandoffInstructions as prompt_with_handoff_instructions,
|
||||
// P1: Workflow Patterns (new)
|
||||
Chunking,
|
||||
If,
|
||||
when,
|
||||
Knowledge,
|
||||
Parallel,
|
||||
Route,
|
||||
Session,
|
||||
// P2: Context Types (new - only items not already exported)
|
||||
ContextManager,
|
||||
MCP,
|
||||
type ManagerConfig,
|
||||
type OptimizerStrategy,
|
||||
type ContextPack,
|
||||
type GuardrailResult,
|
||||
type ContextConfig,
|
||||
// P2: Telemetry Functions (new)
|
||||
enableTelemetry as enable_telemetry,
|
||||
disableTelemetry as disable_telemetry,
|
||||
getTelemetry as get_telemetry,
|
||||
enablePerformanceMode as enable_performance_mode,
|
||||
disablePerformanceMode as disable_performance_mode,
|
||||
cleanupTelemetryResources as cleanup_telemetry_resources,
|
||||
// P3: Display callbacks (snake_case for Python parity)
|
||||
register_display_callback,
|
||||
sync_display_callbacks,
|
||||
async_display_callbacks,
|
||||
display_error,
|
||||
display_generating,
|
||||
display_instruction,
|
||||
display_interaction,
|
||||
display_self_reflection,
|
||||
display_tool_call,
|
||||
error_logs,
|
||||
// P3: Plugin functions
|
||||
get_plugin_manager,
|
||||
get_default_plugin_dirs,
|
||||
ensure_plugin_dir,
|
||||
get_plugin_template,
|
||||
load_plugin,
|
||||
parse_plugin_header,
|
||||
parse_plugin_header_from_file,
|
||||
discover_plugins,
|
||||
discover_and_load_plugins,
|
||||
// P3: Trace & condition functions
|
||||
evaluate_condition,
|
||||
get_dimensions,
|
||||
track_workflow,
|
||||
resolve_guardrail_policies,
|
||||
trace_context,
|
||||
} from './parity';
|
||||
} from './integrations/computer-use';
|
||||
@@ -1,225 +1,7 @@
|
||||
/**
|
||||
* Planning System - Plans, Steps, and TodoLists
|
||||
*
|
||||
* Python parity with praisonaiagents/planning module
|
||||
*/
|
||||
|
||||
// ============================================================================
|
||||
// READ_ONLY_TOOLS & RESTRICTED_TOOLS (Python parity)
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Read-only tools that are allowed in plan mode.
|
||||
* These tools only read data and don't modify anything.
|
||||
*
|
||||
* Python parity: praisonaiagents/planning/__init__.py:38-52
|
||||
*/
|
||||
export const READ_ONLY_TOOLS: readonly string[] = [
|
||||
'read_file',
|
||||
'list_directory',
|
||||
'search_codebase',
|
||||
'search_files',
|
||||
'grep_search',
|
||||
'find_files',
|
||||
'web_search',
|
||||
'get_file_content',
|
||||
'list_files',
|
||||
'read_document',
|
||||
'search_web',
|
||||
'fetch_url',
|
||||
'get_context',
|
||||
] as const;
|
||||
|
||||
/**
|
||||
* Restricted tools that are blocked in plan mode.
|
||||
* These tools can modify files, execute commands, or have side effects.
|
||||
*
|
||||
* Python parity: praisonaiagents/planning/__init__.py:55-73
|
||||
*/
|
||||
export const RESTRICTED_TOOLS: readonly string[] = [
|
||||
'write_file',
|
||||
'create_file',
|
||||
'delete_file',
|
||||
'execute_command',
|
||||
'run_command',
|
||||
'shell_command',
|
||||
'modify_file',
|
||||
'edit_file',
|
||||
'remove_file',
|
||||
'move_file',
|
||||
'copy_file',
|
||||
'mkdir',
|
||||
'rmdir',
|
||||
'git_commit',
|
||||
'git_push',
|
||||
'npm_install',
|
||||
'pip_install',
|
||||
] as const;
|
||||
|
||||
/**
|
||||
* Research tools that are safe for planning research.
|
||||
*
|
||||
* Python parity: praisonaiagents/planning/__init__.py:76-90
|
||||
*/
|
||||
export const RESEARCH_TOOLS: readonly string[] = [
|
||||
'web_search',
|
||||
'search_web',
|
||||
'duckduckgo_search',
|
||||
'tavily_search',
|
||||
'brave_search',
|
||||
'google_search',
|
||||
'read_url',
|
||||
'fetch_url',
|
||||
'read_file',
|
||||
'list_directory',
|
||||
'search_codebase',
|
||||
'grep_search',
|
||||
'find_files',
|
||||
] as const;
|
||||
|
||||
// ============================================================================
|
||||
// ApprovalCallback (Python parity)
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Configuration for ApprovalCallback.
|
||||
*
|
||||
* Python parity: praisonaiagents/planning/approval.py:27-58
|
||||
*/
|
||||
export interface ApprovalCallbackConfig {
|
||||
/** Whether to automatically approve all plans */
|
||||
autoApprove?: boolean;
|
||||
/** Custom approval function (sync or async) */
|
||||
approveFn?: (plan: Plan) => boolean | Promise<boolean>;
|
||||
/** Callback when plan is rejected */
|
||||
onReject?: (plan: Plan) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for plan approval flow.
|
||||
*
|
||||
* Handles the approval process for plans before execution.
|
||||
* Can be configured for auto-approval or custom approval logic.
|
||||
*
|
||||
* Python parity: praisonaiagents/planning/approval.py:27-131
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // Auto-approve all plans
|
||||
* const callback = new ApprovalCallback({ autoApprove: true });
|
||||
*
|
||||
* // Custom approval function
|
||||
* const callback = new ApprovalCallback({
|
||||
* approveFn: (plan) => plan.steps.length <= 5
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
export class ApprovalCallback {
|
||||
private autoApprove: boolean;
|
||||
private approveFn?: (plan: Plan) => boolean | Promise<boolean>;
|
||||
private onReject?: (plan: Plan) => void;
|
||||
|
||||
constructor(config: ApprovalCallbackConfig = {}) {
|
||||
this.autoApprove = config.autoApprove ?? false;
|
||||
this.approveFn = config.approveFn;
|
||||
this.onReject = config.onReject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronously check if plan is approved.
|
||||
*/
|
||||
call(plan: Plan): boolean {
|
||||
if (this.autoApprove) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.approveFn) {
|
||||
const result = this.approveFn(plan);
|
||||
if (result instanceof Promise) {
|
||||
// For sync call, we can't await - return false and suggest async
|
||||
console.warn('ApprovalCallback: approveFn is async, use asyncCall instead');
|
||||
return false;
|
||||
}
|
||||
if (!result && this.onReject) {
|
||||
this.onReject(plan);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Default: require explicit approval
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously check if plan is approved.
|
||||
*/
|
||||
async asyncCall(plan: Plan): Promise<boolean> {
|
||||
if (this.autoApprove) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.approveFn) {
|
||||
const result = await this.approveFn(plan);
|
||||
if (!result && this.onReject) {
|
||||
this.onReject(plan);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Default: require explicit approval
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Always approve plans.
|
||||
*/
|
||||
static alwaysApprove(_plan: Plan): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Always reject plans.
|
||||
*/
|
||||
static alwaysReject(_plan: Plan): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Approve plans with few steps automatically.
|
||||
*/
|
||||
static approveIfSmall(maxSteps: number = 5): (plan: Plan) => boolean {
|
||||
return (plan: Plan) => plan.steps.length <= maxSteps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Approve plans that don't use dangerous tools.
|
||||
*/
|
||||
static approveIfNoDangerousTools(plan: Plan): boolean {
|
||||
for (const step of plan.steps) {
|
||||
const tools = step.metadata?.tools as string[] | undefined;
|
||||
if (tools) {
|
||||
for (const tool of tools) {
|
||||
if (RESTRICTED_TOOLS.includes(tool)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an ApprovalCallback.
|
||||
*/
|
||||
export function createApprovalCallback(config?: ApprovalCallbackConfig): ApprovalCallback {
|
||||
return new ApprovalCallback(config);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Core Types
|
||||
// ============================================================================
|
||||
|
||||
export type PlanStatus = 'pending' | 'in_progress' | 'completed' | 'failed' | 'cancelled';
|
||||
export type TodoStatus = 'pending' | 'in_progress' | 'completed';
|
||||
|
||||
|
||||
@@ -1,690 +0,0 @@
|
||||
/**
|
||||
* Plugin Module for PraisonAI TypeScript SDK
|
||||
*
|
||||
* Python parity with praisonaiagents/plugins module
|
||||
*
|
||||
* Provides:
|
||||
* - Plugin protocols and interfaces
|
||||
* - Plugin manager for discovery and loading
|
||||
* - Plugin hooks and lifecycle
|
||||
* - Single-file plugin support
|
||||
*/
|
||||
|
||||
// ============================================================================
|
||||
// Plugin Hooks Enum
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Plugin hook points.
|
||||
* Python parity: praisonaiagents/plugins/plugin.py
|
||||
*/
|
||||
export enum PluginHook {
|
||||
BEFORE_AGENT = 'before_agent',
|
||||
AFTER_AGENT = 'after_agent',
|
||||
BEFORE_TOOL = 'before_tool',
|
||||
AFTER_TOOL = 'after_tool',
|
||||
BEFORE_LLM = 'before_llm',
|
||||
AFTER_LLM = 'after_llm',
|
||||
ON_ERROR = 'on_error',
|
||||
ON_START = 'on_start',
|
||||
ON_STOP = 'on_stop',
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin types.
|
||||
* Python parity: praisonaiagents/plugins/plugin.py
|
||||
*/
|
||||
export enum PluginType {
|
||||
TOOL = 'tool',
|
||||
HOOK = 'hook',
|
||||
AGENT = 'agent',
|
||||
LLM = 'llm',
|
||||
MEMORY = 'memory',
|
||||
FUNCTION = 'function',
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Plugin Interfaces
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Plugin metadata.
|
||||
* Python parity: praisonaiagents/plugins/parser.py
|
||||
*/
|
||||
export interface PluginMetadata {
|
||||
name: string;
|
||||
version: string;
|
||||
description?: string;
|
||||
author?: string;
|
||||
hooks?: PluginHook[];
|
||||
type?: PluginType;
|
||||
dependencies?: string[];
|
||||
config?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin info for listing.
|
||||
* Python parity: praisonaiagents/plugins/plugin.py
|
||||
*/
|
||||
export interface PluginInfo {
|
||||
name: string;
|
||||
version: string;
|
||||
description?: string;
|
||||
enabled: boolean;
|
||||
type: PluginType | string;
|
||||
hooks?: PluginHook[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin protocol interface.
|
||||
* Python parity: praisonaiagents/plugins/protocols.py
|
||||
*/
|
||||
export interface PluginProtocol {
|
||||
name: string;
|
||||
version: string;
|
||||
description?: string;
|
||||
hooks?: PluginHook[];
|
||||
|
||||
initialize?(): void | Promise<void>;
|
||||
shutdown?(): void | Promise<void>;
|
||||
onHook?(hook: PluginHook, ...args: any[]): any | Promise<any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tool plugin protocol.
|
||||
* Python parity: praisonaiagents/plugins/protocols.py
|
||||
*/
|
||||
export interface ToolPluginProtocol extends PluginProtocol {
|
||||
type: 'tool';
|
||||
execute(input: any): any | Promise<any>;
|
||||
getSchema(): Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook plugin protocol.
|
||||
* Python parity: praisonaiagents/plugins/protocols.py
|
||||
*/
|
||||
export interface HookPluginProtocol extends PluginProtocol {
|
||||
type: 'hook';
|
||||
hooks: PluginHook[];
|
||||
handle(hook: PluginHook, context: any): any | Promise<any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Agent plugin protocol.
|
||||
* Python parity: praisonaiagents/plugins/protocols.py
|
||||
*/
|
||||
export interface AgentPluginProtocol extends PluginProtocol {
|
||||
type: 'agent';
|
||||
beforeAgent?(agentName: string, input: string): string | Promise<string>;
|
||||
afterAgent?(agentName: string, output: string): string | Promise<string>;
|
||||
}
|
||||
|
||||
/**
|
||||
* LLM plugin protocol.
|
||||
* Python parity: praisonaiagents/plugins/protocols.py
|
||||
*/
|
||||
export interface LLMPluginProtocol extends PluginProtocol {
|
||||
type: 'llm';
|
||||
beforeLLM?(messages: any[], options: any): any | Promise<any>;
|
||||
afterLLM?(response: any): any | Promise<any>;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Plugin Class
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Base Plugin class.
|
||||
* Python parity: praisonaiagents/plugins/plugin.py
|
||||
*/
|
||||
export class Plugin implements PluginProtocol {
|
||||
name: string;
|
||||
version: string;
|
||||
description?: string;
|
||||
hooks: PluginHook[];
|
||||
type: PluginType;
|
||||
private _enabled: boolean = false;
|
||||
|
||||
constructor(config: {
|
||||
name: string;
|
||||
version?: string;
|
||||
description?: string;
|
||||
hooks?: PluginHook[];
|
||||
type?: PluginType;
|
||||
}) {
|
||||
this.name = config.name;
|
||||
this.version = config.version ?? '1.0.0';
|
||||
this.description = config.description;
|
||||
this.hooks = config.hooks ?? [];
|
||||
this.type = config.type ?? PluginType.FUNCTION;
|
||||
}
|
||||
|
||||
get enabled(): boolean {
|
||||
return this._enabled;
|
||||
}
|
||||
|
||||
enable(): void {
|
||||
this._enabled = true;
|
||||
}
|
||||
|
||||
disable(): void {
|
||||
this._enabled = false;
|
||||
}
|
||||
|
||||
initialize(): void | Promise<void> {
|
||||
// Override in subclass
|
||||
}
|
||||
|
||||
shutdown(): void | Promise<void> {
|
||||
// Override in subclass
|
||||
}
|
||||
|
||||
onHook(hook: PluginHook, ...args: any[]): any | Promise<any> {
|
||||
// Override in subclass
|
||||
return args[0];
|
||||
}
|
||||
|
||||
getInfo(): PluginInfo {
|
||||
return {
|
||||
name: this.name,
|
||||
version: this.version,
|
||||
description: this.description,
|
||||
enabled: this._enabled,
|
||||
type: this.type,
|
||||
hooks: this.hooks,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function-based plugin.
|
||||
* Python parity: praisonaiagents/plugins/plugin.py
|
||||
*/
|
||||
export class FunctionPlugin extends Plugin {
|
||||
private _handler: (hook: PluginHook, ...args: any[]) => any;
|
||||
|
||||
constructor(config: {
|
||||
name: string;
|
||||
version?: string;
|
||||
description?: string;
|
||||
hooks?: PluginHook[];
|
||||
handler: (hook: PluginHook, ...args: any[]) => any;
|
||||
}) {
|
||||
super({
|
||||
name: config.name,
|
||||
version: config.version,
|
||||
description: config.description,
|
||||
hooks: config.hooks,
|
||||
type: PluginType.FUNCTION,
|
||||
});
|
||||
this._handler = config.handler;
|
||||
}
|
||||
|
||||
onHook(hook: PluginHook, ...args: any[]): any {
|
||||
if (this.hooks.includes(hook)) {
|
||||
return this._handler(hook, ...args);
|
||||
}
|
||||
return args[0];
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Plugin Parse Error
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Plugin parse error.
|
||||
* Python parity: praisonaiagents/plugins/parser.py
|
||||
*/
|
||||
export class PluginParseError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public readonly filePath?: string,
|
||||
public readonly line?: number
|
||||
) {
|
||||
super(message);
|
||||
this.name = 'PluginParseError';
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Plugin Manager
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Plugin manager for discovery and lifecycle.
|
||||
* Python parity: praisonaiagents/plugins/manager.py
|
||||
*/
|
||||
export class PluginManager {
|
||||
private _plugins: Map<string, Plugin> = new Map();
|
||||
private _singleFilePlugins: Map<string, PluginMetadata> = new Map();
|
||||
private _hookHandlers: Map<PluginHook, Array<(args: any[]) => any>> = new Map();
|
||||
|
||||
constructor() {
|
||||
// Initialize hook handler maps
|
||||
for (const hook of Object.values(PluginHook)) {
|
||||
this._hookHandlers.set(hook, []);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a plugin.
|
||||
*/
|
||||
register(plugin: Plugin): void {
|
||||
this._plugins.set(plugin.name, plugin);
|
||||
|
||||
// Register hook handlers
|
||||
for (const hook of plugin.hooks) {
|
||||
const handlers = this._hookHandlers.get(hook) ?? [];
|
||||
handlers.push((...args: any[]) => plugin.onHook(hook, ...args));
|
||||
this._hookHandlers.set(hook, handlers);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister a plugin.
|
||||
*/
|
||||
unregister(name: string): boolean {
|
||||
const plugin = this._plugins.get(name);
|
||||
if (plugin) {
|
||||
plugin.disable();
|
||||
this._plugins.delete(name);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a plugin by name.
|
||||
*/
|
||||
get(name: string): Plugin | undefined {
|
||||
return this._plugins.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable a plugin.
|
||||
*/
|
||||
enable(name: string): boolean {
|
||||
const plugin = this._plugins.get(name);
|
||||
if (plugin) {
|
||||
plugin.enable();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable a plugin.
|
||||
*/
|
||||
disable(name: string): boolean {
|
||||
const plugin = this._plugins.get(name);
|
||||
if (plugin) {
|
||||
plugin.disable();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a plugin is enabled.
|
||||
*/
|
||||
isEnabled(name: string): boolean {
|
||||
const plugin = this._plugins.get(name);
|
||||
return plugin?.enabled ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* List all plugins.
|
||||
*/
|
||||
listPlugins(): PluginInfo[] {
|
||||
const result: PluginInfo[] = [];
|
||||
|
||||
for (const plugin of this._plugins.values()) {
|
||||
result.push(plugin.getInfo());
|
||||
}
|
||||
|
||||
for (const [name, meta] of this._singleFilePlugins) {
|
||||
result.push({
|
||||
name,
|
||||
version: meta.version,
|
||||
description: meta.description,
|
||||
enabled: false,
|
||||
type: meta.type ?? 'single_file',
|
||||
hooks: meta.hooks,
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire a hook to all registered handlers.
|
||||
*/
|
||||
async fireHook(hook: PluginHook, ...args: any[]): Promise<any> {
|
||||
const handlers = this._hookHandlers.get(hook) ?? [];
|
||||
let result = args[0];
|
||||
|
||||
for (const handler of handlers) {
|
||||
const handlerResult = await handler(args);
|
||||
if (handlerResult !== undefined) {
|
||||
result = handlerResult;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire a hook synchronously.
|
||||
*/
|
||||
fireHookSync(hook: PluginHook, ...args: any[]): any {
|
||||
const handlers = this._hookHandlers.get(hook) ?? [];
|
||||
let result = args[0];
|
||||
|
||||
for (const handler of handlers) {
|
||||
const handlerResult = handler(args);
|
||||
if (handlerResult !== undefined) {
|
||||
result = handlerResult;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize all plugins.
|
||||
*/
|
||||
async initializeAll(): Promise<void> {
|
||||
for (const plugin of this._plugins.values()) {
|
||||
if (plugin.enabled && plugin.initialize) {
|
||||
await plugin.initialize();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown all plugins.
|
||||
*/
|
||||
async shutdownAll(): Promise<void> {
|
||||
for (const plugin of this._plugins.values()) {
|
||||
if (plugin.enabled && plugin.shutdown) {
|
||||
await plugin.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Auto-discover plugins from default directories.
|
||||
*/
|
||||
autoDiscoverPlugins(): void {
|
||||
// In browser/Node.js, this would scan directories
|
||||
// For now, this is a no-op placeholder
|
||||
}
|
||||
|
||||
/**
|
||||
* Load plugins from a directory.
|
||||
*/
|
||||
loadFromDirectory(path: string): void {
|
||||
// Placeholder for directory loading
|
||||
// Would use fs in Node.js environment
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Global Plugin Manager
|
||||
// ============================================================================
|
||||
|
||||
let _globalPluginManager: PluginManager | null = null;
|
||||
|
||||
/**
|
||||
* Get the global plugin manager.
|
||||
* Python parity: praisonaiagents/plugins/manager.py
|
||||
*/
|
||||
export function getPluginManager(): PluginManager {
|
||||
if (!_globalPluginManager) {
|
||||
_globalPluginManager = new PluginManager();
|
||||
}
|
||||
return _globalPluginManager;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Plugin Discovery Functions
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Get default plugin directories.
|
||||
* Python parity: praisonaiagents/plugins/discovery.py
|
||||
*/
|
||||
export function getDefaultPluginDirs(): string[] {
|
||||
const dirs: string[] = [];
|
||||
|
||||
// Project-level
|
||||
dirs.push('./.praison/plugins');
|
||||
dirs.push('./.praisonai/plugins');
|
||||
|
||||
// User-level (would use os.homedir() in Node.js)
|
||||
if (typeof process !== 'undefined' && process.env?.HOME) {
|
||||
dirs.push(`${process.env.HOME}/.praison/plugins`);
|
||||
dirs.push(`${process.env.HOME}/.praisonai/plugins`);
|
||||
}
|
||||
|
||||
return dirs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure plugin directory exists.
|
||||
* Python parity: praisonaiagents/plugins/discovery.py
|
||||
*/
|
||||
export function ensurePluginDir(path: string): boolean {
|
||||
// In Node.js, would use fs.mkdirSync
|
||||
// For browser, this is a no-op
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Discover plugins in a directory.
|
||||
* Python parity: praisonaiagents/plugins/discovery.py
|
||||
*/
|
||||
export function discoverPlugins(directory: string): PluginMetadata[] {
|
||||
// Placeholder - would scan directory for plugin files
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a single plugin.
|
||||
* Python parity: praisonaiagents/plugins/discovery.py
|
||||
*/
|
||||
export function loadPlugin(path: string): Plugin | null {
|
||||
// Placeholder - would load and instantiate plugin
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Discover and load all plugins.
|
||||
* Python parity: praisonaiagents/plugins/discovery.py
|
||||
*/
|
||||
export function discoverAndLoadPlugins(directories?: string[]): Plugin[] {
|
||||
const dirs = directories ?? getDefaultPluginDirs();
|
||||
const plugins: Plugin[] = [];
|
||||
|
||||
for (const dir of dirs) {
|
||||
const discovered = discoverPlugins(dir);
|
||||
for (const meta of discovered) {
|
||||
const plugin = new Plugin({
|
||||
name: meta.name,
|
||||
version: meta.version,
|
||||
description: meta.description,
|
||||
hooks: meta.hooks,
|
||||
type: meta.type,
|
||||
});
|
||||
plugins.push(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
return plugins;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get plugin template.
|
||||
* Python parity: praisonaiagents/plugins/discovery.py
|
||||
*/
|
||||
export function getPluginTemplate(name: string): string {
|
||||
return `/**
|
||||
* ${name} Plugin
|
||||
*
|
||||
* A PraisonAI plugin.
|
||||
*/
|
||||
|
||||
import { Plugin, PluginHook, PluginType } from 'praisonai';
|
||||
|
||||
export class ${name}Plugin extends Plugin {
|
||||
constructor() {
|
||||
super({
|
||||
name: '${name.toLowerCase()}',
|
||||
version: '1.0.0',
|
||||
description: 'A custom plugin',
|
||||
hooks: [PluginHook.BEFORE_AGENT],
|
||||
type: PluginType.HOOK,
|
||||
});
|
||||
}
|
||||
|
||||
onHook(hook: PluginHook, ...args: any[]): any {
|
||||
if (hook === PluginHook.BEFORE_AGENT) {
|
||||
console.log('Before agent:', args);
|
||||
}
|
||||
return args[0];
|
||||
}
|
||||
}
|
||||
|
||||
export default new ${name}Plugin();
|
||||
`;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Plugin Parser Functions
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Parse plugin header from content.
|
||||
* Python parity: praisonaiagents/plugins/parser.py
|
||||
*/
|
||||
export function parsePluginHeader(content: string): PluginMetadata | null {
|
||||
// Look for YAML frontmatter or JSDoc-style header
|
||||
const yamlMatch = content.match(/^---\n([\s\S]*?)\n---/);
|
||||
if (yamlMatch) {
|
||||
try {
|
||||
// Simple YAML parsing for common fields
|
||||
const yaml = yamlMatch[1];
|
||||
const metadata: PluginMetadata = {
|
||||
name: '',
|
||||
version: '1.0.0',
|
||||
};
|
||||
|
||||
const nameMatch = yaml.match(/name:\s*(.+)/);
|
||||
if (nameMatch) metadata.name = nameMatch[1].trim();
|
||||
|
||||
const versionMatch = yaml.match(/version:\s*(.+)/);
|
||||
if (versionMatch) metadata.version = versionMatch[1].trim();
|
||||
|
||||
const descMatch = yaml.match(/description:\s*(.+)/);
|
||||
if (descMatch) metadata.description = descMatch[1].trim();
|
||||
|
||||
const authorMatch = yaml.match(/author:\s*(.+)/);
|
||||
if (authorMatch) metadata.author = authorMatch[1].trim();
|
||||
|
||||
return metadata;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Look for JSDoc-style @plugin annotation
|
||||
const jsdocMatch = content.match(/@plugin\s+(\w+)/);
|
||||
if (jsdocMatch) {
|
||||
return {
|
||||
name: jsdocMatch[1],
|
||||
version: '1.0.0',
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse plugin header from file path.
|
||||
* Python parity: praisonaiagents/plugins/parser.py
|
||||
*/
|
||||
export function parsePluginHeaderFromFile(filePath: string): PluginMetadata | null {
|
||||
// In Node.js, would read file and call parsePluginHeader
|
||||
// For browser, this is a placeholder
|
||||
return null;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Easy Enable API
|
||||
// ============================================================================
|
||||
|
||||
let _pluginsEnabled = false;
|
||||
let _enabledPluginNames: string[] | null = null;
|
||||
|
||||
/**
|
||||
* Enable the plugin system.
|
||||
* Python parity: praisonaiagents/plugins/__init__.py
|
||||
*/
|
||||
export function enable(plugins?: string[]): void {
|
||||
_pluginsEnabled = true;
|
||||
_enabledPluginNames = plugins ?? null;
|
||||
|
||||
const manager = getPluginManager();
|
||||
manager.autoDiscoverPlugins();
|
||||
|
||||
if (plugins) {
|
||||
for (const name of plugins) {
|
||||
manager.enable(name);
|
||||
}
|
||||
} else {
|
||||
for (const info of manager.listPlugins()) {
|
||||
manager.enable(info.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable plugins.
|
||||
* Python parity: praisonaiagents/plugins/__init__.py
|
||||
*/
|
||||
export function disable(plugins?: string[]): void {
|
||||
const manager = getPluginManager();
|
||||
|
||||
if (plugins) {
|
||||
for (const name of plugins) {
|
||||
manager.disable(name);
|
||||
}
|
||||
} else {
|
||||
_pluginsEnabled = false;
|
||||
_enabledPluginNames = null;
|
||||
for (const info of manager.listPlugins()) {
|
||||
manager.disable(info.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* List all plugins.
|
||||
* Python parity: praisonaiagents/plugins/__init__.py
|
||||
*/
|
||||
export function listPlugins(): PluginInfo[] {
|
||||
return getPluginManager().listPlugins();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if plugins are enabled.
|
||||
* Python parity: praisonaiagents/plugins/__init__.py
|
||||
*/
|
||||
export function isEnabled(name?: string): boolean {
|
||||
if (name === undefined) {
|
||||
return _pluginsEnabled;
|
||||
}
|
||||
return getPluginManager().isEnabled(name);
|
||||
}
|
||||
@@ -1,608 +0,0 @@
|
||||
/**
|
||||
* Protocols Module for PraisonAI TypeScript SDK
|
||||
*
|
||||
* Python parity with praisonaiagents protocols
|
||||
*
|
||||
* Provides:
|
||||
* - A2A (Agent-to-Agent) protocol types
|
||||
* - AGUI (Agent GUI) protocol types
|
||||
* - AutoRagAgent configuration
|
||||
* - Tools class
|
||||
* - Global singletons (config, memory, obs, workflows)
|
||||
* - Guardrail policy resolver
|
||||
*/
|
||||
|
||||
// ============================================================================
|
||||
// A2A Protocol Types (Agent-to-Agent)
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* A2A Task state.
|
||||
* Python parity: praisonaiagents/ui/a2a/types.py
|
||||
*/
|
||||
export enum A2ATaskState {
|
||||
PENDING = 'pending',
|
||||
RUNNING = 'running',
|
||||
COMPLETED = 'completed',
|
||||
FAILED = 'failed',
|
||||
CANCELED = 'canceled',
|
||||
}
|
||||
|
||||
/**
|
||||
* A2A Role.
|
||||
* Python parity: praisonaiagents/ui/a2a/types.py
|
||||
*/
|
||||
export enum A2ARole {
|
||||
USER = 'user',
|
||||
AGENT = 'agent',
|
||||
}
|
||||
|
||||
/**
|
||||
* A2A Text part.
|
||||
* Python parity: praisonaiagents/ui/a2a/types.py
|
||||
*/
|
||||
export interface A2ATextPart {
|
||||
type: 'text';
|
||||
text: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A2A File part.
|
||||
* Python parity: praisonaiagents/ui/a2a/types.py
|
||||
*/
|
||||
export interface A2AFilePart {
|
||||
type: 'file';
|
||||
file: {
|
||||
name: string;
|
||||
mimeType: string;
|
||||
bytes?: string;
|
||||
uri?: string;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A2A Data part.
|
||||
* Python parity: praisonaiagents/ui/a2a/types.py
|
||||
*/
|
||||
export interface A2ADataPart {
|
||||
type: 'data';
|
||||
data: Record<string, any>;
|
||||
}
|
||||
|
||||
export type A2APart = A2ATextPart | A2AFilePart | A2ADataPart;
|
||||
|
||||
/**
|
||||
* A2A Message.
|
||||
* Python parity: praisonaiagents/ui/a2a/types.py
|
||||
*/
|
||||
export interface A2AMessage {
|
||||
role: A2ARole;
|
||||
parts: A2APart[];
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* A2A Task status.
|
||||
* Python parity: praisonaiagents/ui/a2a/types.py
|
||||
*/
|
||||
export interface A2ATaskStatus {
|
||||
state: A2ATaskState;
|
||||
message?: A2AMessage;
|
||||
timestamp?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A2A Task.
|
||||
* Python parity: praisonaiagents/ui/a2a/types.py
|
||||
*/
|
||||
export interface A2ATask {
|
||||
id: string;
|
||||
sessionId?: string;
|
||||
status: A2ATaskStatus;
|
||||
artifacts?: A2AArtifact[];
|
||||
history?: A2AMessage[];
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* A2A Artifact.
|
||||
* Python parity: praisonaiagents/ui/a2a/types.py
|
||||
*/
|
||||
export interface A2AArtifact {
|
||||
name: string;
|
||||
description?: string;
|
||||
parts: A2APart[];
|
||||
index?: number;
|
||||
append?: boolean;
|
||||
lastChunk?: boolean;
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* A2A Agent skill.
|
||||
* Python parity: praisonaiagents/ui/a2a/types.py
|
||||
*/
|
||||
export interface A2AAgentSkill {
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
tags?: string[];
|
||||
examples?: string[];
|
||||
inputModes?: string[];
|
||||
outputModes?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* A2A Agent capabilities.
|
||||
* Python parity: praisonaiagents/ui/a2a/types.py
|
||||
*/
|
||||
export interface A2AAgentCapabilities {
|
||||
streaming?: boolean;
|
||||
pushNotifications?: boolean;
|
||||
stateTransitionHistory?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* A2A Agent card.
|
||||
* Python parity: praisonaiagents/ui/a2a/types.py
|
||||
*/
|
||||
export interface A2AAgentCard {
|
||||
name: string;
|
||||
description?: string;
|
||||
url: string;
|
||||
provider?: {
|
||||
organization: string;
|
||||
url?: string;
|
||||
};
|
||||
version: string;
|
||||
documentationUrl?: string;
|
||||
capabilities?: A2AAgentCapabilities;
|
||||
authentication?: {
|
||||
schemes: string[];
|
||||
credentials?: string;
|
||||
};
|
||||
defaultInputModes?: string[];
|
||||
defaultOutputModes?: string[];
|
||||
skills?: A2AAgentSkill[];
|
||||
}
|
||||
|
||||
/**
|
||||
* A2A Send message request.
|
||||
* Python parity: praisonaiagents/ui/a2a/types.py
|
||||
*/
|
||||
export interface A2ASendMessageRequest {
|
||||
id: string;
|
||||
jsonrpc: '2.0';
|
||||
method: 'message/send';
|
||||
params: {
|
||||
id: string;
|
||||
sessionId?: string;
|
||||
message: A2AMessage;
|
||||
acceptedOutputModes?: string[];
|
||||
pushNotificationConfig?: {
|
||||
url: string;
|
||||
token?: string;
|
||||
};
|
||||
metadata?: Record<string, any>;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A2A class placeholder.
|
||||
* Python parity: praisonaiagents/ui/a2a/a2a.py
|
||||
*/
|
||||
export class A2A {
|
||||
private agentCard: A2AAgentCard;
|
||||
|
||||
constructor(config: {
|
||||
name: string;
|
||||
description?: string;
|
||||
url?: string;
|
||||
version?: string;
|
||||
skills?: A2AAgentSkill[];
|
||||
}) {
|
||||
this.agentCard = {
|
||||
name: config.name,
|
||||
description: config.description,
|
||||
url: config.url ?? 'http://localhost:8000',
|
||||
version: config.version ?? '1.0.0',
|
||||
skills: config.skills,
|
||||
};
|
||||
}
|
||||
|
||||
getAgentCard(): A2AAgentCard {
|
||||
return this.agentCard;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// AGUI Protocol Types (Agent GUI)
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* AGUI class placeholder.
|
||||
* Python parity: praisonaiagents/ui/agui/agui.py
|
||||
*/
|
||||
export class AGUI {
|
||||
private config: {
|
||||
name: string;
|
||||
description?: string;
|
||||
};
|
||||
|
||||
constructor(config: {
|
||||
name: string;
|
||||
description?: string;
|
||||
}) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
getName(): string {
|
||||
return this.config.name;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// AutoRagAgent Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Retrieval policy for AutoRagAgent.
|
||||
* Python parity: praisonaiagents/agents/auto_rag_agent.py
|
||||
*/
|
||||
export enum RetrievalPolicy {
|
||||
AUTO = 'auto',
|
||||
ALWAYS = 'always',
|
||||
NEVER = 'never',
|
||||
}
|
||||
|
||||
/**
|
||||
* AutoRagAgent configuration.
|
||||
* Python parity: praisonaiagents/agents/auto_rag_agent.py
|
||||
*/
|
||||
export interface AutoRagAgentConfig {
|
||||
retrievalPolicy?: RetrievalPolicy;
|
||||
topK?: number;
|
||||
hybrid?: boolean;
|
||||
rerank?: boolean;
|
||||
includeCitations?: boolean;
|
||||
citationsMode?: 'append' | 'hidden' | 'none';
|
||||
maxContextTokens?: number;
|
||||
autoKeywords?: Set<string>;
|
||||
autoMinLength?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default auto keywords for RAG retrieval.
|
||||
*/
|
||||
export const DEFAULT_AUTO_KEYWORDS = new Set([
|
||||
'what', 'how', 'why', 'when', 'where', 'who', 'which',
|
||||
'explain', 'describe', 'summarize', 'find', 'search',
|
||||
'tell me', 'show me', 'according to', 'based on',
|
||||
'cite', 'source', 'reference', 'document', 'paper',
|
||||
]);
|
||||
|
||||
/**
|
||||
* AutoRagAgent class.
|
||||
* Python parity: praisonaiagents/agents/auto_rag_agent.py
|
||||
*/
|
||||
export class AutoRagAgent {
|
||||
private config: AutoRagAgentConfig;
|
||||
|
||||
constructor(config: AutoRagAgentConfig = {}) {
|
||||
this.config = {
|
||||
retrievalPolicy: RetrievalPolicy.AUTO,
|
||||
topK: 5,
|
||||
hybrid: false,
|
||||
rerank: false,
|
||||
includeCitations: true,
|
||||
citationsMode: 'append',
|
||||
maxContextTokens: 4000,
|
||||
autoKeywords: DEFAULT_AUTO_KEYWORDS,
|
||||
autoMinLength: 10,
|
||||
...config,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if query should trigger retrieval.
|
||||
*/
|
||||
shouldRetrieve(query: string): boolean {
|
||||
if (this.config.retrievalPolicy === RetrievalPolicy.ALWAYS) {
|
||||
return true;
|
||||
}
|
||||
if (this.config.retrievalPolicy === RetrievalPolicy.NEVER) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// AUTO mode - check heuristics
|
||||
if (query.length < (this.config.autoMinLength ?? 10)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const lowerQuery = query.toLowerCase();
|
||||
for (const keyword of this.config.autoKeywords ?? DEFAULT_AUTO_KEYWORDS) {
|
||||
if (lowerQuery.includes(keyword)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
getConfig(): AutoRagAgentConfig {
|
||||
return { ...this.config };
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Tools Class
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Tool definition.
|
||||
*/
|
||||
export interface ToolDefinition {
|
||||
name: string;
|
||||
description: string;
|
||||
parameters?: Record<string, any>;
|
||||
execute: (...args: any[]) => any | Promise<any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tools registry class.
|
||||
* Python parity: praisonaiagents/tools
|
||||
*/
|
||||
export class Tools {
|
||||
private _tools: Map<string, ToolDefinition> = new Map();
|
||||
|
||||
/**
|
||||
* Register a tool.
|
||||
*/
|
||||
register(tool: ToolDefinition): void {
|
||||
this._tools.set(tool.name, tool);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a tool by name.
|
||||
*/
|
||||
get(name: string): ToolDefinition | undefined {
|
||||
return this._tools.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if tool exists.
|
||||
*/
|
||||
has(name: string): boolean {
|
||||
return this._tools.has(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* List all tools.
|
||||
*/
|
||||
list(): ToolDefinition[] {
|
||||
return Array.from(this._tools.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a tool.
|
||||
*/
|
||||
remove(name: string): boolean {
|
||||
return this._tools.delete(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all tools.
|
||||
*/
|
||||
clear(): void {
|
||||
this._tools.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tool count.
|
||||
*/
|
||||
get count(): number {
|
||||
return this._tools.size;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Global Singletons
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Global config singleton.
|
||||
* Python parity: praisonaiagents.config
|
||||
*/
|
||||
let _globalConfig: Record<string, any> = {};
|
||||
|
||||
export const config = {
|
||||
get<T = any>(key: string, defaultValue?: T): T | undefined {
|
||||
return _globalConfig[key] ?? defaultValue;
|
||||
},
|
||||
set(key: string, value: any): void {
|
||||
_globalConfig[key] = value;
|
||||
},
|
||||
getAll(): Record<string, any> {
|
||||
return { ..._globalConfig };
|
||||
},
|
||||
clear(): void {
|
||||
_globalConfig = {};
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Global memory singleton.
|
||||
* Python parity: praisonaiagents.memory
|
||||
*/
|
||||
let _globalMemory: Map<string, any> = new Map();
|
||||
|
||||
export const memory = {
|
||||
get<T = any>(key: string): T | undefined {
|
||||
return _globalMemory.get(key);
|
||||
},
|
||||
set(key: string, value: any): void {
|
||||
_globalMemory.set(key, value);
|
||||
},
|
||||
has(key: string): boolean {
|
||||
return _globalMemory.has(key);
|
||||
},
|
||||
delete(key: string): boolean {
|
||||
return _globalMemory.delete(key);
|
||||
},
|
||||
clear(): void {
|
||||
_globalMemory.clear();
|
||||
},
|
||||
keys(): string[] {
|
||||
return Array.from(_globalMemory.keys());
|
||||
},
|
||||
size(): number {
|
||||
return _globalMemory.size;
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Global observability singleton.
|
||||
* Python parity: praisonaiagents.obs
|
||||
*/
|
||||
let _obsEnabled = false;
|
||||
let _obsProvider: any = null;
|
||||
|
||||
export const obs = {
|
||||
enable(provider?: any): void {
|
||||
_obsEnabled = true;
|
||||
_obsProvider = provider;
|
||||
},
|
||||
disable(): void {
|
||||
_obsEnabled = false;
|
||||
_obsProvider = null;
|
||||
},
|
||||
isEnabled(): boolean {
|
||||
return _obsEnabled;
|
||||
},
|
||||
getProvider(): any {
|
||||
return _obsProvider;
|
||||
},
|
||||
trace(name: string, data?: Record<string, any>): void {
|
||||
if (_obsEnabled && _obsProvider?.trace) {
|
||||
_obsProvider.trace(name, data);
|
||||
}
|
||||
},
|
||||
span(name: string): { end: () => void } {
|
||||
if (_obsEnabled && _obsProvider?.span) {
|
||||
return _obsProvider.span(name);
|
||||
}
|
||||
return { end: () => {} };
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Global workflows singleton.
|
||||
* Python parity: praisonaiagents.workflows
|
||||
*/
|
||||
let _workflows: Map<string, any> = new Map();
|
||||
|
||||
export const workflows = {
|
||||
register(name: string, workflow: any): void {
|
||||
_workflows.set(name, workflow);
|
||||
},
|
||||
get(name: string): any | undefined {
|
||||
return _workflows.get(name);
|
||||
},
|
||||
has(name: string): boolean {
|
||||
return _workflows.has(name);
|
||||
},
|
||||
list(): string[] {
|
||||
return Array.from(_workflows.keys());
|
||||
},
|
||||
remove(name: string): boolean {
|
||||
return _workflows.delete(name);
|
||||
},
|
||||
clear(): void {
|
||||
_workflows.clear();
|
||||
},
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// Guardrail Policy Resolver
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Guardrail policy.
|
||||
*/
|
||||
export interface GuardrailPolicy {
|
||||
name: string;
|
||||
action: 'block' | 'warn' | 'log' | 'allow';
|
||||
conditions?: Record<string, any>;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve guardrail policies.
|
||||
* Python parity: praisonaiagents/config/resolvers.py
|
||||
*/
|
||||
export function resolveGuardrailPolicies(
|
||||
policies: (string | GuardrailPolicy)[]
|
||||
): GuardrailPolicy[] {
|
||||
const resolved: GuardrailPolicy[] = [];
|
||||
|
||||
for (const policy of policies) {
|
||||
if (typeof policy === 'string') {
|
||||
// Resolve string preset
|
||||
const preset = GUARDRAIL_POLICY_PRESETS[policy];
|
||||
if (preset) {
|
||||
resolved.push(preset);
|
||||
} else {
|
||||
resolved.push({
|
||||
name: policy,
|
||||
action: 'warn',
|
||||
});
|
||||
}
|
||||
} else {
|
||||
resolved.push(policy);
|
||||
}
|
||||
}
|
||||
|
||||
return resolved;
|
||||
}
|
||||
|
||||
/**
|
||||
* Guardrail policy presets.
|
||||
*/
|
||||
export const GUARDRAIL_POLICY_PRESETS: Record<string, GuardrailPolicy> = {
|
||||
'block-pii': {
|
||||
name: 'block-pii',
|
||||
action: 'block',
|
||||
message: 'PII detected in content',
|
||||
},
|
||||
'warn-pii': {
|
||||
name: 'warn-pii',
|
||||
action: 'warn',
|
||||
message: 'PII detected in content',
|
||||
},
|
||||
'block-profanity': {
|
||||
name: 'block-profanity',
|
||||
action: 'block',
|
||||
message: 'Profanity detected',
|
||||
},
|
||||
'log-all': {
|
||||
name: 'log-all',
|
||||
action: 'log',
|
||||
},
|
||||
'allow-all': {
|
||||
name: 'allow-all',
|
||||
action: 'allow',
|
||||
},
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// AgentManager Alias
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* AgentManager is an alias for AgentTeam.
|
||||
* Python parity: praisonaiagents/agents/agents.py
|
||||
*
|
||||
* Note: This is exported from the main index.ts as an alias.
|
||||
* The actual AgentTeam class is in the agents module.
|
||||
*/
|
||||
export type AgentManager = any; // Alias - actual type is AgentTeam
|
||||
@@ -1,137 +0,0 @@
|
||||
/**
|
||||
* RAG Module Index - Export all RAG utilities
|
||||
*
|
||||
* Python parity with praisonaiagents/rag/__init__.py
|
||||
*/
|
||||
|
||||
// Models
|
||||
export {
|
||||
RetrievalStrategy,
|
||||
type RetrievalStrategyType,
|
||||
type Citation,
|
||||
createCitation,
|
||||
formatCitation,
|
||||
type ContextPack,
|
||||
createContextPack,
|
||||
hasCitations,
|
||||
formatContextPackForPrompt,
|
||||
type RAGResult,
|
||||
createRAGResult,
|
||||
formatAnswerWithCitations,
|
||||
type RAGConfig,
|
||||
DEFAULT_RAG_TEMPLATE,
|
||||
createRAGConfig,
|
||||
} from './models';
|
||||
|
||||
// Retrieval Config
|
||||
export {
|
||||
RetrievalPolicy,
|
||||
type RetrievalPolicyType,
|
||||
CitationsMode,
|
||||
type CitationsModeType,
|
||||
type RetrievalConfig,
|
||||
createRetrievalConfig,
|
||||
createSimpleRetrievalConfig,
|
||||
createSmartRetrievalConfig,
|
||||
} from './retrieval-config';
|
||||
|
||||
// RAG class (placeholder for full implementation)
|
||||
export class RAG {
|
||||
private config: import('./models').RAGConfig;
|
||||
private knowledge: any;
|
||||
|
||||
constructor(options: { knowledge?: any; config?: Partial<import('./models').RAGConfig> } = {}) {
|
||||
const { createRAGConfig } = require('./models');
|
||||
this.config = createRAGConfig(options.config);
|
||||
this.knowledge = options.knowledge;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query the RAG pipeline.
|
||||
*/
|
||||
async query(question: string): Promise<import('./models').RAGResult> {
|
||||
const { createRAGResult } = require('./models');
|
||||
|
||||
// If no knowledge base, return empty result
|
||||
if (!this.knowledge) {
|
||||
return createRAGResult({
|
||||
answer: 'No knowledge base configured.',
|
||||
query: question,
|
||||
});
|
||||
}
|
||||
|
||||
// Retrieve context from knowledge base
|
||||
let context = '';
|
||||
let citations: import('./models').Citation[] = [];
|
||||
|
||||
if (typeof this.knowledge.search === 'function') {
|
||||
const results = await this.knowledge.search(question, this.config.topK);
|
||||
if (results && results.length > 0) {
|
||||
context = results.map((r: any) => r.content || r.text || '').join('\n\n');
|
||||
citations = results.map((r: any, i: number) => ({
|
||||
id: `[${i + 1}]`,
|
||||
source: r.source || r.id || `doc-${i}`,
|
||||
text: (r.content || r.text || '').slice(0, 200),
|
||||
score: r.score || 0,
|
||||
metadata: r.metadata || {},
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
// For now, return the context as the answer (full LLM generation would be added)
|
||||
return createRAGResult({
|
||||
answer: context || 'No relevant information found.',
|
||||
citations,
|
||||
contextUsed: context,
|
||||
query: question,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve context without generating an answer.
|
||||
*/
|
||||
async retrieve(question: string): Promise<import('./models').ContextPack> {
|
||||
const { createContextPack } = require('./models');
|
||||
|
||||
if (!this.knowledge) {
|
||||
return createContextPack({ query: question });
|
||||
}
|
||||
|
||||
let context = '';
|
||||
let citations: import('./models').Citation[] = [];
|
||||
|
||||
if (typeof this.knowledge.search === 'function') {
|
||||
const results = await this.knowledge.search(question, this.config.topK);
|
||||
if (results && results.length > 0) {
|
||||
context = results.map((r: any) => r.content || r.text || '').join('\n\n');
|
||||
citations = results.map((r: any, i: number) => ({
|
||||
id: `[${i + 1}]`,
|
||||
source: r.source || r.id || `doc-${i}`,
|
||||
text: (r.content || r.text || '').slice(0, 200),
|
||||
score: r.score || 0,
|
||||
metadata: r.metadata || {},
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
return createContextPack({
|
||||
context,
|
||||
citations,
|
||||
query: question,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current configuration.
|
||||
*/
|
||||
getConfig(): import('./models').RAGConfig {
|
||||
return { ...this.config };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a RAG instance.
|
||||
*/
|
||||
export function createRAG(options?: { knowledge?: any; config?: Partial<import('./models').RAGConfig> }): RAG {
|
||||
return new RAG(options);
|
||||
}
|
||||
Loaded 100 of 147 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user