File size: 2,975 Bytes
d07c56c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/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)