re0 / github_brain.py
LocalAIPrototype's picture
Add github_brain.py file
4ba4581 unverified
Raw
History Blame Contribute Delete
2.47 kB
import json
import base64
from github import Github
class GitHubBrain:
def __init__(self, token, repo_name):
"""
GitHubとの接続を初期化します。
token: GitHubの個人アクセストークン
repo_name: 'ユーザー名/リポジトリ名' の形式
"""
self.gh = Github(token)
self.repo = self.gh.get_repo(repo_name)
self.memory_path = "memory.json"
def load_memory(self):
"""GitHub上の memory.json を読み込む"""
try:
content = self.repo.get_contents(self.memory_path)
# GitHubはBase64でデータを返すのでデコードしてJSONに変換
return json.loads(content.decoded_content.decode('utf-8'))
except Exception as e:
print(f"Memory load error: {e}")
# ファイルがない場合は初期状態を返す
return {"history": [], "last_action": ""}
def save_memory(self, data, commit_message="AI Memory Update"):
"""GitHub上の memory.json を更新する"""
try:
content = self.repo.get_contents(self.memory_path)
# JSONを文字列にしてから保存
new_content = json.dumps(data, indent=2, ensure_ascii=False)
self.repo.update_file(
path=self.memory_path,
message=commit_message,
content=new_content,
sha=content.sha
)
except Exception as e:
print(f"Memory save error: {e}")
def get_latest_tasks(self):
"""
GitHubのIssueから未解決のタスク(ラベル: 'task')を取得
"""
tasks = []
try:
# 'task'ラベルがついたオープンなIssueを検索
issues = self.repo.get_issues(state='open', labels=['task'])
for issue in issues:
tasks.append({
"id": issue.number,
"title": issue.title,
"body": issue.body
})
except Exception as e:
print(f"Task fetch error: {e}")
return tasks
def report_to_issue(self, issue_number, comment):
"""Issueに進捗をコメントとして書き込む"""
try:
issue = self.repo.get_issue(number=issue_number)
issue.create_comment(comment)
except Exception as e:
print(f"Report error: {e}")