Repository navigation
Expand file tree
/
Copy pathconfig.py
More file actions
154 lines (124 loc) · 4.98 KB
/
Copy pathconfig.py
File metadata and controls
154 lines (124 loc) · 4.98 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
"""Configuration discovery for Feishu Bridge.
Resolution order:
1. Explicit path (--config argument)
2. $FEISHU_BRIDGE_CONFIG environment variable
3. ~/.config/feishu-bridge/config.json (XDG standard)
If no config exists and stdin is a TTY, an interactive setup wizard runs.
"""
import json
import logging
import os
import shutil
import sys
from pathlib import Path
log = logging.getLogger("feishu-bridge")
_XDG_CONFIG_PATH = Path.home() / ".config" / "feishu-bridge" / "config.json"
def _prompt(label: str) -> str:
"""Read one line from stdin; exit cleanly on EOF (Ctrl-D)."""
try:
return input(label)
except EOFError:
print("\n 已取消。", file=sys.stderr)
raise SystemExit(1)
def _validate_credential(value: str, name: str) -> str:
"""Reject empty or multi-line credential values."""
value = value.strip()
if not value:
print(f" {name} 不能为空", file=sys.stderr)
raise SystemExit(1)
if "\n" in value or "\r" in value or "\t" in value:
print(f" {name} 不能包含换行或制表符", file=sys.stderr)
raise SystemExit(1)
return value
def _interactive_setup(bot_name: str) -> str:
"""Guide the user through first-time config creation. Returns config path."""
print("\n✦ Feishu Bridge 首次配置向导\n")
print(" 请先在飞书开放平台创建机器人:")
print(" https://open.feishu.cn/page/openclaw?form=multiAgent\n")
app_id = _validate_credential(_prompt(" App ID: "), "App ID")
app_secret = _validate_credential(_prompt(" App Secret: "), "App Secret")
default_ws = str(
Path.home() / ".local" / "share" / "feishu-bridge" / "workspaces" / bot_name
)
ws_input = _prompt(f" 工作目录 [{default_ws}]: ").strip()
workspace = ws_input or default_ws
# Agent type selection
agent_type_input = _prompt(" Agent 类型 [claude/codex] (claude): ").strip().lower()
agent_type = agent_type_input if agent_type_input in ("claude", "codex") else "claude"
agent_cmd = agent_type # "claude" or "codex"
if not shutil.which(agent_cmd):
print(f"\n ⚠️ 命令 '{agent_cmd}' 未在 PATH 中找到。"
f"请安装后重试或在配置中设置绝对路径。", file=sys.stderr)
raise SystemExit(1)
agent_cfg = {"type": agent_type, "command": agent_cmd, "timeout_seconds": 300}
config = {
"bots": [
{
"name": bot_name,
"app_id": "${FEISHU_APP_ID}",
"app_secret": "${FEISHU_APP_SECRET}",
"workspace": workspace,
"allowed_users": ["*"],
}
],
"agent": agent_cfg,
}
config_dir = _XDG_CONFIG_PATH.parent
config_dir.mkdir(parents=True, exist_ok=True)
# Credentials → .env with atomic 0o600 permissions (no TOCTOU race)
env_file = config_dir / ".env"
env_content = f"FEISHU_APP_ID={app_id}\nFEISHU_APP_SECRET={app_secret}\n"
fd = os.open(str(env_file), os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
with os.fdopen(fd, "w") as fh:
fh.write(env_content)
os.chmod(str(env_file), 0o600) # enforce perms even if file pre-existed
# Load into current process so ${VAR} substitution works immediately
os.environ.setdefault("FEISHU_APP_ID", app_id)
os.environ.setdefault("FEISHU_APP_SECRET", app_secret)
_XDG_CONFIG_PATH.write_text(
json.dumps(config, indent=2, ensure_ascii=False) + "\n"
)
print(f"\n 凭证已写入 {env_file}")
print(f" 配置已写入 {_XDG_CONFIG_PATH}\n")
return str(_XDG_CONFIG_PATH)
def resolve_config_path(explicit: str | None = None,
bot_name: str | None = None) -> str:
"""Find config file using the discovery chain.
Args:
explicit: Path passed via --config CLI argument (highest priority).
bot_name: Bot name from --bot, used for interactive setup.
Returns:
Resolved config file path.
Raises:
SystemExit: If no config file is found.
"""
# 1. Explicit --config
if explicit:
p = Path(explicit).expanduser()
if p.exists():
return str(p)
log.error("Config file not found: %s", explicit)
raise SystemExit(1)
# 2. Environment variable
env_path = os.environ.get("FEISHU_BRIDGE_CONFIG")
if env_path:
p = Path(env_path).expanduser()
if p.exists():
return str(p)
log.error("$FEISHU_BRIDGE_CONFIG points to non-existent file: %s", env_path)
raise SystemExit(1)
# 3. XDG standard path
if _XDG_CONFIG_PATH.exists():
return str(_XDG_CONFIG_PATH)
# 4. Interactive setup (TTY only)
if sys.stdin.isatty() and bot_name:
return _interactive_setup(bot_name)
# No config found and not interactive
log.error(
"No config file found. Provide one via:\n"
" --config <path>\n"
" $FEISHU_BRIDGE_CONFIG environment variable\n"
" %s",
_XDG_CONFIG_PATH,
)
raise SystemExit(1)