-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
216 lines (168 loc) · 6.76 KB
/
Copy pathconfig.py
File metadata and controls
216 lines (168 loc) · 6.76 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""Configuration -- config.json (agent) + mcp.json (MCP servers)."""
from __future__ import annotations
import json
import logging
import os
from dataclasses import dataclass, field
from pathlib import Path
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Bundled data helpers
# ---------------------------------------------------------------------------
_PACKAGE_DIR = Path(__file__).resolve().parent
_PACKAGE_DATA = _PACKAGE_DIR / "data"
USER_DIR = Path.home() / ".mtv-agent"
def bundled_data_path(subpath: str) -> Path:
"""Resolve a path inside the bundled ``data/`` directory."""
return _PACKAGE_DATA / subpath
def bundled_config_example() -> Path:
return bundled_data_path("config.json.example")
def bundled_mcp_example() -> Path:
return bundled_data_path("mcp.json.example")
def bundled_policies_example() -> Path:
return bundled_data_path("policies.json.example")
# ---------------------------------------------------------------------------
# Config file discovery
# ---------------------------------------------------------------------------
_CONFIG_SEARCH_PATHS = [
Path("config.json"),
USER_DIR / "config.json",
]
_MCP_SEARCH_PATHS = [
Path("mcp.json"),
USER_DIR / "mcp.json",
]
def _find_file(name: str, override: str | None = None) -> Path | None:
if override:
p = Path(override).expanduser()
return p if p.is_file() else None
search = _MCP_SEARCH_PATHS if name == "mcp.json" else _CONFIG_SEARCH_PATHS
for p in search:
resolved = p.expanduser()
if resolved.is_file():
return resolved
return None
def _load_json(name: str, override: str | None = None) -> dict:
path = _find_file(name, override)
if not path:
search = _MCP_SEARCH_PATHS if name == "mcp.json" else _CONFIG_SEARCH_PATHS
locations = "\n".join(f" - {p.expanduser().resolve()}" for p in search)
raise SystemExit(
f"Error: {name} not found.\n\n"
f"Searched:\n{locations}\n\n"
f"Run 'mtv-agent init' to create a default configuration."
)
with open(path) as f:
return json.load(f)
# ---------------------------------------------------------------------------
# MCP server config
# ---------------------------------------------------------------------------
@dataclass
class MCPServerConfig:
"""One MCP server entry from mcp.json."""
name: str
transport: str = "http" # "http" or "stdio"
url: str | None = None
headers: dict[str, str] = field(default_factory=dict)
command: str | None = None
args: list[str] = field(default_factory=list)
env: dict[str, str] | None = None
def load_mcp_servers(override: str | None = None) -> list[MCPServerConfig]:
"""Parse mcp.json and return a list of server configs.
If an explicit override path is given but doesn't exist, returns an empty
list (useful for testing or running without external MCP servers).
"""
if override:
path = Path(override).expanduser()
if not path.is_file():
return []
with open(path) as f:
data = json.load(f)
else:
data = _load_json("mcp.json")
servers: list[MCPServerConfig] = []
for name, entry in data.get("mcpServers", {}).items():
if not entry.get("enabled", True):
continue
servers.append(
MCPServerConfig(
name=name,
transport=entry.get("transport", "http"),
url=entry.get("url"),
headers=entry.get("headers", {}),
command=entry.get("command"),
args=entry.get("args", []),
env=entry.get("env"),
)
)
return servers
# ---------------------------------------------------------------------------
# Application settings
# ---------------------------------------------------------------------------
_BUNDLED_SKILLS = str(_PACKAGE_DATA / "skills")
_BUNDLED_COMMANDS = str(_PACKAGE_DATA / "commands")
@dataclass
class Settings:
"""Flat application settings loaded from config.json + env."""
llm_base_url: str = "http://localhost:1234/v1"
llm_api_key: str = "not-needed"
llm_model: str | None = None
host: str = "0.0.0.0"
port: int = 8000
skills_dir: str = _BUNDLED_SKILLS
commands_dir: str = _BUNDLED_COMMANDS
cache_dir: str = "~/.mtv-agent/cache"
max_iterations: int = 20
max_history_chars: int = 160_000
mcp_config: str | None = None
dump_llm: bool = False
dump_dir: str = "~/.mtv-agent/dumps"
def load_settings(override: str | None = None) -> Settings:
"""Load settings from config.json, falling back to defaults."""
data = _load_json("config.json", override)
llm = data.get("llm", {})
srv = data.get("server", {})
skills = data.get("skills", {})
commands = data.get("commands", {})
cache = data.get("cache", {})
agent = data.get("agent", {})
debug = data.get("debug", {})
return Settings(
llm_base_url=llm.get("baseUrl", Settings.llm_base_url),
llm_api_key=llm.get("apiKey", Settings.llm_api_key),
llm_model=llm.get("model"),
host=os.environ.get("MTV_AGENT_HOST", srv.get("host", Settings.host)),
port=int(os.environ.get("MTV_AGENT_PORT", srv.get("port", Settings.port))),
skills_dir=skills.get("dir", Settings.skills_dir),
commands_dir=commands.get("dir", Settings.commands_dir),
cache_dir=cache.get("dir", Settings.cache_dir),
max_iterations=agent.get("maxIterations", Settings.max_iterations),
max_history_chars=agent.get("maxHistoryChars", Settings.max_history_chars),
dump_llm=debug.get("dumpLlm", Settings.dump_llm),
dump_dir=debug.get("dumpDir", Settings.dump_dir),
)
# ---------------------------------------------------------------------------
# TUI settings (read/write by the client side)
# ---------------------------------------------------------------------------
DEFAULT_THEME = "textual-dark"
def load_tui_theme() -> str:
"""Read the TUI theme from config.json (best-effort, returns default on error)."""
path = _find_file("config.json")
if not path:
return DEFAULT_THEME
try:
data = json.loads(path.read_text())
return data.get("tui", {}).get("theme", DEFAULT_THEME)
except (json.JSONDecodeError, OSError):
return DEFAULT_THEME
def save_tui_theme(theme: str) -> None:
"""Persist the TUI theme into config.json."""
path = _find_file("config.json")
if not path:
path = USER_DIR / "config.json"
try:
data = json.loads(path.read_text())
except (json.JSONDecodeError, OSError):
data = {}
data.setdefault("tui", {})["theme"] = theme
path.write_text(json.dumps(data, indent=2) + "\n")