-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
162 lines (130 loc) · 4.93 KB
/
Copy pathdeploy.py
File metadata and controls
162 lines (130 loc) · 4.93 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
162
"""Deploy subcommand: generate MCP JSON config for a target IDE."""
from __future__ import annotations
import json
import sys
from pathlib import Path
from vendors.codegrave_directive import CODEGRAVE_SYSTEM_DIRECTIVE
DEPLOY_DEFAULTS: dict[str, Path] = {
"claude": Path(".mcp.json"),
"opencode": Path.home() / ".config" / "opencode" / "opencode.json",
"windsurf": Path.home() / ".codeium" / "windsurf" / "mcp_config.json",
"cline": Path.home() / ".cline" / "mcp.json",
"copilot": Path.home() / ".copilot" / "mcp-config.json",
}
SERVER_KEY = "codegrave"
def _resolve_binary() -> str:
if getattr(sys, "frozen", False):
return sys.executable
binary_name = "codegrave.exe" if sys.platform == "win32" else "codegrave"
cwd_path = Path.cwd() / "dist" / binary_name
if cwd_path.exists():
return str(cwd_path)
script_dir = Path(__file__).resolve().parent
dev_path = script_dir / "dist" / binary_name
if dev_path.exists():
return str(dev_path)
print("Error: Binary not found. Run './please build' first.")
sys.exit(1)
def _load_existing(path: Path) -> dict:
if not path.exists():
return {}
try:
return json.loads(path.read_text(encoding="utf-8"))
except json.JSONDecodeError:
print(f"Warning: {path.name} is corrupted. Backing up and recreating.")
path.rename(path.with_suffix(".json.bak"))
return {}
def _generate_claude_config(binary: str) -> dict:
return {
"mcpServers": {
SERVER_KEY: {
"command": binary,
"args": [],
}
}
}
def _generate_copilot_config(binary: str) -> dict:
"""Copilot CLI uses "type": "local" + "tools": ["*"] schema."""
return {
"mcpServers": {
SERVER_KEY: {
"type": "local",
"command": binary,
"args": [],
"tools": ["*"],
}
}
}
def _generate_opencode_config(binary: str) -> dict:
return {
"mcp": {
SERVER_KEY: {
"type": "local",
"command": [binary],
"enabled": True,
}
},
"agent": {
SERVER_KEY: {
"description": "Autonomous software engineer bound to a localized memory of dead bugs.",
"mode": "primary",
"tools": {
"write": True,
"edit": True,
"bash": True,
},
"prompt": "{file:./prompts/codegrave.txt}",
}
},
"default_agent": SERVER_KEY,
}
def _merge_config(existing: dict, generated: dict) -> dict:
merged = dict(existing)
for top_key in generated:
if top_key in merged and isinstance(merged[top_key], dict) and isinstance(generated[top_key], dict):
merged[top_key] = dict(merged[top_key])
merged[top_key].update(generated[top_key])
else:
merged[top_key] = generated[top_key]
return merged
def _write_json(path: Path, data: dict) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(data, indent=2) + "\n", encoding="utf-8")
def deploy(target: str, dump_path: Path | None = None, binary_path: str | None = None) -> None:
if binary_path is not None:
if not Path(binary_path).exists():
print(f"Error: Binary not found at {binary_path}. Run './please build' first.")
sys.exit(1)
binary = binary_path
else:
binary = _resolve_binary()
if target == "claude":
generated = _generate_claude_config(binary)
elif target == "opencode":
generated = _generate_opencode_config(binary)
elif target == "windsurf":
generated = _generate_claude_config(binary) # same schema as claude
elif target == "cline":
generated = _generate_claude_config(binary) # same schema as claude
elif target == "copilot":
generated = _generate_copilot_config(binary)
else:
print(f"Error: Unknown deploy target '{target}'.")
sys.exit(1)
if dump_path is not None:
_write_json(dump_path, generated)
print(f"MCP config written to {dump_path}")
return
default_path = DEPLOY_DEFAULTS[target]
existing = _load_existing(default_path.resolve())
merged = _merge_config(existing, generated)
_write_json(default_path.resolve(), merged)
if target == "opencode":
prompt_dir = default_path.resolve().parent / "prompts"
prompt_dir.mkdir(parents=True, exist_ok=True)
prompt_file = prompt_dir / "codegrave.txt"
prompt_file.write_text(CODEGRAVE_SYSTEM_DIRECTIVE, encoding="utf-8")
print(f"MCP config written to {default_path.resolve()}")
if target == "opencode":
print(f"Agent prompt written to {default_path.resolve().parent / 'prompts' / 'codegrave.txt'}")
print(f"Server key: {SERVER_KEY} -> {binary}")