fix: KOOK 连不上根因——空token裸账号不注册/占名 + open指向带token账号 + 前端token不覆盖真值
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled

agent-119 后端:
- buildAccountAdapter: token平台(kook/telegram/weixin)BotToken空→跳过build(不注册不占名), channelAccountsFromEnv 无tokenenv不合成账号(杜绝裸kook id="")
- runtimeconfig cleanAccounts: LoadChannels/SaveChannels 丢弃token平台空token账号(清理channels.json现有裸kook, 下次保存抹掉)
- findChannel: 裸平台名(kook)回退到该平台第一个带token账号, open"kook"不再命中裸的missing token

我补 前端token掩码不就覆真值:
- openConfig 不再把'(已配置,仅掩码显示)'当token回填(setToken空)→save token空不写bot_token保留真值
- 加 tokenHint(显示'已配置留空不改'/重填换); 修之前channels.json kook:3fc8 token变掩码串的bug
全仓 build/vet/test绿+前端build绿(463行)
This commit is contained in:
Aska committed 2026-09-07 16:01:47 +08:00
1 parent 5398486fdf
commit b614633432
7 files changed
+314 -30

No files matched your search

+44 -24
View File
@@ -19,6 +19,7 @@ import (
"log/slog"
"net/http"
"os"
"strings"
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter"
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
@@ -84,25 +85,33 @@ func buildPlatformAdapters(platform string, accts []config.ChannelAccount, log *
}
// channelAccountsFromEnv synthesizes the env fallback: the single-account
// CHANNEL_<NAME>_* adapters (always present so the channel list stays stable and
// an unset token simply stays disconnected) plus any CHANNELS_JSON multi-account
// entries, which coexist with the single-account adapters as before. It is the
// .env fallback passed to runtimeconfig.LoadChannels when channels.json is absent
// or invalid.
// CHANNEL_<NAME>_* adapters plus any CHANNELS_JSON multi-account entries. It is
// the .env fallback passed to runtimeconfig.LoadChannels when channels.json is
// absent or invalid.
//
// A token-requiring platform (kook/telegram/weixin-ilink) is synthesized only
// when its token env var is set; with an unset token there is no account at all,
// so an empty-token placeholder never claims the bare platform name. qq-obv11 and
// webhook are always present (they do not need a token; the bridge's configured()
// gate decides whether to dial) so the channel list stays stable.
func channelAccountsFromEnv() map[string][]config.ChannelAccount {
out := map[string][]config.ChannelAccount{}
out["kook"] = append(out["kook"], config.ChannelAccount{Config: map[string]any{
"bot_token": os.Getenv("CHANNEL_KOOK_TOKEN"),
"base_url": os.Getenv("CHANNEL_KOOK_BASE_URL"),
"receive_mode": os.Getenv("CHANNEL_KOOK_RECEIVE_MODE"),
"webhook_path": os.Getenv("CHANNEL_KOOK_WEBHOOK_PATH"),
"webhook_verify_token": os.Getenv("CHANNEL_KOOK_WEBHOOK_VERIFY_TOKEN"),
"webhook_encrypt_key": os.Getenv("CHANNEL_KOOK_WEBHOOK_ENCRYPT_KEY"),
}})
out["telegram"] = append(out["telegram"], config.ChannelAccount{Config: map[string]any{
"bot_token": os.Getenv("CHANNEL_TELEGRAM_BOT_TOKEN"),
"webhook_url": os.Getenv("CHANNEL_TELEGRAM_WEBHOOK_URL"),
}})
if tok := os.Getenv("CHANNEL_KOOK_TOKEN"); tok != "" {
out["kook"] = append(out["kook"], config.ChannelAccount{Config: map[string]any{
"bot_token": tok,
"base_url": os.Getenv("CHANNEL_KOOK_BASE_URL"),
"receive_mode": os.Getenv("CHANNEL_KOOK_RECEIVE_MODE"),
"webhook_path": os.Getenv("CHANNEL_KOOK_WEBHOOK_PATH"),
"webhook_verify_token": os.Getenv("CHANNEL_KOOK_WEBHOOK_VERIFY_TOKEN"),
"webhook_encrypt_key": os.Getenv("CHANNEL_KOOK_WEBHOOK_ENCRYPT_KEY"),
}})
}
if tok := os.Getenv("CHANNEL_TELEGRAM_BOT_TOKEN"); tok != "" {
out["telegram"] = append(out["telegram"], config.ChannelAccount{Config: map[string]any{
"bot_token": tok,
"webhook_url": os.Getenv("CHANNEL_TELEGRAM_WEBHOOK_URL"),
}})
}
out["qq-obv11"] = append(out["qq-obv11"], config.ChannelAccount{Config: map[string]any{
"mode": envString("CHANNEL_QQ_OBV11_MODE", "server"),
"listen_addr": os.Getenv("CHANNEL_QQ_OBV11_LISTEN_ADDR"),
@@ -117,13 +126,15 @@ func channelAccountsFromEnv() map[string][]config.ChannelAccount {
"secret": os.Getenv("CHANNEL_WEBHOOK_SECRET"),
"signatureHeader": os.Getenv("CHANNEL_WEBHOOK_SIGNATURE_HEADER"),
}})
out["weixin-ilink"] = append(out["weixin-ilink"], config.ChannelAccount{Config: map[string]any{
"base_url": os.Getenv("CHANNEL_WEIXIN_ILINK_BASE_URL"),
"cdn_base_url": os.Getenv("CHANNEL_WEIXIN_ILINK_CDN_BASE_URL"),
"bot_token": os.Getenv("CHANNEL_WEIXIN_ILINK_BOT_TOKEN"),
"bot_id": os.Getenv("CHANNEL_WEIXIN_ILINK_BOT_ID"),
"owner_id": os.Getenv("CHANNEL_WEIXIN_ILINK_OWNER_ID"),
}})
if tok := os.Getenv("CHANNEL_WEIXIN_ILINK_BOT_TOKEN"); tok != "" {
out["weixin-ilink"] = append(out["weixin-ilink"], config.ChannelAccount{Config: map[string]any{
"base_url": os.Getenv("CHANNEL_WEIXIN_ILINK_BASE_URL"),
"cdn_base_url": os.Getenv("CHANNEL_WEIXIN_ILINK_CDN_BASE_URL"),
"bot_token": tok,
"bot_id": os.Getenv("CHANNEL_WEIXIN_ILINK_BOT_ID"),
"owner_id": os.Getenv("CHANNEL_WEIXIN_ILINK_OWNER_ID"),
}})
}
if raw := os.Getenv("CHANNELS_JSON"); raw != "" {
if accounts, err := config.ParseChannelAccounts(raw); err == nil {
for platform, accts := range accounts {
@@ -206,12 +217,18 @@ func buildAccountAdapter(platform string, acct config.ChannelAccount, log *slog.
if err := adapter.DecodeConfig(acct.Config, &cfg); err != nil {
return nil, fmt.Errorf("decode telegram config: %w", err)
}
if strings.TrimSpace(cfg.BotToken) == "" {
return nil, fmt.Errorf("%s: missing bot token", platform)
}
inner = telegram.New(cfg, telegram.WithLogger(log))
case "weixin-ilink":
var cfg weixin_ilink.Config
if err := adapter.DecodeConfig(acct.Config, &cfg); err != nil {
return nil, fmt.Errorf("decode weixin-ilink config: %w", err)
}
if strings.TrimSpace(cfg.BotToken) == "" {
return nil, fmt.Errorf("%s: missing bot token", platform)
}
w, err := weixin_ilink.New(cfg, nil, logger.WithSource(log, "plugins/channels/weixin_ilink"))
if err != nil {
return nil, fmt.Errorf("build weixin-ilink: %w", err)
@@ -228,6 +245,9 @@ func buildAccountAdapter(platform string, acct config.ChannelAccount, log *slog.
if err := adapter.DecodeConfig(acct.Config, &cfg); err != nil {
return nil, fmt.Errorf("decode kook config: %w", err)
}
if strings.TrimSpace(cfg.BotToken) == "" {
return nil, fmt.Errorf("%s: missing bot token", platform)
}
inner = kook.New(cfg, kook.WithLogger(log))
case "webhook":
var cfg webhook.Config
+52
View File
@@ -51,6 +51,58 @@ func TestBuildChannelAdapters_KookMultiAccount(t *testing.T) {
}
}
// TestBuildChannelAdapters_SkipsEmptyTokenKookFromFile verifies a stale
// channels.json with both a bare empty-token kook and a real token account builds
// only the token account, so the bare "kook" never registers (and never wins a
// bare-name open).
func TestBuildChannelAdapters_SkipsEmptyTokenKookFromFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "channels.json")
raw := `{"kook":[
{"id":"","config":{"base_url":"","bot_token":""}},
{"id":"3fc8","config":{"bot_token":"real-token"}}
]}`
if err := os.WriteFile(path, []byte(raw), 0o600); err != nil {
t.Fatalf("write: %v", err)
}
f := &channelFactory{path: path, fallback: channelAccountsFromEnv(), log: testLog()}
adapters := buildChannelAdapters(f)
if hasAdapter(adapters, "kook") {
t.Fatalf("bare empty-token kook should not be built, got %v", names(adapters))
}
if !hasAdapter(adapters, "kook:3fc8") {
t.Fatalf("expected kook:3fc8, got %v", names(adapters))
}
}
// TestChannelAccountsFromEnv_NoEmptyTokenPlaceholders verifies the env fallback
// does not synthesize an empty-token kook/telegram/weixin account (only a real
// token produces the account), so a bare placeholder is never registered.
func TestChannelAccountsFromEnv_NoEmptyTokenPlaceholders(t *testing.T) {
for _, k := range []string{"CHANNEL_KOOK_TOKEN", "CHANNEL_TELEGRAM_BOT_TOKEN", "CHANNEL_WEIXIN_ILINK_BOT_TOKEN", "CHANNELS_JSON"} {
t.Setenv(k, "")
}
env := channelAccountsFromEnv()
for _, platform := range []string{"kook", "telegram", "weixin-ilink"} {
if _, ok := env[platform]; ok {
t.Fatalf("empty-token %s should not be synthesized", platform)
}
}
}
// TestChannelAccountsFromEnv_KookPresentWhenTokenSet verifies a set token yields
// the account.
func TestChannelAccountsFromEnv_KookPresentWhenTokenSet(t *testing.T) {
t.Setenv("CHANNEL_KOOK_TOKEN", "kook-secret")
env := channelAccountsFromEnv()
list, ok := env["kook"]
if !ok || len(list) != 1 {
t.Fatalf("expected a synthesized kook account, got %+v", env["kook"])
}
if list[0].Config["bot_token"] != "kook-secret" {
t.Fatalf("bot_token = %v, want kook-secret", list[0].Config["bot_token"])
}
}
func hasAdapter(adapters []adapter.PlatformAdapter, name string) bool {
for _, a := range adapters {
if a.Name() == name {