-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsetup_claude.py
More file actions
161 lines (138 loc) Β· 5.79 KB
/
setup_claude.py
File metadata and controls
161 lines (138 loc) Β· 5.79 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os
import json
import shutil
import subprocess
from pathlib import Path
from utils import discover_serving_endpoints, ensure_https, get_gateway_host, pick_in_geo_model
# Set HOME if not properly set
if not os.environ.get("HOME") or os.environ["HOME"] == "/":
os.environ["HOME"] = "/app/python/source_code"
home = Path(os.environ["HOME"])
# Create ~/.claude directory
claude_dir = home / ".claude"
claude_dir.mkdir(exist_ok=True)
# 1. Write settings.json for Databricks model serving (requires DATABRICKS_TOKEN)
token = os.environ.get("DATABRICKS_TOKEN", "").strip()
if token:
gateway_host = get_gateway_host()
databricks_host = ensure_https(os.environ.get("DATABRICKS_HOST", "").rstrip("/"))
if gateway_host:
anthropic_base_url = f"{gateway_host}/anthropic"
print(f"Using Databricks AI Gateway: {gateway_host}")
else:
anthropic_base_url = f"{databricks_host}/serving-endpoints/anthropic"
print(f"Using Databricks Host: {databricks_host}")
settings_path = claude_dir / "settings.json"
# Read-merge-write to preserve env vars from other setup scripts (e.g. setup_mlflow.py)
if settings_path.exists():
try:
settings = json.loads(settings_path.read_text())
except (json.JSONDecodeError, OSError):
settings = {}
else:
settings = {}
# Discover models actually served at this workspace. The direct serving-
# endpoints list reflects Databricks Geo Designated Services policy β a
# workspace in AU only sees in-geo models, etc. Validating env-set defaults
# against this list avoids configuring Claude Code with a model the gateway
# claims to serve but the user's geo can't access.
available = discover_serving_endpoints(databricks_host, token)
if available:
print(f"Discovered {len(available)} READY serving endpoints at workspace")
requested_model = os.environ.get("ANTHROPIC_MODEL", "databricks-claude-opus-4-7")
active_model = pick_in_geo_model(
[requested_model, "databricks-claude-opus-4-6", "databricks-claude-sonnet-4-6"],
available,
fallback=requested_model,
)
opus_model = pick_in_geo_model(
["databricks-claude-opus-4-7", "databricks-claude-opus-4-6"],
available,
fallback="databricks-claude-opus-4-7",
)
sonnet_model = pick_in_geo_model(
["databricks-claude-sonnet-4-6", "databricks-claude-sonnet-4-5"],
available,
fallback="databricks-claude-sonnet-4-6",
)
haiku_model = pick_in_geo_model(
["databricks-claude-haiku-4-5"],
available,
fallback="databricks-claude-haiku-4-5",
)
if available and active_model != requested_model:
print(f"ANTHROPIC_MODEL={requested_model} not served at this workspace, using {active_model}")
settings.setdefault("env", {})
settings["env"]["ANTHROPIC_MODEL"] = active_model
settings["env"]["ANTHROPIC_BASE_URL"] = anthropic_base_url
settings["env"]["ANTHROPIC_AUTH_TOKEN"] = token
settings["env"]["ANTHROPIC_DEFAULT_OPUS_MODEL"] = opus_model
settings["env"]["ANTHROPIC_DEFAULT_SONNET_MODEL"] = sonnet_model
settings["env"]["ANTHROPIC_DEFAULT_HAIKU_MODEL"] = haiku_model
settings["env"]["ANTHROPIC_CUSTOM_HEADERS"] = "x-databricks-use-coding-agent-mode: true"
settings["env"]["CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS"] = "1"
settings_path.write_text(json.dumps(settings, indent=2))
print(f"Claude configured: {settings_path}")
else:
print("No DATABRICKS_TOKEN β skipping settings.json (will be configured after PAT setup)")
# 2. Write ~/.claude.json with onboarding skip AND MCP servers
mcp_servers = {
"deepwiki": {
"type": "http",
"url": "https://mcp.deepwiki.com/mcp"
},
"exa": {
"type": "http",
"url": "https://mcp.exa.ai/mcp"
}
}
# Auto-configure team-memory MCP if URL is provided
team_memory_url = os.environ.get("TEAM_MEMORY_MCP_URL", "").strip().rstrip("/")
if team_memory_url:
mcp_servers["team-memory"] = {
"type": "http",
"url": f"{team_memory_url}/mcp"
}
print(f"Team memory MCP configured: {team_memory_url}/mcp")
claude_json = {
"hasCompletedOnboarding": True,
"mcpServers": mcp_servers
}
claude_json_path = home / ".claude.json"
claude_json_path.write_text(json.dumps(claude_json, indent=2))
print(f"Onboarding skipped + MCPs configured: {claude_json_path}")
# 3. Install Claude Code CLI if not present
local_bin = home / ".local" / "bin"
claude_bin = local_bin / "claude"
print("Installing/upgrading Claude Code CLI...")
result = subprocess.run(
["bash", "-c", "curl -fsSL https://claude.ai/install.sh | bash"],
env={**os.environ, "HOME": str(home)},
capture_output=True,
text=True
)
if result.returncode == 0:
print("Claude Code CLI installed successfully")
else:
print(f"CLI install warning: {result.stderr}")
# 4. Copy subagent definitions to ~/.claude/agents/
# These enable TDD workflow: prd-writer β test-generator β implementer β build-feature
agents_src = Path(__file__).parent / "agents"
agents_dst = claude_dir / "agents"
agents_dst.mkdir(exist_ok=True)
if agents_src.exists():
copied = []
for agent_file in agents_src.glob("*.md"):
shutil.copy2(str(agent_file), str(agents_dst / agent_file.name))
copied.append(agent_file.name)
if copied:
print(f"Subagents installed: {', '.join(copied)}")
else:
print("No agents directory found, skipping subagent setup")
# 5. Create projects directory
projects_dir = home / "projects"
projects_dir.mkdir(exist_ok=True)
print(f"Projects directory: {projects_dir}")
# 5. Git identity and hooks are now configured by app.py's _setup_git_config()
# (runs directly in Python before setup_claude.py, writes ~/.gitconfig and ~/.githooks/)
print("Git identity and hooks: configured by app.py (skipping here)")