Initial commit
This commit is contained in:
50
app/ensure_codex_config.py
Normal file
50
app/ensure_codex_config.py
Normal file
@@ -0,0 +1,50 @@
|
||||
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")
|
||||
|
||||
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.env]
|
||||
PYTHONPATH = "/app"
|
||||
BOT_STATE_PATH = "{_toml_string(state_path)}"
|
||||
BOT_TIMEZONE = "{_toml_string(timezone)}"
|
||||
{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('"', '\\"')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user