with open('d:/Project/Code/Uni/Cyrene/backend/platform-bridge/internal/handler/bridge_handler.go', 'r', encoding='utf-8') as f: lines = f.readlines() # 1. Add sync and time to imports for i, line in enumerate(lines): if line.strip() == '"strconv"': lines.insert(i+1, '\t"sync"\n') lines.insert(i+2, '\t"time"\n') print('1. Imports added') break # 2. Add fields to struct for i, line in enumerate(lines): if 'internalToken string' in line: lines.insert(i+1, '\tchannelsMu sync.RWMutex\n') lines.insert(i+2, '\tcachedChannels []bridge.ChannelInfo\n') print('2. Struct fields added') break # 3. Add route for i, line in enumerate(lines): if 'send-proactive' in line: lines.insert(i+1, '\tmux.HandleFunc("/api/v1/channels", h.listChannels)\n') print('3. Route added') break # 4. Add listChannels handler (before func health) for i, line in enumerate(lines): if line.strip().startswith('func (h *BridgeHandler) health('): handler = '''// listChannels returns cached channels from all adapters (groups + friends). // GET /api/v1/channels func (h *BridgeHandler) listChannels(w http.ResponseWriter, r *http.Request) { \th.channelsMu.RLock() \tchannels := h.cachedChannels \th.channelsMu.RUnlock() \tif channels == nil { \t\tchannels = []bridge.ChannelInfo{} \t} \twriteJSON(w, http.StatusOK, map[string]interface{}{ \t\t"channels": channels, \t\t"total": len(channels), \t}) } ''' lines[i:i] = [handler] print('4. listChannels added') break # 5. Add refresh methods at end for i in range(len(lines)-1, 0, -1): if lines[i].rstrip() == '}': last = i break refresh = ''' // StartChannelRefresh periodically queries all adapters for their channel lists. func (h *BridgeHandler) StartChannelRefresh(interval time.Duration) { \tif interval <= 0 { \t\tinterval = 10 * time.Minute \t} \tgo func() { \t\tticker := time.NewTicker(interval) \t\tdefer ticker.Stop() \t\tlog.Printf("[channel-refresh] 频道缓存刷新已启动 (间隔=%v)", interval) \t\th.refreshChannels() \t\tfor range ticker.C { \t\t\th.refreshChannels() \t\t} \t}() } func (h *BridgeHandler) refreshChannels() { \tvar all []bridge.ChannelInfo \tfor _, name := range h.router.ListAdapters() { \t\ta, err := h.router.GetAdapter(name) \t\tif err != nil { \t\t\tcontinue \t\t} \t\tif lister, ok := a.(bridge.ChannelLister); ok { \t\t\tchannels := lister.ListChannels() \t\t\tall = append(all, channels...) \t\t} \t} \th.channelsMu.Lock() \th.cachedChannels = all \th.channelsMu.Unlock() \tlog.Printf("[channel-refresh] 频道缓存已更新: %d 个频道", len(all)) } ''' lines.insert(last, refresh) print('5. Refresh methods added') with open('d:/Project/Code/Uni/Cyrene/backend/platform-bridge/internal/handler/bridge_handler.go', 'w', encoding='utf-8') as f: f.write(''.join(lines)) print('DONE')