-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathutils.py
More file actions
97 lines (71 loc) · 2.68 KB
/
utils.py
File metadata and controls
97 lines (71 loc) · 2.68 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
from typing import Optional
import os
import sys
import json
import re
from importlib.metadata import version
def get_version():
try:
return version('agentstack')
except (KeyError, FileNotFoundError) as e:
print(e)
return "Unknown version"
def verify_agentstack_project():
if not os.path.isfile('agentstack.json'):
print("\033[31mAgentStack Error: This does not appear to be an AgentStack project."
"\nPlease ensure you're at the root directory of your project and a file named agentstack.json exists. "
"If you're starting a new project, run `agentstack init`\033[0m")
sys.exit(1)
def get_framework(path: Optional[str] = None) -> str:
try:
file_path = 'agentstack.json'
if path is not None:
file_path = path + '/' + file_path
agentstack_data = open_json_file(file_path)
framework = agentstack_data.get('framework')
if framework.lower() not in ['crewai', 'autogen', 'litellm']:
print(term_color("agentstack.json contains an invalid framework", "red"))
return framework
except FileNotFoundError:
print("\033[31mFile agentstack.json does not exist. Are you in the right directory?\033[0m")
sys.exit(1)
def get_telemetry_opt_out(path: Optional[str] = None) -> str:
try:
file_path = 'agentstack.json'
if path is not None:
file_path = path + '/' + file_path
agentstack_data = open_json_file(file_path)
opt_out = agentstack_data.get('telemetry_opt_out', False)
return opt_out
except FileNotFoundError:
print("\033[31mFile agentstack.json does not exist. Are you in the right directory?\033[0m")
sys.exit(1)
def camel_to_snake(name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
def snake_to_camel(s):
return ''.join(word.title() for word in s.split('_'))
def open_json_file(path) -> dict:
with open(path, 'r') as f:
data = json.load(f)
return data
def clean_input(input_string):
special_char_pattern = re.compile(r'[^a-zA-Z0-9\s_]')
return re.sub(special_char_pattern, '', input_string).lower().replace(' ', '_').replace('-', '_')
def term_color(text: str, color: str) -> str:
colors = {
'red': '91',
'green': '92',
'yellow': '93',
'blue': '94',
'purple': '95',
'cyan': '96',
'white': '97'
}
color_code = colors.get(color)
if color_code:
return f"\033[{color_code}m{text}\033[00m"
else:
return text
def is_snake_case(string: str):
return bool(re.match('^[a-z0-9_]+$', string))