78 lines
2.0 KiB
Python
78 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import re
|
|
from pathlib import Path
|
|
|
|
|
|
BEGIN = "# BEGIN telegram-codex-bridge managed MCP"
|
|
END = "# END telegram-codex-bridge managed MCP"
|
|
|
|
|
|
def main() -> None:
|
|
codex_home = Path(os.getenv("CODEX_HOME", "/root/.codex"))
|
|
codex_home.mkdir(parents=True, exist_ok=True)
|
|
config_path = codex_home / "config.toml"
|
|
state_path = os.getenv("BOT_STATE_PATH", "/app/data/state.json")
|
|
timezone = os.getenv("BOT_TIMEZONE", "Asia/Seoul")
|
|
gitea_env = _mcp_env_lines(
|
|
[
|
|
"GITEA_BASE_URL",
|
|
"GITEA_USERNAME",
|
|
"GITEA_EMAIL",
|
|
"GITEA_TOKEN",
|
|
"GITEA_DEFAULT_OWNER",
|
|
"GITEA_DEFAULT_REPO",
|
|
]
|
|
)
|
|
|
|
block = f"""{BEGIN}
|
|
[mcp_servers.telegram_bridge]
|
|
command = "python"
|
|
args = ["-m", "app.bridge_mcp"]
|
|
tool_timeout_sec = 30.0
|
|
default_tools_approval_mode = "auto"
|
|
|
|
[mcp_servers.telegram_bridge.tools.telegram_delete_schedule]
|
|
approval_mode = "approve"
|
|
|
|
[mcp_servers.telegram_bridge.tools.telegram_delete_personal_record]
|
|
approval_mode = "approve"
|
|
|
|
[mcp_servers.telegram_bridge.env]
|
|
PYTHONPATH = "/app"
|
|
BOT_STATE_PATH = "{_toml_string(state_path)}"
|
|
BOT_TIMEZONE = "{_toml_string(timezone)}"
|
|
{gitea_env}
|
|
{END}
|
|
"""
|
|
|
|
existing = config_path.read_text(encoding="utf-8") if config_path.exists() else ""
|
|
pattern = re.compile(
|
|
rf"{re.escape(BEGIN)}.*?{re.escape(END)}\s*",
|
|
flags=re.DOTALL,
|
|
)
|
|
if pattern.search(existing):
|
|
updated = pattern.sub(block, existing)
|
|
else:
|
|
updated = existing.rstrip() + "\n\n" + block if existing.strip() else block
|
|
config_path.write_text(updated, encoding="utf-8")
|
|
|
|
|
|
def _toml_string(value: str) -> str:
|
|
return value.replace("\\", "\\\\").replace('"', '\\"')
|
|
|
|
|
|
def _mcp_env_lines(names: list[str]) -> str:
|
|
lines: list[str] = []
|
|
for name in names:
|
|
value = os.getenv(name)
|
|
if value is None:
|
|
continue
|
|
lines.append(f'{name} = "{_toml_string(value)}"')
|
|
return "\n".join(lines)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|