code-datascience / scripts /hf_pull_script.py
delqhi's picture
Deploy code-datascience agent
d07c56c verified
Raw
History Blame Contribute Delete
2.98 kB
#!/usr/bin/env python3
"""
A2A Consumer HF VM Bootstrap Script
Prepares the HF VM runtime environment for agent deployment.
No longer pulls OpenAI tokens — uses qwen3.6-plus-free via opencode CLI.
"""
from __future__ import annotations
import json
import os
import subprocess
import sys
from pathlib import Path
AGENT_ROOT = Path(__file__).resolve().parent.parent
AUTH_FILE = Path.home() / ".local" / "share" / "opencode" / "auth.json"
def bootstrap_hf_vm() -> bool:
"""Prepare the HF VM runtime for the agent."""
print(f"[hf-pull] Bootstrapping HF VM for agent at {AGENT_ROOT}")
# Ensure required directories exist
for dirpath in [
Path.home() / ".local" / "share" / "opencode",
Path.home() / ".config" / "sin",
AGENT_ROOT / "logs",
AGENT_ROOT / "data",
]:
dirpath.mkdir(parents=True, exist_ok=True)
print(f"[hf-pull] Ensured directory: {dirpath}")
# Write minimal auth.json with placeholder (opencode CLI handles auth)
auth_data = {
"openai": {
"type": "oauth",
"refresh": "opencode-managed"
}
}
AUTH_FILE.parent.mkdir(parents=True, exist_ok=True)
AUTH_FILE.write_text(json.dumps(auth_data, indent=2) + "\n")
print(f"[hf-pull] Wrote placeholder auth to {AUTH_FILE}")
# Verify node is available
try:
result = subprocess.run(["node", "--version"], capture_output=True, text=True, timeout=5)
print(f"[hf-pull] Node.js available: {result.stdout.strip()}")
except FileNotFoundError:
print("[hf-pull] WARNING: node not found — install Node.js 18+")
return False
# Install dependencies if node_modules missing
pkg_lock = AGENT_ROOT / "package-lock.json"
node_modules = AGENT_ROOT / "node_modules"
if pkg_lock.exists() and not node_modules.exists():
print("[hf-pull] Installing dependencies...")
result = subprocess.run(
["npm", "ci", "--production"],
cwd=str(AGENT_ROOT),
capture_output=True,
text=True,
timeout=300
)
if result.returncode != 0:
print(f"[hf-pull] npm ci failed: {result.stderr}")
return False
print("[hf-pull] Dependencies installed")
# Build if dist missing
dist_dir = AGENT_ROOT / "dist"
if not dist_dir.exists() and (AGENT_ROOT / "tsconfig.json").exists():
print("[hf-pull] Building agent...")
result = subprocess.run(
["npm", "run", "build"],
cwd=str(AGENT_ROOT),
capture_output=True,
text=True,
timeout=300
)
if result.returncode != 0:
print(f"[hf-pull] Build failed: {result.stderr}")
return False
print("[hf-pull] Build complete")
print("[hf-pull] HF VM bootstrap complete")
return True
if __name__ == "__main__":
success = bootstrap_hf_vm()
sys.exit(0 if success else 1)