-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathpaths.py
More file actions
452 lines (334 loc) Ā· 10.3 KB
/
Copy pathpaths.py
File metadata and controls
452 lines (334 loc) Ā· 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
"""
Centralized Path Utilities for PraisonAI Agents.
All persistent data uses ~/.praisonai/ by default.
Override with PRAISONAI_HOME environment variable.
This module provides a single source of truth for all data storage paths,
eliminating hardcoded paths throughout the codebase (DRY principle).
Usage:
from praisonaiagents.paths import get_data_dir, get_sessions_dir
# Get user data directory
data_dir = get_data_dir() # ~/.praisonai/
# Get specific subdirectories
sessions_dir = get_sessions_dir() # ~/.praisonai/sessions/
# Override with environment variable
# export PRAISONAI_HOME=/custom/path
# data_dir = get_data_dir() # /custom/path/
Backward Compatibility:
If ~/.praisonai/ doesn't exist but ~/.praison/ does, the legacy
path will be used with a deprecation warning. Run 'praisonai migrate-data'
to migrate to the new location.
"""
import os
import warnings
from pathlib import Path
from typing import Dict, Optional, Union
# Environment variable for override
ENV_VAR = "PRAISONAI_HOME"
# Default directory name (branded)
DEFAULT_DIR_NAME = ".praisonai"
# Legacy directory name (for backward compat)
LEGACY_DIR_NAME = ".praison"
# Cache for data dir to avoid repeated filesystem checks
_data_dir_cache: Optional[Path] = None
def _clear_cache() -> None:
"""Clear the data dir cache. Used for testing."""
global _data_dir_cache
_data_dir_cache = None
def get_data_dir() -> Path:
"""
Get PraisonAI data directory.
Priority:
1. PRAISONAI_HOME env var
2. ~/.praisonai/ (default)
3. ~/.praison/ (legacy fallback with warning)
Returns:
Path to data directory
Example:
>>> from praisonaiagents.paths import get_data_dir
>>> data_dir = get_data_dir()
>>> print(data_dir)
/home/user/.praisonai
"""
global _data_dir_cache
# Check env var first (always takes precedence, no caching)
env_path = os.environ.get(ENV_VAR)
if env_path:
return Path(env_path).expanduser()
# Return cached value if available
if _data_dir_cache is not None:
return _data_dir_cache
home = Path.home()
# Check new location first
new_path = home / DEFAULT_DIR_NAME
if new_path.exists():
_data_dir_cache = new_path
return new_path
# Check legacy location (backward compat)
legacy_path = home / LEGACY_DIR_NAME
if legacy_path.exists():
from .utils.deprecation import warn_deprecated_param
warn_deprecated_param(
"legacy data directory",
since="1.0.0",
removal="2.0.0",
alternative=f"run 'praisonai migrate-data' to migrate to {new_path}",
details=f"Using legacy directory {legacy_path}",
stacklevel=3
)
_data_dir_cache = legacy_path
return legacy_path
# Default to new location (will be created when needed)
_data_dir_cache = new_path
return new_path
def get_sessions_dir() -> Path:
"""
Get sessions directory.
Returns:
Path to ~/.praisonai/sessions/
"""
return get_data_dir() / "sessions"
def get_skills_dir() -> Path:
"""
Get user skills directory.
Returns:
Path to ~/.praisonai/skills/
"""
return get_data_dir() / "skills"
def get_plugins_dir() -> Path:
"""
Get user plugins directory.
Returns:
Path to ~/.praisonai/plugins/
"""
return get_data_dir() / "plugins"
def get_mcp_dir() -> Path:
"""
Get MCP config directory.
Returns:
Path to ~/.praisonai/mcp/
"""
return get_data_dir() / "mcp"
def get_docs_dir() -> Path:
"""
Get docs directory.
Returns:
Path to ~/.praisonai/docs/
"""
return get_data_dir() / "docs"
def get_rules_dir() -> Path:
"""
Get rules directory.
Returns:
Path to ~/.praisonai/rules/
"""
return get_data_dir() / "rules"
def get_permissions_dir() -> Path:
"""
Get permissions directory.
Returns:
Path to ~/.praisonai/permissions/
"""
return get_data_dir() / "permissions"
def get_storage_dir() -> Path:
"""
Get generic storage directory.
Returns:
Path to ~/.praisonai/storage/
"""
return get_data_dir() / "storage"
def get_checkpoints_dir() -> Path:
"""
Get checkpoints directory.
Returns:
Path to ~/.praisonai/checkpoints/
"""
return get_data_dir() / "checkpoints"
def get_snapshots_dir() -> Path:
"""
Get snapshots directory.
Returns:
Path to ~/.praisonai/snapshots/
"""
return get_data_dir() / "snapshots"
def get_learn_dir() -> Path:
"""
Get learn directory for learning stores.
Returns:
Path to ~/.praisonai/learn/
"""
return get_data_dir() / "learn"
def get_cache_dir() -> Path:
"""
Get cache directory (disposable data).
Returns:
Path to ~/.praisonai/cache/
"""
return get_data_dir() / "cache"
def get_mcp_auth_path() -> Path:
"""
Get path to MCP auth storage file.
Returns:
Path to ~/.praisonai/mcp-auth.json
"""
return get_data_dir() / "mcp-auth.json"
def get_memory_dir() -> Path:
"""
Get memory directory for short/long term databases.
Returns:
Path to ~/.praisonai/memory/
"""
return get_data_dir() / "memory"
def get_workflows_dir() -> Path:
"""
Get workflows directory.
Returns:
Path to ~/.praisonai/workflows/
"""
return get_data_dir() / "workflows"
def get_summaries_dir() -> Path:
"""
Get summaries directory for RAG.
Returns:
Path to ~/.praisonai/summaries/
"""
return get_data_dir() / "summaries"
def get_prp_dir() -> Path:
"""
Get PRP (Prompt Response Pair) output directory.
Returns:
Path to ~/.praisonai/prp/
"""
return get_data_dir() / "prp"
def get_runs_dir() -> Path:
"""
Get runs directory for artifacts.
Returns:
Path to ~/.praisonai/runs/
"""
return get_data_dir() / "runs"
def get_project_data_dir(project_path: Optional[Union[str, Path]] = None) -> Path:
"""
Get project-level data directory.
Args:
project_path: Project root (defaults to cwd)
Returns:
Path to .praisonai/ in project
Example:
>>> from praisonaiagents.paths import get_project_data_dir
>>> project_dir = get_project_data_dir("/path/to/project")
>>> print(project_dir)
/path/to/project/.praisonai
"""
if project_path is None:
base = Path.cwd()
elif isinstance(project_path, str):
base = Path(project_path)
else:
base = project_path
return base / DEFAULT_DIR_NAME
def get_project_sessions_dir(project_path: Optional[Union[str, Path]] = None) -> Path:
"""
Get project-level sessions directory.
Args:
project_path: Project root (defaults to cwd)
Returns:
Path to .praisonai/sessions/ in project
"""
return get_project_data_dir(project_path) / "sessions"
def get_project_knowledge_dir(project_path: Optional[Union[str, Path]] = None) -> Path:
"""
Get project-level knowledge directory.
Args:
project_path: Project root (defaults to cwd)
Returns:
Path to .praisonai/knowledge/ in project
"""
return get_project_data_dir(project_path) / "knowledge"
def get_project_summaries_dir(project_path: Optional[Union[str, Path]] = None) -> Path:
"""
Get project-level summaries directory for RAG.
Args:
project_path: Project root (defaults to cwd)
Returns:
Path to .praisonai/summaries/ in project
"""
return get_project_data_dir(project_path) / "summaries"
def get_project_prp_dir(project_path: Optional[Union[str, Path]] = None) -> Path:
"""
Get project-level PRP output directory.
Args:
project_path: Project root (defaults to cwd)
Returns:
Path to .praisonai/prp/ in project
"""
return get_project_data_dir(project_path) / "prp"
def get_config_path() -> Path:
"""
Get path to the main config.yaml file.
Returns:
Path to ~/.praisonai/config.yaml
"""
return get_data_dir() / "config.yaml"
def get_schedules_dir() -> Path:
"""
Get schedules directory.
Returns:
Path to ~/.praisonai/schedules/
"""
return get_data_dir() / "schedules"
def get_storage_path() -> Path:
"""
Get default SQLite storage database path.
Returns:
Path to ~/.praisonai/storage.db
"""
return get_data_dir() / "storage.db"
def ensure_dir(path: Union[str, Path]) -> Path:
"""
Ensure a directory exists, creating it if necessary.
Args:
path: Directory path to ensure exists
Returns:
Path object for the directory
Example:
>>> from praisonaiagents.paths import ensure_dir, get_sessions_dir
>>> sessions = ensure_dir(get_sessions_dir())
"""
if isinstance(path, str):
path = Path(path)
path.mkdir(parents=True, exist_ok=True)
return path
def get_all_paths() -> Dict[str, Path]:
"""
Get all PraisonAI data paths.
Returns:
Dictionary mapping path names to Path objects
Example:
>>> from praisonaiagents.paths import get_all_paths
>>> paths = get_all_paths()
>>> for name, path in paths.items():
... print(f"{name}: {path}")
"""
return {
"data_dir": get_data_dir(),
"sessions": get_sessions_dir(),
"skills": get_skills_dir(),
"plugins": get_plugins_dir(),
"mcp": get_mcp_dir(),
"docs": get_docs_dir(),
"rules": get_rules_dir(),
"permissions": get_permissions_dir(),
"storage": get_storage_dir(),
"storage_db": get_storage_path(),
"schedules": get_schedules_dir(),
"checkpoints": get_checkpoints_dir(),
"snapshots": get_snapshots_dir(),
"learn": get_learn_dir(),
"cache": get_cache_dir(),
"mcp_auth": get_mcp_auth_path(),
"memory": get_memory_dir(),
"workflows": get_workflows_dir(),
"summaries": get_summaries_dir(),
"prp": get_prp_dir(),
"runs": get_runs_dir(),
}