40 lines
1.7 KiB
Python
40 lines
1.7 KiB
Python
import os
|
|
import json
|
|
|
|
class GitHubTool:
|
|
def __init__(self):
|
|
self.base_url = "https://api.github.com"
|
|
self.token = os.environ.get("GITHUB_TOKEN")
|
|
self.repo = os.environ.get("GITHUB_REPOSITORY")
|
|
self.current_branch = "main" # Default to main branch
|
|
|
|
self.instances = {}
|
|
self.functions = []
|
|
self._load_functions()
|
|
|
|
def _load_functions(self):
|
|
function_dir = os.path.join(os.path.dirname(__file__), "github_tool_functions")
|
|
for filename in os.listdir(function_dir):
|
|
if filename.endswith(".py") and filename != "__init__.py":
|
|
function_name = filename[:-3]
|
|
module = __import__(f"tools.github_tool_functions.{function_name}", fromlist=[function_name.capitalize()])
|
|
class_name = getattr(module, function_name.capitalize())
|
|
self.instances[function_name] = class_name(self.base_url, self.token, self.repo, self.current_branch)
|
|
|
|
with open(os.path.join(function_dir, filename), 'r') as f:
|
|
content = f.read()
|
|
json_start = content.find('{')
|
|
json_end = content.rfind('}') + 1
|
|
if json_start != -1 and json_end != -1:
|
|
json_def = json.loads(content[json_start:json_end])
|
|
self.functions.append(json_def)
|
|
|
|
def execute(self, function_name, **kwargs):
|
|
if function_name in self.instances:
|
|
return self.instances[function_name](**kwargs)
|
|
else:
|
|
error_message = f"Unknown function: {function_name}"
|
|
return {"error": error_message}
|
|
|
|
def get_functions(self):
|
|
return self.functions |