主动推送目标不再取第一个白名单渠道(kook),也不再双目标 web+kook;改为每次只发一个平台,范围=全部已连接白名单渠道(含微信 weixin-ilink)。
backthink/push.go: pusher 单目标路由——web hint 一次 pushActive;im/auto/实体平台 hint 一次性 pushIM (msgs 置哨兵/实体平台),无 IM sink 时诚实降级 pushActive;保留 PushGate 降频与成功 RecordPush。
channelbridge/proactive.go: proactiveTarget 三级选择——①决策 [渠道:xxx] 实体平台且白名单+已连接优先;②最近活跃(主会话最近20条 newest-first 找白名单+已连接平台,web 会话自动跳过);③回退 NextConnectedAccount 用旋转游标避免永远 kook。微信不再排除、可被选中;决策 hint 指定非白名单渠道回退(不报错)。Bridge 新增 proactiveCursor 字段(mu 保护)。
context.go: guidancePrompt 描述改为每次挑一个合适渠道(含微信)。
测试: 决策优先/最近活跃/微信可选中/无信号回退旋转/单目标 等,go build/vet/test 全绿 + windows/darwin 交叉编译。stash@{0} 保留被取代的'web+kook双目标'WIP以备回滚。
143 lines
3.3 KiB
Go
143 lines
3.3 KiB
Go
package backthink
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"io"
|
|
"log/slog"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/db"
|
|
)
|
|
|
|
// mockThinker records every Think invocation and returns a fixed conclusion. It
|
|
// respects ctx cancellation so Stop can drain a hung pass.
|
|
type mockThinker struct {
|
|
mu sync.Mutex
|
|
text string
|
|
err error
|
|
sleep time.Duration
|
|
calls int
|
|
last string
|
|
}
|
|
|
|
func (m *mockThinker) Think(ctx context.Context, sessionID, trigger string) (string, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return "", err
|
|
}
|
|
if m.sleep > 0 {
|
|
select {
|
|
case <-ctx.Done():
|
|
return "", ctx.Err()
|
|
case <-time.After(m.sleep):
|
|
}
|
|
}
|
|
m.mu.Lock()
|
|
m.calls++
|
|
m.last = trigger
|
|
m.mu.Unlock()
|
|
return m.text, m.err
|
|
}
|
|
|
|
func (m *mockThinker) count() int { m.mu.Lock(); defer m.mu.Unlock(); return m.calls }
|
|
func (m *mockThinker) lastTrigger() string { m.mu.Lock(); defer m.mu.Unlock(); return m.last }
|
|
|
|
// mockState returns a fixed UserState.
|
|
type mockState struct{ state UserState }
|
|
|
|
func (m *mockState) Current(context.Context) (UserState, error) { return m.state, nil }
|
|
|
|
// mockPusher records Push calls and returns a fixed result.
|
|
type mockPusher struct {
|
|
mu sync.Mutex
|
|
result PushResult
|
|
calls int
|
|
lastMsg adapter.UnifiedMessage
|
|
lastHint string
|
|
}
|
|
|
|
func (p *mockPusher) Push(ctx context.Context, req PushRequest) PushResult {
|
|
p.mu.Lock()
|
|
p.calls++
|
|
p.lastMsg = req.Message
|
|
p.lastHint = req.ChannelHint
|
|
p.mu.Unlock()
|
|
return p.result
|
|
}
|
|
|
|
func (p *mockPusher) count() int { p.mu.Lock(); defer p.mu.Unlock(); return p.calls }
|
|
|
|
// mockActive implements ActiveClient.
|
|
type mockActive struct {
|
|
mu sync.Mutex
|
|
n int
|
|
err error
|
|
calls int
|
|
last adapter.UnifiedMessage
|
|
}
|
|
|
|
func (m *mockActive) SendProactive(ctx context.Context, msg adapter.UnifiedMessage) (int, error) {
|
|
m.mu.Lock()
|
|
m.calls++
|
|
m.last = msg
|
|
m.mu.Unlock()
|
|
return m.n, m.err
|
|
}
|
|
|
|
// mockIMAdapter implements IMAdapter.
|
|
type mockIMAdapter struct {
|
|
mu sync.Mutex
|
|
err error
|
|
calls int
|
|
last adapter.UnifiedMessage
|
|
}
|
|
|
|
func (m *mockIMAdapter) SendProactive(ctx context.Context, msg adapter.UnifiedMessage) error {
|
|
m.mu.Lock()
|
|
m.calls++
|
|
m.last = msg
|
|
m.mu.Unlock()
|
|
return m.err
|
|
}
|
|
|
|
func (m *mockIMAdapter) count() int { m.mu.Lock(); defer m.mu.Unlock(); return m.calls }
|
|
|
|
// mockGate implements PushGate.
|
|
type mockGate struct{ allow bool }
|
|
|
|
func (g *mockGate) Allow(context.Context, PushKind) bool { return g.allow }
|
|
func (g *mockGate) RecordPush(PushKind) {}
|
|
|
|
// newTestDB opens an isolated in-memory SQLite handle for one test and closes it
|
|
// on cleanup.
|
|
func newTestDB(t *testing.T) *sql.DB {
|
|
t.Helper()
|
|
h, err := db.Open(context.Background(), ":memory:", nil)
|
|
if err != nil {
|
|
t.Fatalf("open test db: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = h.Close() })
|
|
return h
|
|
}
|
|
|
|
// discardLog returns a logger that writes nowhere.
|
|
func discardLog() *slog.Logger {
|
|
return slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
}
|
|
|
|
// eventually polls cond until it is true or the deadline passes.
|
|
func eventually(t *testing.T, timeout time.Duration, cond func() bool) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(timeout)
|
|
for time.Now().Before(deadline) {
|
|
if cond() {
|
|
return
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
t.Fatalf("condition not met within %v", timeout)
|
|
}
|