Wizard-Vibe-Studio / github_bridge.py
dryymatt's picture
πŸ§™β€β™‚οΈ Wizard-Vibe Studio: github_bridge.py
a81f88c verified
"""
╔══════════════════════════════════════════════════════════╗
β•‘ AUTONOMOUS GITHUB BRIDGE β•‘
β•‘ β•‘
β•‘ Access Protocol: β•‘
β•‘ 1. SCAN env for GITHUB_TOKEN β•‘
β•‘ 2. If absent β†’ HF Hub as canonical source β•‘
β•‘ 3. Generate git-bundle for offline mirroring β•‘
β•‘ 4. Auto-detect token on next cycle β†’ push β•‘
β•‘ β•‘
β•‘ Target: github.com/M523zappin/Wizard-Vibe-Studio β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
"""
import asyncio, json, os, base64, shutil, subprocess
from pathlib import Path
from typing import Dict
import aiohttp
TARGET_GITHUB = "M523zappin/Wizard-Vibe-Studio"
TARGET_URL = f"https://github.com/{TARGET_GITHUB}"
CANONICAL_HF = "dryymatt/Wizard-Vibe-Studio"
GEN_DIR = Path(__file__).parent / "generated"
class GitHubBridge:
"""Autonomous publisher with fallback chain."""
def __init__(self):
self.token = os.environ.get("GITHUB_TOKEN") or os.environ.get("HF_TOKEN")
self.api = "https://api.github.com"
async def publish(self, repo_name: str, files: Dict[str, str], description: str = "") -> Dict:
"""Publish to GitHub with automatic fallback to HF Hub."""
result = {"success": False, "url": None, "method": None}
# PATH 1: GitHub API
if self.token:
gh = await self._push_github(repo_name, files, description)
if gh["success"]:
result.update(gh); result["method"] = "github-api-direct"
return result
result["github_error"] = gh.get("error")
# PATH 2: HF Hub (always works)
hf = await self._push_hf(repo_name, files, description)
if hf["success"]:
result.update(hf); result["method"] = "hf-hub-bridge"
result["github_target"] = TARGET_URL
result["bundle"] = self._create_bundle(repo_name, files)
return result
# PATH 3: Local
local = self._create_bundle(repo_name, files)
result.update({"success": True, "url": local["path"], "method": "local-bundle"})
return result
async def _push_github(self, repo_name: str, files: Dict, desc: str) -> Dict:
try:
headers = {"Authorization": f"Bearer {self.token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28"}
async with aiohttp.ClientSession() as s:
user = (await s.get(f"{self.api}/user", headers=headers)).json()
owner = user.get("login")
if not owner: return {"success": False, "error": f"Bad token: {user.get('message','')}"}
await s.post(f"{self.api}/user/repos", headers=headers,
json={"name": repo_name, "description": desc or "Wizard-Vibe Studio", "auto_init": True, "private": False})
for path, content in files.items():
await s.put(f"{self.api}/repos/{owner}/{repo_name}/contents/{path}", headers=headers,
json={"message": f"πŸ§™β€β™‚οΈ Wizard-Vibe: {path}", "content": base64.b64encode(content.encode()).decode()})
return {"success": True, "url": f"https://github.com/{owner}/{repo_name}"}
except Exception as e:
return {"success": False, "error": str(e)}
async def _push_hf(self, repo_name: str, files: Dict, desc: str) -> Dict:
try:
from huggingface_hub import HfApi, create_repo
api = HfApi()
full_id = f"dryymatt/{repo_name}"
create_repo(full_id, repo_type='model', private=False, exist_ok=True)
for path, content in files.items():
api.upload_file(path_or_fileobj=content.encode(), path_in_repo=path,
repo_id=full_id, repo_type='model')
return {"success": True, "url": f"https://huggingface.co/{full_id}"}
except Exception as e:
return {"success": False, "error": str(e)}
def _create_bundle(self, repo_name: str, files: Dict) -> Dict:
d = GEN_DIR / repo_name
d.mkdir(parents=True, exist_ok=True)
for p, c in files.items():
fp = d / p; fp.parent.mkdir(parents=True, exist_ok=True); fp.write_text(c)
mirror = f"""#!/bin/bash
# Wizard-Vibe Studio β€” GitHub Mirror Script
# Target: {TARGET_URL}
# Run: GITHUB_TOKEN=ghp_xxx ./mirror.sh
set -e
cd "$(dirname "$0")"
git init && git add . && git commit -m "πŸ§™β€β™‚οΈ Wizard-Vibe Studio β€” First Commit"
git branch -M main
git remote add origin https://{TARGET_GITHUB}.git
echo "βœ… Bundle ready. Push with: git push -u origin main"
"""
(d / "mirror.sh").write_text(mirror); (d / "mirror.sh").chmod(0o755)
return {"path": str(d), "mirror": "mirror.sh"}
def generate_agent_card(self, name: str, desc: str, url: str) -> Dict:
return {
"name": name,
"description": desc or "Wizard-Vibe Studio β€” sandbox-first autonomous creator",
"url": url or TARGET_URL,
"provider": {"organization": "Wizard-Vibe Studio / M523zappin", "url": "https://huggingface.co/dryymatt"},
"version": "1.0.0", "a2aVersion": "1.0",
"capabilities": {"streaming": True},
"skills": [
{"id": "code-gen", "name": "Code Generation", "tags": ["wizard-vibe","sse","streaming"]},
{"id": "reflect-select", "name": "Self-Healing", "tags": ["reflect-select"]},
{"id": "autonomous-publish", "name": "Autonomous Publish", "tags": ["github","hf","a2a"]},
],
}
# ─── Singleton ────────────────────────────────────────
bridge = GitHubBridge()