feat: KOOK自动获取bot身份 + action/narration剥出气泡 + 渠道配置表单回填
agent-116 KOOK token即用+自动获取:
- kook/identity.go: Connect 调 GET /user/me 拿 bot 身份(bot_id/bot_username/bot_nickname), 失败容错
- 渠道列表 enrichAccountsWithBot 附 bot 身份; base_url空用默认/receive_mode默认ws(只填token可跑)
agent-117 action/narration 剥出气泡(主人需求: 动作/旁白不进气泡,按位置上/下):
- parseContent.splitBubble(content)->{body,head,tail}: body=纯文本(去标签), head=开头/中间action/narr(气泡上), tail=结尾(气泡下)
- renderContent 只渲染text(去内联灰斜体); chat.tsx BubbleNotes 泡外 *动作/旁白* 灰斜体小字(气泡外依附), SplitChatBubble 先剥splitBubble再splitParagraphs(body), <p>/\n\n/毛玻璃/SourceLabel不破坏
- 注: 非chat mode renderContent 现在丢弃action/narr(可接受)
Channels 配置表单回填(修save后输入框空):
- openConfig 从 data.accounts[0].config 回填 token(掩码提示)/base_url/平台字段/黑白名单/限流, 不再硬清空
全仓 build/vet/test绿+前端build绿
This commit is contained in:
1 parent
bdd9d752e1
commit
65091f6497
15 files changed
+571
-83
No files matched your search
@@ -142,6 +142,7 @@ func (s *Server) adminListPlugins(w http.ResponseWriter, r *http.Request) {
|
||||
// accounts, masked so a raw token never reaches the client.
|
||||
func (s *Server) adminListChannels(w http.ResponseWriter, r *http.Request) {
|
||||
accountsByPlatform := s.maskedChannelAccounts()
|
||||
botByPlatform := s.liveBotIdentity()
|
||||
out := make([]ChannelSummary, 0)
|
||||
if s.caps != nil {
|
||||
for _, cap := range s.caps.List(capreg.KindChannel) {
|
||||
@@ -151,7 +152,7 @@ func (s *Server) adminListChannels(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
sum := ChannelSummary{Name: ch.Name(), Connected: ch.IsConnected()}
|
||||
if accts, ok := accountsByPlatform[channelBasePlatform(ch.Name())]; ok && len(accts) > 0 {
|
||||
sum.Accounts = accts
|
||||
sum.Accounts = enrichAccountsWithBot(accts, botByPlatform[channelBasePlatform(ch.Name())])
|
||||
}
|
||||
out = append(out, sum)
|
||||
}
|
||||
@@ -200,6 +201,88 @@ func (s *Server) hasChannelSummary(out []ChannelSummary, platform string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// botInfo is the live, connected bot's resolved identity (kook fetches it from
|
||||
// /api/v3/user/me on Connect). It is surfaced through the adapter's Config() so
|
||||
// the admin list can show who the bot is without persisting it.
|
||||
type botInfo struct {
|
||||
id string
|
||||
username string
|
||||
nickname string
|
||||
}
|
||||
|
||||
// empty reports whether no part of the bot identity resolved.
|
||||
func (b botInfo) empty() bool { return b.id == "" && b.username == "" && b.nickname == "" }
|
||||
|
||||
// liveBotIdentity collects the bot identity each registered channel adapter
|
||||
// reports, keyed by bare platform then account id (a bare adapter name yields an
|
||||
// empty account id). Adapters that do not expose bot identity (telegram,
|
||||
// webhook, ...) contribute nothing, so other platforms are unaffected.
|
||||
func (s *Server) liveBotIdentity() map[string]map[string]botInfo {
|
||||
out := make(map[string]map[string]botInfo)
|
||||
if s.caps == nil {
|
||||
return out
|
||||
}
|
||||
for _, cap := range s.caps.List(capreg.KindChannel) {
|
||||
ch, ok := cap.Value.(interface{ Config() map[string]any })
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
info := botIdentityOf(ch.Config())
|
||||
if info.empty() {
|
||||
continue
|
||||
}
|
||||
platform, acct := splitAdapterName(cap.Name)
|
||||
if out[platform] == nil {
|
||||
out[platform] = make(map[string]botInfo)
|
||||
}
|
||||
out[platform][acct] = info
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// botIdentityOf reads the optional bot-identity fields off an adapter config map.
|
||||
// The keys are non-sensitive (bot_id / bot_username / bot_nickname) so they are
|
||||
// never masked by runtimeconfig.Masked.
|
||||
func botIdentityOf(cfg map[string]any) botInfo {
|
||||
if cfg == nil {
|
||||
return botInfo{}
|
||||
}
|
||||
var b botInfo
|
||||
b.id, _ = cfg["bot_id"].(string)
|
||||
b.username, _ = cfg["bot_username"].(string)
|
||||
b.nickname, _ = cfg["bot_nickname"].(string)
|
||||
return b
|
||||
}
|
||||
|
||||
// splitAdapterName splits a (possibly multi-account) adapter name like
|
||||
// "kook:conf0" into ("kook", "conf0"); a bare name yields an empty account id.
|
||||
func splitAdapterName(name string) (platform, acct string) {
|
||||
if base, tail, ok := strings.Cut(name, ":"); ok {
|
||||
return base, tail
|
||||
}
|
||||
return name, ""
|
||||
}
|
||||
|
||||
// enrichAccountsWithBot merges the live bot identity into the matching account(s)
|
||||
// of a platform. An account is matched to its live adapter by id, so a
|
||||
// multi-account platform shows each bot's own identity. Accounts without a live
|
||||
// bot (not connected yet) keep their stored fields unchanged.
|
||||
func enrichAccountsWithBot(accts []AdminChannelAccount, by map[string]botInfo) []AdminChannelAccount {
|
||||
if len(by) == 0 {
|
||||
return accts
|
||||
}
|
||||
out := make([]AdminChannelAccount, 0, len(accts))
|
||||
for _, a := range accts {
|
||||
if info, ok := by[a.ID]; ok && !info.empty() {
|
||||
a.BotID = info.id
|
||||
a.BotUsername = info.username
|
||||
a.BotNickname = info.nickname
|
||||
}
|
||||
out = append(out, a)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// adminListIoT lists IoT devices (admin read).
|
||||
func (s *Server) adminListIoT(w http.ResponseWriter, r *http.Request) {
|
||||
out := make([]iotDeviceSummary, 0)
|
||||
|
||||
@@ -35,6 +35,13 @@ type AdminChannelAccount struct {
|
||||
GroupMsgPerMin int `json:"groupMsgPerMin,omitempty"`
|
||||
MsgPerMin int `json:"msgPerMin,omitempty"`
|
||||
MaxMessages int `json:"maxMessages,omitempty"`
|
||||
// BotID/BotUsername/BotNickname are the connected bot's resolved identity
|
||||
// (kook fetches it from /api/v3/user/me on connect). They come from the live
|
||||
// adapter, not the persisted channels.json config, and are non-sensitive (the
|
||||
// bot's public handle/display name), so the panel may show them.
|
||||
BotID string `json:"botId,omitempty"`
|
||||
BotUsername string `json:"botUsername,omitempty"`
|
||||
BotNickname string `json:"botNickname,omitempty"`
|
||||
}
|
||||
|
||||
// channelConfigRequest is the PUT /api/v1/admin/channels/{platform}/config body.
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
@@ -248,6 +249,21 @@ func (stubChannel) HealthCheck(ctx context.Context) error { return nil }
|
||||
|
||||
var _ sdk.Channel = stubChannel{}
|
||||
|
||||
// stubBotChannel mimics a live kook adapter that resolved its bot identity via
|
||||
// GET /api/v3/user/me on Connect (surfaced through Config()).
|
||||
type stubBotChannel struct{}
|
||||
|
||||
func (stubBotChannel) Name() string { return "kook:conf0" }
|
||||
func (stubBotChannel) Connect(ctx context.Context) error { return nil }
|
||||
func (stubBotChannel) Disconnect(ctx context.Context) error { return nil }
|
||||
func (stubBotChannel) IsConnected() bool { return true }
|
||||
func (stubBotChannel) HealthCheck(ctx context.Context) error { return nil }
|
||||
func (stubBotChannel) Config() map[string]any {
|
||||
return map[string]any{"bot_id": "bot-7", "bot_username": "bot7", "bot_nickname": "小K"}
|
||||
}
|
||||
|
||||
var _ sdk.Channel = stubBotChannel{}
|
||||
|
||||
// captureRunner records the sessionID each Run call sees.
|
||||
type captureRunner struct {
|
||||
got []string
|
||||
@@ -553,6 +569,46 @@ func TestAdminChannelsFromRegistry(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdminChannels_SurfacesBotIdentity verifies a connected kook adapter's
|
||||
// resolved bot identity (from user/me) reaches the accounts of GET /admin/channels
|
||||
// so the panel can show who the bot is.
|
||||
func TestAdminChannels_SurfacesBotIdentity(t *testing.T) {
|
||||
t.Parallel()
|
||||
caps := capreg.New()
|
||||
if err := caps.RegisterChannel(stubBotChannel{}); err != nil {
|
||||
t.Fatalf("register channel: %v", err)
|
||||
}
|
||||
store := &stubChannelConfigStore{
|
||||
path: filepath.Join(t.TempDir(), "channels.json"),
|
||||
accounts: map[string][]config.ChannelAccount{
|
||||
"kook": {{ID: "conf0", Config: map[string]any{"bot_token": "tk"}}},
|
||||
},
|
||||
}
|
||||
_, ts, jwt := buildEnv(t, cfgDefault(), Deps{Registry: caps, Channels: store})
|
||||
tok := signAny(t, jwt, "acct", auth.RoleOwner)
|
||||
resp, body := doReq(t, http.MethodGet, ts.URL+"/api/v1/admin/channels", nil, tok)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200 (body=%s)", resp.StatusCode, body)
|
||||
}
|
||||
var items []struct {
|
||||
Name string `json:"name"`
|
||||
Accounts []AdminChannelAccount `json:"accounts"`
|
||||
}
|
||||
if err := json.Unmarshal(decodeBody(t, body)["data"], &items); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if len(items) != 1 || items[0].Name != "kook:conf0" {
|
||||
t.Fatalf("items = %+v, want single kook:conf0", items)
|
||||
}
|
||||
if len(items[0].Accounts) != 1 {
|
||||
t.Fatalf("accounts = %+v, want one", items[0].Accounts)
|
||||
}
|
||||
acct := items[0].Accounts[0]
|
||||
if acct.BotNickname != "小K" || acct.BotUsername != "bot7" || acct.BotID != "bot-7" {
|
||||
t.Errorf("account bot identity = %+v, want 小K/bot7/bot-7", acct)
|
||||
}
|
||||
}
|
||||
|
||||
// --- Admin: policy errors --------------------------------------------------
|
||||
|
||||
func TestAdminSetPolicyUnavailable(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user