docs(comments): 注释中文化 · internal/services/channelbridge(纯替换)
- 只翻注释/文档字符串:剥掉注释后代码行逐字节不变(机械校验只剩允许的行尾注释形态) - 术语对齐既有译法(owner→主人、persona→角色、engage→参与度判定、quiet hours→安静时段、backoff→退避) - 复核:go build 绿 + channelbridge 包测试绿;gofmt 干净
This commit is contained in:
1 parent
61cf80d5ee
commit
093f38d986
28 files changed
+1089
-1089
No files matched your search
@@ -10,35 +10,35 @@ import (
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/user"
|
||||
)
|
||||
|
||||
// proactiveTarget is the owner's IM address (platform + account id) resolved for
|
||||
// a proactive push. The account id doubles as the DM peer on every adapter.
|
||||
// proactiveTarget 是为一次主动推送解析出的主人 IM 地址
|
||||
// (平台 + 账号 id)。账号 id 在每个适配器上都兼作私聊对端。
|
||||
type proactiveTarget struct {
|
||||
platform string
|
||||
accountID string
|
||||
}
|
||||
|
||||
// proactiveRecentCount is how many of the most recent main-session messages the
|
||||
// proactive target selection scans to find "where the owner last talked", so a
|
||||
// push lands on the channel she is actually using. Compile-time so no magic number
|
||||
// appears in the run path.
|
||||
// proactiveRecentCount 是主动推送目标选择扫描的最近主会话消息条数,
|
||||
// 用于找出「主人最近在哪里说话」,使推送落在她
|
||||
// 实际在用的渠道上。编译期常量,运行路径上不会出现
|
||||
// 魔法数字。
|
||||
const proactiveRecentCount = 20
|
||||
|
||||
// SendProactive pushes msg to the owner's IM channel via the registered adapter
|
||||
// for that platform. It is the backthink IMAdapter seam (design/12 §3 fallback) and
|
||||
// delivers to exactly ONE owner channel, chosen by the single-target selection in
|
||||
// proactiveTarget: her explicit [渠道:...] choice first, then the platform she last
|
||||
// used, then the whitelist default (rotating so it is not always the same channel).
|
||||
// WeChat is a first-class candidate (it is whitelisted and, when connected, the
|
||||
// recent-active/fallback selection may pick it).
|
||||
// SendProactive 通过该平台已注册的适配器把 msg 推送到主人的 IM 渠道。
|
||||
// 它是 backthink 的 IMAdapter 接缝(design/12 §3 回退),
|
||||
// 且只投递到一个主人渠道,由 proactiveTarget 中的单目标选择
|
||||
// 决定:先看她显式的 [渠道:...] 选择,其次是她最近
|
||||
// 使用的平台,最后是白名单默认(轮转,以免总是同一个渠道)。
|
||||
// WeChat 是一等候选(它在白名单里,且连接后
|
||||
// 最近活跃/回退选择可能选中它)。
|
||||
//
|
||||
// The push exits through the shared IM bubble dispatcher (sendIMBubbles), so a
|
||||
// multi-paragraph message is split into bubbles, role-play/narration is stripped, and
|
||||
// sends are spaced 0.5-2s apart — the same behaviour as a reply, and the reason
|
||||
// WeChat/other IM proactive pushes no longer arrive as one un-split <p> blob.
|
||||
// 推送经共享的 IM 气泡派发器(sendIMBubbles)出口,因此
|
||||
// 多段消息会被拆成气泡、角色扮演/旁白被剥离、
|
||||
// 相邻发送间隔 0.5-2s ——与回复行为一致,也是
|
||||
// WeChat/其他 IM 主动推送不再以一整块未拆分的 <p> 到达的原因。
|
||||
//
|
||||
// When the owner profile has no whitelisted account that is backed by a connected
|
||||
// adapter it returns a clear error so the caller records the reason rather than
|
||||
// silently dropping the push.
|
||||
// 当主人档案没有白名单账号、或该账号没有已连接的
|
||||
// 适配器支撑时,它返回明确错误,便于调用方记录原因,
|
||||
// 而不是静默丢弃推送。
|
||||
func (b *Bridge) SendProactive(ctx context.Context, msg adapter.UnifiedMessage) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
@@ -54,44 +54,44 @@ func (b *Bridge) SendProactive(ctx context.Context, msg adapter.UnifiedMessage)
|
||||
out := msg
|
||||
out.Platform = target.platform
|
||||
out.ChannelType = adapter.ChannelDirect
|
||||
// The owner's account id is the DM peer on every adapter: telegram/qq read
|
||||
// ChannelID, kook/weixin read SenderID (with ChannelID as the fallback). Setting
|
||||
// both keeps a direct message targetable regardless of which field the adapter
|
||||
// prefers, without duplicating the message.
|
||||
// 主人的账号 id 在每个适配器上都作私聊对端:telegram/qq 读
|
||||
// ChannelID,kook/weixin 读 SenderID(ChannelID 作回退)。两者
|
||||
// 都设置,无论适配器偏好哪个字段,私聊消息都可投达,
|
||||
// 且不会重复发送。
|
||||
out.ChannelID = target.accountID
|
||||
out.SenderID = target.accountID
|
||||
// The push exits through the shared IM bubble dispatcher, so a multi-paragraph
|
||||
// proactive message is split into bubbles and role-play/narration is stripped, with
|
||||
// the same 0.5-2s gap as a reply — WeChat proactive pushes are no longer sent as a
|
||||
// single un-split message.
|
||||
// 推送经共享的 IM 气泡派发器出口,因此多段主动消息
|
||||
// 会被拆成气泡、角色扮演/旁白被剥离,并采用
|
||||
// 与回复相同的 0.5-2s 间隔——WeChat 主动推送不再作为
|
||||
// 一整条未拆分消息发送。
|
||||
if err := b.sendIMBubbles(ctx, out, func(ctx context.Context, msg adapter.UnifiedMessage) error {
|
||||
return ad.SendProactive(ctx, msg)
|
||||
}, msg.Content); err != nil {
|
||||
return fmt.Errorf("channelbridge: send proactive via %q: %w", target.platform, err)
|
||||
}
|
||||
// Mirror the proactive message into the main conversation context (design/16
|
||||
// §4.4 + §4.6.1). Without this the model would not see what it proactively said, so
|
||||
// a follow-up owner reply would feel incoherent (the message "loses" its history).
|
||||
// It is the same mirror path as owner-private IM replies, scoped to MainSessionID.
|
||||
// 把主动消息镜像进主对话上下文(design/16
|
||||
// §4.4 + §4.6.1)。否则模型看不到自己主动说了什么,
|
||||
// 主人后续回复就会显得不连贯(该消息「丢失」了历史)。
|
||||
// 它与主人私聊 IM 回复走同一条镜像路径,作用域为 MainSessionID。
|
||||
b.persistToMain(ctx, history.RoleAssistant, msg.Content, "", target.platform,
|
||||
fmt.Sprintf("%s:%s:%s", target.platform, target.accountID, target.accountID),
|
||||
target.platform)
|
||||
return nil
|
||||
}
|
||||
|
||||
// proactiveTarget resolves the single owner account a proactive push should reach.
|
||||
// The range is every whitelisted owner channel that is backed by a connected
|
||||
// adapter (WeChat included), and the choice follows the backthink priority:
|
||||
// proactiveTarget 解析主动推送应当到达的唯一主人账号。
|
||||
// 候选范围是每个有已连接适配器支撑的白名单主人渠道
|
||||
// (含 WeChat),选择遵循 backthink 的优先级:
|
||||
//
|
||||
// 1. A concrete msg.Platform (her [渠道:...] choice) that is whitelisted AND
|
||||
// connected selects that channel.
|
||||
// 2. The channel she most recently used in the main session (an owner/dialog
|
||||
// message on a whitelisted, connected platform) is preferred.
|
||||
// 3. Otherwise the platform that the whitelist default selects, rotating from a
|
||||
// cursor so the fallback is not always the same channel (e.g. always kook).
|
||||
// 1. 具体 msg.Platform(她显式的 [渠道:...] 选择)既在白名单
|
||||
// 又已连接时,选中该渠道。
|
||||
// 2. 优先选她在主会话中最近使用的渠道(白名单、
|
||||
// 已连接平台上的主人/对话消息)。
|
||||
// 3. 否则取白名单默认选中的平台,并按游标轮转,
|
||||
// 使回退不会总是同一个渠道(例如总是 kook)。
|
||||
//
|
||||
// A nil owner or an empty whitelist yields !ok, as does a whitelist with no
|
||||
// connected adapter.
|
||||
// 主人为 nil、白名单为空,或白名单中没有任何
|
||||
// 已连接适配器时,返回 !ok。
|
||||
func (b *Bridge) proactiveTarget(ctx context.Context, msg adapter.UnifiedMessage) (proactiveTarget, bool) {
|
||||
accounts := b.ownerAccounts()
|
||||
if len(accounts) == 0 {
|
||||
@@ -116,8 +116,8 @@ func (b *Bridge) proactiveTarget(ctx context.Context, msg adapter.UnifiedMessage
|
||||
return proactiveTarget{}, false
|
||||
}
|
||||
|
||||
// ownerAccounts returns the owner's whitelisted accounts that carry an account id,
|
||||
// in whitelist order. A nil owner or an empty whitelist yields nil.
|
||||
// ownerAccounts 按白名单顺序返回主人带账号 id 的白名单账号。
|
||||
// 主人为 nil 或白名单为空时返回 nil。
|
||||
func (b *Bridge) ownerAccounts() []user.OwnerAccount {
|
||||
if b.owner == nil {
|
||||
return nil
|
||||
@@ -134,8 +134,8 @@ func (b *Bridge) ownerAccounts() []user.OwnerAccount {
|
||||
return out
|
||||
}
|
||||
|
||||
// matchOwnerAccount returns the owner account whose platform equals platform,
|
||||
// case-insensitively.
|
||||
// matchOwnerAccount 返回平台与 platform 相等(忽略大小写)的
|
||||
// 主人账号。
|
||||
func matchOwnerAccount(accounts []user.OwnerAccount, platform string) (user.OwnerAccount, bool) {
|
||||
for _, a := range accounts {
|
||||
if strings.EqualFold(a.Platform, platform) {
|
||||
@@ -145,10 +145,10 @@ func matchOwnerAccount(accounts []user.OwnerAccount, platform string) (user.Owne
|
||||
return user.OwnerAccount{}, false
|
||||
}
|
||||
|
||||
// recentActiveAccount returns the owner account for the most recent main-session
|
||||
// message whose platform is a whitelisted, connected owner channel — the "she is
|
||||
// active there" signal. A nil history store, a failed read or an empty tail yields
|
||||
// ok=false so selection falls through to the default rather than guessing.
|
||||
// recentActiveAccount 返回最近一条主会话消息所对应的主人账号,
|
||||
// 该消息的平台须是白名单且已连接的主人渠道——即「她在
|
||||
// 那里活跃」的信号。历史存储为 nil、读取失败或尾部为空时
|
||||
// 返回 ok=false,使选择退到默认值而不是瞎猜。
|
||||
func (b *Bridge) recentActiveAccount(ctx context.Context, accounts []user.OwnerAccount) (user.OwnerAccount, bool) {
|
||||
if b.history == nil {
|
||||
return user.OwnerAccount{}, false
|
||||
@@ -157,7 +157,7 @@ func (b *Bridge) recentActiveAccount(ctx context.Context, accounts []user.OwnerA
|
||||
if err != nil || len(recent) == 0 {
|
||||
return user.OwnerAccount{}, false
|
||||
}
|
||||
// Recent is oldest-first; walk newest-first so the most recent channel wins.
|
||||
// Recent 是最旧在前;从最新往回走,使最近的渠道胜出。
|
||||
for i := len(recent) - 1; i >= 0; i-- {
|
||||
p := strings.ToLower(strings.TrimSpace(recent[i].Platform))
|
||||
if p == "" || isGenericProactivePlatform(p) {
|
||||
@@ -170,10 +170,10 @@ func (b *Bridge) recentActiveAccount(ctx context.Context, accounts []user.OwnerA
|
||||
return user.OwnerAccount{}, false
|
||||
}
|
||||
|
||||
// nextConnectedAccount returns the next whitelisted owner account with a connected
|
||||
// adapter, advancing a cursor so the no-signal fallback rotates instead of always
|
||||
// landing on the first whitelisted channel (e.g. always kook). It holds the bridge
|
||||
// lock to read the adapter map and update the cursor atomically.
|
||||
// nextConnectedAccount 返回下一个有已连接适配器的白名单主人账号,
|
||||
// 并推进游标,使无信号回退会轮转,而不是总落在
|
||||
// 第一个白名单渠道(例如总是 kook)。它持有桥接器
|
||||
// 锁,以原子地读取适配器 map 并更新游标。
|
||||
func (b *Bridge) nextConnectedAccount(accounts []user.OwnerAccount) (user.OwnerAccount, bool) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
@@ -189,17 +189,17 @@ func (b *Bridge) nextConnectedAccount(accounts []user.OwnerAccount) (user.OwnerA
|
||||
return user.OwnerAccount{}, false
|
||||
}
|
||||
|
||||
// hasConnectedAdapter reports whether a registered adapter with this base platform
|
||||
// is currently connected, so a channel is only ever selected when it can actually
|
||||
// deliver a message.
|
||||
// hasConnectedAdapter 报告该基础平台是否有已注册且当前已连接的
|
||||
// 适配器,因此只有真能投递消息的渠道才会
|
||||
// 被选中。
|
||||
func (b *Bridge) hasConnectedAdapter(platform string) bool {
|
||||
b.mu.RLock()
|
||||
defer b.mu.RUnlock()
|
||||
return b.hasConnectedAdapterLocked(platform)
|
||||
}
|
||||
|
||||
// hasConnectedAdapterLocked is hasConnectedAdapter without taking the lock; the
|
||||
// caller must hold b.mu (read or write).
|
||||
// hasConnectedAdapterLocked 是不加锁版本的 hasConnectedAdapter;
|
||||
// 调用方须持有 b.mu(读锁或写锁)。
|
||||
func (b *Bridge) hasConnectedAdapterLocked(platform string) bool {
|
||||
for name, a := range b.adapters {
|
||||
if strings.EqualFold(basePlatform(name), platform) && a.IsConnected() {
|
||||
@@ -209,11 +209,11 @@ func (b *Bridge) hasConnectedAdapterLocked(platform string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// isGenericProactivePlatform reports whether platform is a routing keyword rather
|
||||
// than a concrete channel id. Only these fall through to the recent-active / default
|
||||
// selection; a concrete id must resolve to a whitelisted, connected channel before
|
||||
// it is honored, so an explicit channel choice is never silently replaced by a
|
||||
// different channel.
|
||||
// isGenericProactivePlatform 报告 platform 是路由关键词还是
|
||||
// 具体渠道 id。只有关键词才会退到最近活跃 / 默认
|
||||
// 选择;具体 id 必须先解析为白名单且已连接的渠道
|
||||
// 才被采纳,因此显式的渠道选择绝不会被静默替换成
|
||||
// 另一个渠道。
|
||||
func isGenericProactivePlatform(platform string) bool {
|
||||
switch platform {
|
||||
case "", "cyrene-backthink":
|
||||
@@ -223,10 +223,10 @@ func isGenericProactivePlatform(platform string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// adapterForPlatform returns a registered adapter whose base platform matches
|
||||
// platform (multi-account names like "kook:3be..." resolve to their bare base),
|
||||
// preferring a connected one so a dead account is only tried when it is the only
|
||||
// option. It returns nil when none is registered.
|
||||
// adapterForPlatform 返回基础平台与 platform 匹配的已注册适配器
|
||||
// (多账号名如 "kook:3be..." 会归为其裸基础名),
|
||||
// 优先取已连接的,使失效账号只在别无选择时才被尝试。
|
||||
// 没有已注册的匹配项时返回 nil。
|
||||
func (b *Bridge) adapterForPlatform(platform string) adapter.PlatformAdapter {
|
||||
b.mu.RLock()
|
||||
defer b.mu.RUnlock()
|
||||
|
||||
Reference in new issue
Block a user