import urllib.request import json import base64 class GitHubWriter: """ TGI Sovereign Component: GitHub Write Bridge Allows the OS to commit changes to its own repository. """ def __init__(self, api_bridge): self.api = api_bridge def commit_file(self, repo, path, content, message="TGI Self-Modification"): """ Creates or updates a file in the GitHub repository. """ token = self.api.vault.access_data("GITHUB_TOKEN", self.api.m, self.api.k) if not token: return "Access Denied." url = f"https://api.github.com/repos/{repo}/contents/{path}" print(f"\n[*] [GITHUB WRITER]: Committing to {path} in {repo}...") # 1. Check if file exists to get SHA sha = None try: get_req = urllib.request.Request(url, headers={"Authorization": f"token {token}"}) with urllib.request.urlopen(get_req) as res: sha = json.loads(res.read())['sha'] except: pass # File might not exist # 2. Prepare Payload data = { "message": message, "content": base64.b64encode(content.encode()).decode(), "branch": "main" } if sha: data["sha"] = sha # 3. Execute PUT request req = urllib.request.Request(url, data=json.dumps(data).encode(), headers={ "Authorization": f"token {token}", "Content-Type": "application/json" }, method="PUT") try: with urllib.request.urlopen(req) as res: print(f" [✓] COMMIT SUCCESSFUL.") return json.loads(res.read()) except Exception as e: print(f" [-] COMMIT FAILED: {str(e)}") return None if __name__ == "__main__": from fso_external_api_bridge import ExternalAPIBridge from fso_token_vault_setup import setup_token_vault v = setup_token_vault() api = ExternalAPIBridge(v) writer = GitHubWriter(api) print("\n--- [GITHUB WRITER DEMO]: Ready for Self-Modification ---")