diff --git a/CONTRIBUTING.en.md b/CONTRIBUTING.en.md index 204143f3f..b1dfe27f9 100644 --- a/CONTRIBUTING.en.md +++ b/CONTRIBUTING.en.md @@ -108,7 +108,7 @@ Before the first release candidate (RC), all six format and interface numbers we | Format or interface | Current value | Location | |---|---|---| | Plugin interface | `1` | `INTERFACE` in `src/len_bot/plugin.py` | -| Root configuration | `5` | `CONFIG_VERSION` in `src/len_bot/next/config.py` | +| Root configuration | `6` | `CONFIG_VERSION` in `src/len_bot/next/config.py` | | Business database | `2` | `FORMAT_VERSION` in `src/len_bot/next/storage/store.py` | | Memory job database | `1` | `FORMAT_VERSION` in `src/len_bot/next/memory/jobs.py` | | Local memory index | `1` | `FORMAT_VERSION` in `src/len_bot/next/memory/local.py` | diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index df9df49e4..0633371ed 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -108,7 +108,7 @@ uv run --no-sync ruff check src/len_bot scripts deploy/package tests | 编号项 | 当前值 | 定义位置 | |---|---|---| | 插件接口版本 | `1` | `src/len_bot/plugin.py` 的 `INTERFACE` | -| 根配置格式 | `5` | `src/len_bot/next/config.py` 的 `CONFIG_VERSION` | +| 根配置格式 | `6` | `src/len_bot/next/config.py` 的 `CONFIG_VERSION` | | 业务数据库 | `2` | `src/len_bot/next/storage/store.py` 的 `FORMAT_VERSION` | | 记忆处理库 | `1` | `src/len_bot/next/memory/jobs.py` 的 `FORMAT_VERSION` | | 本地记忆索引 | `1` | `src/len_bot/next/memory/local.py` 的 `FORMAT_VERSION` | diff --git a/developer/architecture.md b/developer/architecture.md index 41b92e169..1d1b446a9 100644 --- a/developer/architecture.md +++ b/developer/architecture.md @@ -62,7 +62,7 @@ OneBot ────────────> 平台适配器 ──> 消息入 ### 持续关注与局部收尾 -`attention.declarations` 开启后,允许 `attention` 工具的角色可以创建跨回合意图;`attention.disposition` 独立控制本轮收尾的 `open/closed`。两项均默认关闭。意图跨回合保留,条件命中后由模型判断是否完成。配置、工具和调度语义见[持续关注](attention.md)。 +`attention.declarations` 开启后,允许 `attention` 工具的角色可以创建跨回合意图;`attention.disposition` 独立控制本轮收尾的 `open/closed`。前者默认开启(写 `null` 关闭),后者默认关闭。意图跨回合保留,条件命中后由模型判断是否完成。配置、工具和调度语义见[持续关注](attention.md)。 ### 请求顺序 @@ -173,7 +173,7 @@ OneBot ────────────> 平台适配器 ──> 消息入 - 只执行 `memory` 工具,其他工具返回未执行。角色没有开放 `memory` 时不能整理记忆。 - 已在会话历史里的消息按消息 id 指明范围,不在历史里的(已压缩进回想或还没进会话)附原文。 - 最后一次回复正文是 JSON,含 `expressions`、`terms`、`effects`;说法来源为本批群友消息,黑话还可引用本次提供的既有用例及相邻人类消息;不合格的条目丢掉,其余照常落库,丢掉的原因记在该批的回复记录里。读不出 JSON 时请模型重答一次,工具轮数用完时请模型直接给结果。结果写进原有的 `memory_jobs`、`learning_batches`、`jargon_calls` 和 `reply_effect_calls`;请求以 `upkeep` 用途记在 `model_calls`,用量只计一次。 -- 触发沿用 `memory.ingest` 和 `learning` 的阈值,聊天模型开轮时不取快照,轮次结束后再唤醒;会话压缩前强制整理一次。一批失败时四类结果都不落库,进度不前进;60 秒后自动重跑同一范围,连续失败 3 次后等面板重试。memory 工具调用出错时把错误交给模型,不让整批失败。工作者遇到意外错误时在面板显示,暂停 60 秒后继续。 +- 触发沿用 `memory.ingest` 和 `learning` 的阈值,聊天模型开轮时不取快照,轮次结束后再唤醒;会话压缩前强制整理一次。一批失败时四类结果都不落库,进度不前进;60 秒后自动重跑同一范围,连续失败 3 次后等面板重试;若这 3 次都是服务商 HTTP 错误、连接失败或请求超时,聊天模型在一段失败后重新答复时(`ModelAlerts` 判定恢复)把失败次数清零一次,同一范围按原规则重新开始,不另加重试次数。memory 工具调用出错时把错误交给模型,不让整批失败。工作者遇到意外错误时在面板显示,暂停 60 秒后继续。 - 手动整理与会话压缩请求按本次请求标记消费;规划读库或准备会话历史期间收到的新请求保留给下一轮整理。 说法去重:没有向量时,同一说法且情境字词重合度不低于 0.6 的新候选并入已有说法;有向量时按余弦相似度 0.9 合并。 diff --git a/developer/attention.md b/developer/attention.md index 5f0b83f27..53ef6c6ff 100644 --- a/developer/attention.md +++ b/developer/attention.md @@ -55,10 +55,24 @@ min(ambient_max_interval_seconds, ambient_min_interval_seconds × 2^silence_leve ## 回复效果系数 -插话间隔除以一个系数。系数取本群最近 3 天内、由环境插话或随机探索唤醒的回合里最多 10 条已判定的回复效果:`agree` 和 `continue` 记 1,`unrelated` 和 `negative` 记 -1,其余记 0,每条按 24 小时半衰加权。加权平均(分母另加 2,避免一两条判定就大幅变化)小于 0 时,系数为 1 加上它的一半,最低 0.5;不小于 0 时为 1。直接联系、点名、对话继续和持续关注不受系数影响。系数随唤醒判定保存为 `effect_factor`,写进 `attention_ambient` 日志,现场的唤醒详情在系数小于 1 时显示。 +插话间隔除以一个系数。系数取本群最近 3 天内、由环境插话或随机探索唤醒的回合里最多 10 条已判定的回复效果:`agree` 和 `continue` 记 1,`unrelated` 和 `negative` 记 -1,其余记 0,每条按 24 小时半衰加权。加权平均(分母另加 2,避免一两条判定就大幅变化)小于 0 时,系数为 1 加上它的一半,最低 0.5,插话间隔最多延长到 2 倍;大于 0 时,系数为 1 加上它的四分之一,最高 1.25,插话间隔最多缩短到 0.8 倍。同样多的判定,收紧比放宽快。直接联系、点名、对话继续和持续关注不受系数影响。系数随唤醒判定保存为 `effect_factor`,写进 `attention_ambient` 日志,现场的唤醒详情在系数不为 1 时显示。 `uv run --no-sync python scripts/first_reply_latency.py batches` 用固定 OneBot 到达序列核对图片后补文字、同一人续发、换人、隔着别人续发、旁人发图和上限;`report <数据库>` 读取实际回合的首句等待与合批、模型、工具、发送耗时。 +## 恢复后的追赶 + +宿主启动时还有上一个进程留下的未读消息,或一个回合因聊天模型的服务商错误失败后,场景进入追赶状态,直到某个回合拿到模型回复。追赶期间: + +- 环境插话和随机探索只在最近 5 分钟(`CATCH_UP_SECONDS`)内有可以接的话时开轮;只有旧消息时撤下这次唤醒,消息留给下一轮,日志 `attention_catch_up`。 +- 开轮时 5 分钟以前的消息单独成段,标题写明追赶开始时间、这些消息当时没有回应、按原时间理解且不逐条补回;@ Bot 的旧消息照常唤醒,按原时间交给模型。失败回合已经读入会话的批次不再重发,恢复回合的批次标题同样带这段说明。 +- 恢复未结束对话(`resume`)的回合不受影响。 + +## 模型故障提醒 + +`runtime/model_alerts.py` 按聊天绑定(服务商和模型)统计所有场景连续以服务商错误结束的回合(`ModelHTTPError`、`ModelConnectionError` 或单次请求自身的 `TimeoutError`,回合结果的 `model_failure`;以 `error` 或 `timeout` 结束的回合都会带上)。整轮超时由外层取消,不算服务商错误。达到根配置 `model_alert_failed_turns`(默认 3)个时私聊每位主人一次:错误类别、最早失败时间、涉及场景数和最近一次错误;之后第一个拿到模型回复的回合(`model_replies` 大于 0)再发一次恢复。主人的私聊场景已接入时经该场景的发送出口并入库,否则直接经平台发送;非真实发送时只写 `model_alert` 日志。失败计数只在内存中,重启后清零。宿主不切换模型,不重试。 + +一段失败结束时(不论是否已提醒),`SessionUpkeep.model_recovered()` 让因服务商错误(含请求超时)用完 3 次自动重试的后台整理清零一次失败次数,积压的整理不必等面板重试;它不增加请求,也不对其他原因的失败生效。 + ## 唤醒判定记录 每条收到的消息在 `messages.wake_verdict` 里保存一次判定:`woke`、`channel`、`reason` 和 `effect_factor`。`effect_factor` 只在消息可以接话时计算,其余为 `null`。 @@ -74,7 +88,7 @@ min(ambient_max_interval_seconds, ambient_min_interval_seconds × 2^silence_leve } ``` -不配置即关闭。配置在场景重新装配时生效。面板群设置的持续关注与对话收尾一节提供两个开关,运行模式和上限在高级设置中。 +`declarations` 默认开启(`enforce` 与下文的默认限制),写 `null` 关闭;`disposition` 不配置即关闭。根配置格式 5 及更早的文件里 `declarations: null` 是面板保存的旧默认值,升级到格式 6 时去掉,按新默认开启;格式 6 起 `null` 表示主动关闭。配置在场景重新装配时生效。面板群设置的持续关注与对话收尾一节提供两个开关,运行模式和上限在高级设置中。 面板读取与取消: @@ -124,6 +138,18 @@ min(ambient_max_interval_seconds, ambient_min_interval_seconds × 2^silence_leve 候选由聊天模型判断后执行 `keep` 或 `resolve`,例如指定账号的一条闲聊也会匹配 `person`。定时条件触发一次后不再自动重复,意图仍保留;再次定时需要修改意图。 +### 由模型决定关注什么 + +持续意图是插话的主要来源:模型在回合里声明接下来要跟的人、话题和时长,命中时宿主按声明唤醒。插话间隔退为兜底,没有声明或声明没命中时才按它偶尔看一眼群: + +- 意图唤醒的回合开始时和环境插话一样记一次 `ambient_last_at`,插话间隔从这时重新起算,意图唤醒替代而不是叠加在插话之上。 +- 同一个意图由消息命中开轮后,`REWAKE_SECONDS`(120 秒)内再命中的消息先只记为候选,日志 `attention_candidate_held`;窗口结束时这些候选按计时条件的方式一起开一次意图唤醒(`next_due` 包含这个时间,状态保存后重启仍有效,遵守安静时段和只回应直接联系)。窗口内已有别的回合开轮并带走了候选时,不再补开。计时条件不受限制。 +- 不增加模型调用:意图命中直接开一个普通回合,没有额外的判断请求。 + +### 话题新鲜度 + +环境插话、随机探索和意图唤醒的批次末尾附 `<话题新鲜度>`(`chat/freshness.py`、`next_topic_freshness.md`),只写事实:叫醒这次的那条消息的时间和距今多久,之后又有几条、几个人在说,其中几条和它有相同的字词(汉字两字组或两个以上字母数字),最后一条距今多久,中间停了 5 分钟以上的次数和最长一次。提示词说明隔得久、后面在聊别的就不回头接,模型判断值得回头时再接。直接联系、点名和对话继续不附这一段。叫醒这次的消息取唤醒时间当时或之后收到的第一条成员消息;窗口结束后补开的意图唤醒取被攒下的候选消息;只由计时条件开轮、批次里没有这样的消息时不附这一段。追赶期间仍按上文只看最近 5 分钟开轮,新鲜度按实际开轮的批次计算。 + ## 局部收尾 可在最后一次 say、react、message_reaction 或 attention 调用上附加: diff --git a/developer/context.md b/developer/context.md index 645142b8b..41e8db555 100644 --- a/developer/context.md +++ b/developer/context.md @@ -14,7 +14,7 @@ 一轮回复的开始时间是固定的。之后到达的消息批次和 wait 的结果,会提供新的真实时间依据。任务快照只列出活动身份、状态和待回答的问题,较长的结果通过实际的结束事件或 task 工具读取。 -参考材料末尾可能附一段最近的说法,列出最近几次发言里反复出现的词和表情图,提醒下一条换个说法。这段内容每次请求从原生历史重新统计,不写入历史,规则见[群聊表达](expression-materials.md#不照抄自己)。 +参考材料末尾依次可能附上:最近一次整理回合统计的本群发言形态;最近的说法,列出最近几次发言里反复出现的词、句式和表情图,提醒下一条换个说法;最后是角色材料里带否定的原句。后两段每次请求重新计算,都不写入历史,规则见[群聊表达](expression-materials.md#参考材料的来源)和[不照抄自己](expression-materials.md#不照抄自己)。 临时召回的内容不写入原生历史,也不作为旧段落摘要的来源。执行记忆工具前,先撤下旧的召回内容。群概览在下一次请求时重新读取,工具只完成一部分就报错时也是如此。配置或能力发生变化时,按最新能力重建请求前缀。 @@ -88,7 +88,7 @@ send_file 返回可以操作的回执和原事件 ID,完整的平台原始帧 原生摘要接着聊天请求续写:沿用同一份 system、tools、回想和历史,去掉本轮参考,末尾追加一条整理指令(`next_recap_continue.md`),用切分点两侧消息的片段标出移出范围,并沿用聊天的缓存亲和标识。服务商的前缀缓存因此覆盖整段历史,新输入只有这条指令。这一轮请求带着 tools,回复里出现工具调用或空正文时按摘要失败处理。续写请求超出上下文窗口时,改用只含旧回想和即将移出旧段落的独立请求。角色交接和插件压缩使用独立请求的资料。手动压缩也计入当前参考的预算。 -摘要成功、内容非空并且满足空间要求后,在原事务中移动起点,并重置已发现的工具集合。摘要失败时,旧的回想和原始记录保持原样。最新的不可拆分单位或摘要输入过大时,错误信息会给出估算值、预留量和窗口大小。 +摘要成功、内容非空并且满足空间要求后,在原事务中移动起点,并重置已发现的工具集合。摘要失败时,旧的回想和原始记录保持原样。回合内的自动压缩因回想输出额度用完(`ModelOutputLimitError`)或预算错误失败、并且未压缩的请求加聊天输出额度仍放得进窗口时,本轮用未压缩的请求继续,日志记 `compaction_held`;此后输入估算比失败时多出 `keep_recent_tokens` 之前不再自动压缩,放不进窗口时照常压缩,再失败则本轮失败。回想插件的失败不走这条路径。最新的不可拆分单位或摘要输入过大时,错误信息会给出估算值、预留量和窗口大小。 ## 临时请求 @@ -98,7 +98,7 @@ send_file 返回可以操作的回执和原事件 ID,完整的平台原始帧 - 插件的 generate 只使用插件明确提供的 system 和 prompt。 - 独立抽取请求使用各自原有的游标、记录表和真实消息样本,不复制聊天会话历史。 -这些请求使用各自所属绑定的输出额度。recap 单独使用 `compaction.max_output_tokens`。 +这些请求使用各自所属绑定的输出额度。recap 单独使用 `compaction.max_output_tokens`,思考强度用 `compaction.reasoning_effort`,未设置时沿用聊天绑定的 `quick_reasoning_effort`;思考预留按这个强度计算,`none`、`off` 不预留。 `say.mention` 使用平台账号,例如 `onebot:70001`,格式与消息的发送者身份和平台发送端一致。只写账号本身、不带平台前缀时,工具参数解析阶段会直接报错。 diff --git a/developer/expression-materials.md b/developer/expression-materials.md index 351922620..03c600326 100644 --- a/developer/expression-materials.md +++ b/developer/expression-materials.md @@ -22,7 +22,10 @@ | `next_direct.md` | `say` 正文参数的含义 | | `next_react.md` | 表情工具的用法,只在开放 `react` 时加入 | | `next_chat_examples.md` | 几个群聊情境里的判断和工具选择 | -| `next_recent_habits.md` | 最近几次发言里反复出现的词和表情图,附在本次参考材料末尾 | +| `next_group_voice.md` | 本群群友最近的发言形态,整理回合统计,作为参考材料 | +| `next_recent_habits.md` | 最近几次发言里反复出现的词、句式和表情图,附在本次参考材料末尾 | +| `next_reply_feedback.md` | 群友对 Bot 最近几句话的实际反应(回复效果判定),放在角色材料里带否定的原句之前 | +| `next_persona_avoid.md` | 角色材料里带否定的原句,原文复述在本次参考材料最后 | 提示词统一用中文,以第二人称称呼角色,不写死角色的性别。工具参数的含义写在工具定义里,提示词不再重复。 @@ -68,7 +71,9 @@ ### 不照抄自己 -模型会把历史里自己说过的话当成最直接的示范,几句口头禅一旦出现,就会越用越多,各群还会各自固定成不同的说法。每次请求时,宿主从原生历史里取最近 8 次 `say` 和 `react`。同一个词出现在其中六成以上的文字发言里并且不少于 4 条,或者同一张图用了 3 次以上,就在参考材料末尾写明次数,提醒下一条换个说法或换张图。群友最近 50 条消息里出现过的词算作话题,不计入。单个字只统计草、哼、喵这类感叹词,吧、呢、啊这类句尾语气词是普通语法,不单独统计。标点和文字配图的比例属于角色的口吻,不统计。这段提醒每次请求重新计算,不写入历史。 +模型会把历史里自己说过的话当成最直接的示范,几句口头禅一旦出现,就会越用越多,各群还会各自固定成不同的说法。每次请求时,宿主从原生历史里取最近 8 次 `say` 和 `react`,只看模型自己的发言,插件卡片和宿主通知不在其中。同一个词出现在其中六成以上的文字发言里并且不少于 4 条,或者同一张图用了 3 次以上,就在参考材料末尾写明次数,提醒下一条换个说法或换张图。群友最近 50 条消息里出现过的词算作话题,不计入。单个字只统计草、哼、喵这类感叹词,吧、呢、啊这类句尾语气词是普通语法,不单独统计。 + +句式也会这样固定下来,所以宿主同样统计四种句子形状,一半以上的文字发言(不少于 4 条)相同时写明次数:开头相同的几个字;最后一个汉字之后的结尾(例如同一个颜文字或同一串标点);最后一个分句开头相同的字(例如每条都以一句叮嘱收尾);同一句里两段固定的字中间夹着每次不同的内容(例如「一边……一边」这类模板,中间每次都一样的算固定说法,按词统计)。这些形状都从文字本身统计,不预设具体说法。偶尔用到的标点和文字配图的比例仍属于角色的口吻,不统计。这段提醒每次请求重新计算,不写入历史。 ### 身份问题由角色决定 @@ -80,6 +85,25 @@ 角色的身份、关系、边界和人工样例来自角色包。开启学习时,每轮开始会按近期的对话,选出最多五条已经采用的群聊说法,和风格一起作为宿主参考放进上下文。 +### 本群发言形态 + +群里怎样说话(多长、几句、多常只发图、会不会一个人连发几条)每个群都不一样,框架不规定,也不替角色规定。整理回合开始时,宿主读取本群最近 300 条消息,统计其中群友的发言(不含本 Bot 和 `other_bot_ids`):文字消息长度的中位数和九成分位、只有一句的消息占比、只发图片的比例、一个人 60 秒内连发的比例和条数。群友消息少于 30 条时不统计。结果只在内存里保留到下一次整理,之后每轮放进参考材料,写明这是群友平常的样子,角色材料对说话方式另有写法时以角色材料为准。没有启用记忆整理或学习的群没有整理回合,也就没有这段参考。 + +### 群友对最近几句话的反应 + +开启回复效果(`learning.reply_effects`)的群,每轮参考材料列出最近 3 天里最新 6 条已判定的 Bot 发言:被附和或接着聊的一组,被纠正、被嫌弃或没人接的一组,每条带发言原文(超过 40 字截断)、判定和整理回合给的一句依据。判定为 `uncertain` 的不列,没有已判定发言时不出现这一段。这里只写群友的实际反应,不总结规律,也不要求下一句怎么改,说法仍由当下话题和角色材料决定。 + +### 角色材料里带否定的原句 + +角色材料在 system 开头,离模型这一轮要写的话最远,其中写明不做什么的句子最容易被忽略。宿主从角色的 `behavior`、`voice.md` 和 `boundaries.md` 里按句取出带否定的原句,按原文、原顺序列在每轮参考材料的最后。一句话满足下面任一条件即取出,判断时先去掉引号(「」『』“”‘’和英文双引号)里的内容,引号里是角色的示例台词: + +- 含「不要」「不准」「不许」「禁止」「避免」「切勿」。 +- 某个分句在主语(她、他、你、我、它、角色名、自称和别名)和「也」「都」「从来」「绝对」等副词之后,以「不」或「别」开头。 + +第二条有两处例外。以「不」「别」开头的固定词不算,例如「不开心」「不好意思」「不仅」「不管」「不得不」「不知道」「别扭」「别人」,完整列表在 `chat/persona_avoid.py`。以「不」开头的分句是句子的条件时也不算:分句里出现「就」「起来」,或者「的时候」「时」「的话」在分句末尾、后面紧跟「会」「就」「也」「先」这类字(如「不同意就直说」「不开心的时候会撒娇」);「不在别人难过时开玩笑」里「时」后面才是被否定的动作,照常取出,「时事」「按时」「成就」这类词里的「时」「就」不作条件。以「别」开头的分句是祈使句,不受条件这一条限制。 + +取句按字面判断,取出的也可能是描述句,所以这一段只写明这几句是角色材料的原文,让模型按原句的意思再对一遍,不把每一句称为禁令。只复述角色自己写的句子,框架不增加条目;没有这类句子时不出现这一段。 + 原话、人工样例、学习参考和已发送记录在上下文中分别标明来源。 记忆抽取把群友明确表达的相处偏好记在这位群友名下。 diff --git a/src/len_bot/next/chat/attention.py b/src/len_bot/next/chat/attention.py index a37c7c469..9cd8f5788 100644 --- a/src/len_bot/next/chat/attention.py +++ b/src/len_bot/next/chat/attention.py @@ -14,6 +14,7 @@ from zoneinfo import ZoneInfo from .session import Chat +from .freshness import topic_freshness from .declarations import AttentionArguments, Declared, declare, match_message, next_due, persisted, tick from .disposition import Disposition, allows from .recap import estimate_content, estimate_request @@ -45,10 +46,14 @@ EFFECT_WINDOW_SECONDS = 3 * 86400 EFFECT_HALF_LIFE_SECONDS = 86400 EFFECT_FLOOR = 0.5 +# Welcomed interjections shorten the interval by at most a fifth; disliked ones may double it. +EFFECT_CEILING = 1.25 PRIORITY = {"direct": 0, "named": 1, "attention": 2, "focus": 3, "ambient": 4, "explore": 5} REACTIONS = {"哈哈", "哈哈哈", "草", "6", "笑死", "啊", "哦", "嗯", ""} # The follow-up an interjection that answered no one opens: the next message from anyone. ANYONE = "*" +# After the host was down or chat turns failed at the model, joining in looks only at this much recent chat. +CATCH_UP_SECONDS = 300 def wake_channel(*, scheduled: bool, notices: bool, events: bool, audio: bool, pending: str | None) -> str: @@ -237,6 +242,11 @@ def __init__(self, chat: Chat, emit: Callable[[dict], None], *, self.chat.toolset.attention = self.declare self.chat.set_external_tools(list(self.chat.toolset.external.values())) self.resume = self.chat.restore() + # Since when the scene is behind: input left from before this start, or a turn that failed at the + # model. Until a turn gets a model reply, joining in looks only at the last CATCH_UP_SECONDS. + left = [at for _, _, at in self.store.pending_messages(self.config.scene) + if at is not None and at < self.started_at] + self.behind_since: float | None = min(left, default=None) def wake_words(self) -> tuple[str, ...]: persona = self.chat.persona @@ -599,7 +609,11 @@ def offered(wake: PendingWake, reason: str, factor: float | None = None) -> Wake state.answered() return offered(PendingWake('named', at, words, people=[message.sender.uid]), '点名:' + '、'.join(words)) if matches and self.enforcing: - return offered(PendingWake('attention', at), '持续关注条件命中') + if any(state.declared.may_wake(match, at) for match in matches): + return offered(PendingWake('attention', at), '持续关注条件命中') + # Held until the intent's rewake window ends; attention_tick then wakes on it. + log_event(logger, 'attention_candidate_held', intents=[match['intent'] for match in matches], + message_id=message.platform_message_id) local_blocked = False followup = self.followup_of(state, message, at) if followup is not None: @@ -748,8 +762,9 @@ async def wait_for_messages(self, seconds: float) -> str: def effect_factor(self) -> float: """Up to EFFECT_SAMPLES judged reactions to this group's interjections in the window, each weighted by - age: ignored or disliked ones lower the factor toward EFFECT_FLOOR, and the interval before the next - interjection grows by its inverse; welcomed ones never shorten it. With no reactions the factor is 1.""" + age: ignored or disliked ones lower the factor toward EFFECT_FLOOR and welcomed ones raise it toward + EFFECT_CEILING; the interval before the next interjection is divided by it. The same balance moves the + factor down twice as far as up, so the bot holds back faster than it opens up. With no reactions it is 1.""" now = self.now() reactions = ReplyEffectStore(self.store).reactions( self.config.scene, ("ambient", "explore"), now - EFFECT_WINDOW_SECONDS, EFFECT_SAMPLES) @@ -757,7 +772,7 @@ def effect_factor(self) -> float: for at, reaction in reactions] # Two neutral samples' worth of prior keeps one bad reaction from doubling the interval. balance = sum(weight * value for weight, value in weights) / (sum(weight for weight, _ in weights) + 2) - return 1 + min(0.0, balance) * (1 - EFFECT_FLOOR) + return 1 + (balance * (1 - EFFECT_FLOOR) if balance < 0 else balance * (EFFECT_CEILING - 1)) def ambient_interval(self) -> float: """How long after the bot last spoke, or last looked in unprompted, it may join in again.""" @@ -1007,14 +1022,55 @@ def fits(end: int) -> bool: start = good return pending[-1][0], contents + def catch_up_batch(self, pending: list[tuple[int, ChatMessage, float]], reason: str) -> tuple[int, list[str]]: + """A batch while the scene is behind: older input under its own heading, the recent part under the wake's.""" + since = datetime.fromtimestamp(self.behind_since, ZoneInfo(self.config.timezone)).isoformat(timespec="seconds") + note = (f"{since} 起 Bot 停机或模型请求失败,期间的消息当时没有回应;按各条原时间理解,不逐条补回," + "@ 你的可以按原时间简短回应") + fresh_from = self.now() - CATCH_UP_SECONDS + split = next((index for index, (_, _, at) in enumerate(pending) if at is not None and at >= fresh_from), + len(pending)) + old, fresh = pending[:split], pending[split:] + if not old: + return self.batch(fresh, reason[:-1] + ";" + note + "]") + if not fresh: + return self.batch(old, reason[:-1] + ";较早的消息:" + note + "]") + _, earlier = self.batch(old, "[较早的消息:" + note + "]") + through, recent = self.batch(fresh, reason) + return through, earlier + recent + + def fresh_to_join(self, pending: list[tuple[int, ChatMessage, float]]) -> bool: + """While behind, something from the last CATCH_UP_SECONDS that is worth joining in on.""" + fresh_from = self.now() - CATCH_UP_SECONDS + return any(at is not None and at >= fresh_from and worth_joining(message, self.settings, self.config.bot_id) + for _, message, at in pending) + + def with_freshness(self, batch: tuple[int, list[str]], + pending: list[tuple[int, ChatMessage, float]]) -> tuple[int, list[str]]: + """An unprompted wake's batch, ending with how fresh the talk that woke it is.""" + woke_at = self.state.pending.first_at + if self.state.pending.channel == 'attention' and self.state.declared is not None: + # A held message match wakes at the end of its rewake window, after it was received. + woke_at = min([woke_at, *(match['at'] for match in self.state.declared.matches + if match['message_id'] is not None)]) + freshness = topic_freshness(pending, woke_at=woke_at, now=self.now(), + timezone=self.config.timezone, + excluded=(self.config.bot_id, *self.settings.other_bot_ids)) + through, contents = batch + return through, contents if freshness is None else [*contents[:-1], contents[-1] + "\n" + freshness] + def consumed_state(self, *, new_turn: bool = False) -> AttentionState: state = copy.deepcopy(self.state) if state.declared is not None: + if state.pending is not None and state.pending.channel == 'attention': + state.declared.woke(self.now()) state.declared.consume(new_turn=new_turn) if state.pending is not None: if state.pending.keywords: state.keyword_last.update(dict.fromkeys(state.pending.keywords, self.now())) - if state.pending.channel == "ambient": + # A turn the model asked for by an intent is a look at the chat too: the interjection + # interval, the fallback when nothing is declared, starts again from it. + if state.pending.channel in {"ambient", "attention"}: state.ambient_last_at = self.now() state.pending = None if new_turn: @@ -1235,6 +1291,10 @@ async def _turn(self, channel: str, **turn) -> None: expressions=len(result["expressions"]), steps=self.chat.trace_step or 0) if result["status"] == "limited": self.resume = True + if result.get("model_failure") is not None: + self.behind_since = result["model_failure"]["at"] if self.behind_since is None else self.behind_since + elif result.get("model_replies"): + self.behind_since = None result["pending_wake"] = pending_wake result["channel"] = channel result["silence_level"] = state.silence_level @@ -1335,10 +1395,22 @@ async def _run(self) -> None: channels.add(self.state.pending.channel) if self.state.pending.explored: channels.add('explore') + catching_up = self.behind_since is not None and not self.resume + if catching_up and channel in {"ambient", "explore"} and not self.fresh_to_join(pending): + # Old chat alone does not open an interjection; it reaches the model with the next turn. + state = copy.deepcopy(self.state) + state.pending = None + self.save_state(state) + log_event(logger, 'attention_catch_up', status='skipped', channel=channel, + behind_since=self.behind_since, messages=len(pending)) + continue reason = ("[恢复未结束的对话]" if self.resume else "[此前未触发唤醒的消息]" if channel in {"schedule", "task", "plugin", "audio"} and self.state.pending is None else self.wake_reason()) - batch = self.batch(pending, reason) if pending else None + batch = (None if not pending else self.catch_up_batch(pending, reason) if catching_up + else self.batch(pending, reason)) + if batch is not None and channel in {"ambient", "explore", "attention"}: + batch = self.with_freshness(batch, pending) direct = ((self.state.pending is not None and self.state.pending.channel == "direct") or (self.resume and self.state.resume_reserve)) wake_received_at = (self.state.pending.first_at diff --git a/src/len_bot/next/chat/context.py b/src/len_bot/next/chat/context.py index 37f796b4e..404f213b2 100644 --- a/src/len_bot/next/chat/context.py +++ b/src/len_bot/next/chat/context.py @@ -15,6 +15,9 @@ from ..media.audio_store import AudioStore from ..config import LabConfig from .declarations import Declared +from .group_voice import describe as describe_group_voice +from .persona_avoid import avoidance_text +from .reply_feedback import reply_feedback from .recap import project_history from ..models.limits import speech_quota from ..models.projection import project_messages, project_old_results @@ -209,17 +212,17 @@ def turn_state(config: LabConfig, store: Store, *, now: float, # Interjections: the only single characters worth naming as a habit. Sentence-final particles such # as 吧、呢、啊 are ordinary grammar and stay out. HABIT_CHARACTERS = frozenset("草哼呜哇诶欸嘿喵捏哈噢嗷嘻") +# Function characters too common to anchor a sentence template on their own. +TEMPLATE_FILLER = frozenset("的了是我你他她它在有和就也都不这那个一吧呢啊呀啦嘛哦么得地着过把被给让要会能说") HAN_RUN = re.compile(r"[\u4e00-\u9fff]+") +HAN = re.compile(r"[\u4e00-\u9fff]") +SENTENCE_BREAK = re.compile(r"[。!?!?~~…\n]+") +CLAUSE_BREAK = re.compile(r"[,,。!?!?~~…;;::、\s]+") +TEMPLATE_GRAM = 6 -def recent_habits(entries: Sequence[tuple[int, dict]], heard: Sequence[str] = ()) -> str | None: - """Name what the latest own expressions in the history keep repeating, for the next one to vary. - - The model reads its earlier lines as the strongest example of how it talks, so a phrase that - slipped in a few times tends to stay. ``heard`` is what others said recently; words they use - belong to the topic and are not named. Punctuation and pictures sent with text are part of the - persona's voice and are not counted. - """ +def _expressions(entries: Sequence[tuple[int, dict]]) -> list[tuple[str, dict]]: + """The latest own ``say`` and ``react`` calls in the history, newest first.""" expressions = [] for _, message in reversed(entries): if message["role"] != "assistant": @@ -235,12 +238,81 @@ def recent_habits(entries: Sequence[tuple[int, dict]], heard: Sequence[str] = () expressions.append((call["function"]["name"], arguments)) if len(expressions) >= HABIT_WINDOW: break - expressions = expressions[:HABIT_WINDOW] + return expressions[:HABIT_WINDOW] + + +def _common(values: Sequence[set[str]], enough: int) -> tuple[str, int] | None: + """The longest value shared by the most texts, when at least ``enough`` share it.""" + frequency = Counter(value for found in values for value in found) + best = sorted(frequency.items(), key=lambda item: (-item[1], -len(item[0]), item[0])) + return best[0] if best and best[0][1] >= enough else None + + +def _prefixes(clause: str, shortest: int, longest: int) -> set[str]: + run = HAN_RUN.match(clause) + return set() if run is None else {run.group(0)[:size] for size in range(shortest, min(longest, len(run.group(0))) + 1)} + + +def _templates(texts: Sequence[str], enough: int, taken: Sequence[str], others: str) -> list[tuple[str, int]]: + """Two fixed pieces with a changing middle that keep coming back in one sentence, such as 「越……越」.""" + occurrences = [] + for text in texts: + found: dict[str, list[tuple[int, int, int]]] = {} + for number, sentence in enumerate(SENTENCE_BREAK.split(text)): + for run in HAN_RUN.finditer(sentence): + for size in range(1, TEMPLATE_GRAM + 1): + for start in range(run.start(), run.end() - size + 1): + found.setdefault(sentence[start:start + size], []).append((number, start, start + size)) + occurrences.append((found, SENTENCE_BREAK.split(text))) + frequency = Counter(gram for found, _ in occurrences for gram in found) + usable = {gram for gram, count in frequency.items() if count >= enough + and (len(gram) > 1 or gram not in TEMPLATE_FILLER) + and not any(gram in word or word in gram for word in taken) + and not (len(gram) > 1 and gram in others)} + # A piece inside a longer one found in as many texts says nothing more. + usable = {gram for gram in usable if not any(gram != longer and gram in longer and frequency[longer] == frequency[gram] + for longer in usable)} + pairs: dict[tuple[str, str], tuple[int, set[str]]] = {} + for found, sentences in occurrences: + seen: dict[tuple[str, str], str] = {} + grams = [(gram, place) for gram in usable for place in found.get(gram, ())] + for first, (number, _, first_end) in grams: + for second, (other, second_start, _) in grams: + if other == number and second_start > first_end and len(first) + len(second) >= 3: + seen.setdefault((first, second), sentences[number][first_end:second_start]) + for pair, middle in seen.items(): + count, middles = pairs.get(pair, (0, set())) + pairs[pair] = (count + 1, middles | {middle}) + chosen: list[tuple[str, int]] = [] + used: list[str] = [] + for (first, second), (count, middles) in sorted( + pairs.items(), key=lambda item: (-item[1][0], -len(item[0][0]) - len(item[0][1]), item[0])): + if count < enough or len(chosen) == 2: + break + # The same middle every time is one fixed phrase, which the word count already covers. + if len(middles) < 2 or any(piece in other or other in piece for piece in (first, second) for other in used): + continue + chosen.append((f"{first}……{second}", count)) + used += [first, second] + return chosen + + +def recent_habits(entries: Sequence[tuple[int, dict]], heard: Sequence[str] = ()) -> str | None: + """Name what the latest own expressions in the history keep repeating, for the next one to vary. + + The model reads its earlier lines as the strongest example of how it talks, so a phrase or a + sentence shape that slipped in a few times tends to stay. Words, the opening and ending of each + line, the opening of its last clause and two-piece templates are counted; ``heard`` is what + others said recently, and words they use belong to the topic and are not named. Pictures sent + with text are part of the persona's voice and are not counted. + """ + expressions = _expressions(entries) if len(expressions) < 5: return None texts = [arguments["content"] for name, arguments in expressions if name == "say" and isinstance(arguments.get("content"), str)] habits = [] + others = "\n".join(heard) if texts: enough = max(4, math.ceil(len(texts) * 0.6)) frequency = Counter() @@ -251,7 +323,6 @@ def recent_habits(entries: Sequence[tuple[int, dict]], heard: Sequence[str] = () grams.update(run[start:start + size] for start in range(len(run) - size + 1) if size > 1 or run[start] in HABIT_CHARACTERS) frequency.update(grams) - others = "\n".join(heard) chosen = [] for gram, count in sorted(frequency.items(), key=lambda item: (-item[1], -len(item[0]), item[0])): if count < enough or len(chosen) == 3: @@ -260,6 +331,7 @@ def recent_habits(entries: Sequence[tuple[int, dict]], heard: Sequence[str] = () continue chosen.append((gram, count)) habits.extend(f"{count} 条用了「{gram}」" for gram, count in chosen) + habits.extend(_shapes(texts, [gram for gram, _ in chosen], others)) files = Counter() for name, arguments in expressions: choice = arguments.get("sticker") if name == "say" else arguments @@ -273,6 +345,37 @@ def recent_habits(entries: Sequence[tuple[int, dict]], heard: Sequence[str] = () count=len(expressions), habits=",".join(habits)) +def _shapes(texts: Sequence[str], words: Sequence[str], others: str) -> list[str]: + """Sentence shapes shared by half of the texts: how they open, end and close, and templates.""" + enough = max(4, math.ceil(len(texts) / 2)) + taken = list(words) + shapes = [] + openings = [{prefix for prefix in _prefixes(text.lstrip(), 2, TEMPLATE_GRAM) if prefix not in others} for text in texts] + if (opening := _common(openings, enough)) is not None and not any( + opening[0] in word or word in opening[0] for word in taken): + shapes.append(f"{opening[1]} 条开头都是「{opening[0]}」") + taken.append(opening[0]) + tails = [] + for text in texts: + squeezed = re.sub(r"\s", "", text) + last = None + for last in HAN.finditer(squeezed): + pass + tail = squeezed if last is None else squeezed[last.end():] + tails.append({tail[-size:] for size in range(1, min(6, len(tail)) + 1)}) + if (ending := _common(tails, enough)) is not None: + shapes.append(f"{ending[1]} 条结尾都是「{ending[0]}」") + closings = [] + for text in texts: + clauses = [clause for clause in CLAUSE_BREAK.split(text) if HAN.search(clause)] + closings.append(_prefixes(clauses[-1], 1, 3) if len(clauses) > 1 else set()) + if (closing := _common(closings, enough)) is not None: + shapes.append(f"{closing[1]} 条最后一句都用「{closing[0]}」起头") + taken.append(closing[0]) + shapes.extend(f"{count} 条用了「{template}」这样的句式" for template, count in _templates(texts, enough, taken, others)) + return shapes + + RECENT_SECONDS = 600 @@ -309,6 +412,8 @@ def __init__(self, config: LabConfig, persona: Persona, store: Store, *, self.config, self.persona, self.store = config, persona, store self.platform, self.memory = platform, memory self.host_state: str | None = None + # How members here write, measured by the latest upkeep pass; None until one ran. + self.group_voice: dict | None = None self.system: str def configure_tools(self, allowed: list[dict], external: list[dict], *, skills: tuple[Skill, ...], @@ -371,9 +476,20 @@ async def project(self, recap: str | None, entries: list[tuple[int, dict]], stat state = {**state, "content": state["content"] + "\n" + Template( read_prompt("next_group_profile.md")).substitute(profile=profile.strip())} state = {**state, 'content': state['content'] + '\n' + current_attention(self.config, self.store)} + state = {**state, 'content': '\n'.join([state['content'], *self.references(entries)])} + return [{"role": "system", "content": self.system}] + project_history(recap, entries) + [state] + + def references(self, entries: list[tuple[int, dict]]) -> list[str]: + """The turn's closing reference material, in order: how members here write, what the own + latest lines keep repeating, how members reacted to the own recent lines, then the persona's own + prohibitions, which always stay last, closest to what the model writes next. Material about the scene + goes before them.""" + references = [] + if self.group_voice is not None: + references.append(describe_group_voice(self.group_voice)) heard = [plain_text(message) for message in self.store.recent_context_messages(self.config.scene, limit=50) if not message.is_self] - habits = recent_habits(entries, heard) - if habits is not None: - state = {**state, 'content': state['content'] + '\n' + habits} - return [{"role": "system", "content": self.system}] + project_history(recap, entries) + [state] + references.append(recent_habits(entries, heard)) + references.append(reply_feedback(self.config, self.store)) + references.append(avoidance_text(self.persona)) + return [text for text in references if text is not None] diff --git a/src/len_bot/next/chat/declarations.py b/src/len_bot/next/chat/declarations.py index 878fbd391..2249c2633 100644 --- a/src/len_bot/next/chat/declarations.py +++ b/src/len_bot/next/chat/declarations.py @@ -88,6 +88,13 @@ class Intent: until: float source_turn: str | None source_call: str | None + # When a match of this intent last opened a turn; another message match waits REWAKE_SECONDS. + # Cleared when a held match wakes at the end of that window. + woke_at: float | None = None + + +# One intent opens at most one turn from messages in this long; matches inside it wake together once it ends. +REWAKE_SECONDS = 120.0 @dataclass @@ -126,6 +133,23 @@ def candidate(self, intent: Intent, at: float, message_id: str | None) -> dict: self.matches = [item for item in self.matches if item['intent'] != intent.id] + [match] return match + def may_wake(self, match: dict, at: float) -> bool: + """A timer always may; a message match only once the intent's last wake is REWAKE_SECONDS old.""" + intent = self.find(match['intent']) + return match['message_id'] is None or intent.woke_at is None or at - intent.woke_at >= REWAKE_SECONDS + + def held_until(self, match: dict) -> float | None: + """When a message match received inside its intent's rewake window wakes on its own; None otherwise.""" + woke_at = self.find(match['intent']).woke_at + if match['message_id'] is None or woke_at is None or match['at'] - woke_at >= REWAKE_SECONDS: + return None + return woke_at + REWAKE_SECONDS + + def woke(self, at: float) -> None: + """The pending candidates open a turn now.""" + for match in self.matches: + self.find(match['intent']).woke_at = at + def consume(self, *, new_turn: bool) -> None: previous = [] if new_turn else self.turn_matches identifiers = {item['intent'] for item in self.matches} @@ -210,6 +234,12 @@ def tick(declared: Declared, now: float) -> list[dict]: for condition in due: condition['fired'] = True matched.append(declared.candidate(intent, now, None)) + for match in declared.matches: + due = declared.held_until(match) + if due is not None and due <= now: + # Fires once: the window is over, so any later message match may wake as usual. + declared.find(match['intent']).woke_at = None + matched.append(match) return matched @@ -217,6 +247,7 @@ def next_due(declared: Declared) -> float | None: times = [intent.until for intent in declared.intents] times += [condition['due'] for intent in declared.intents for condition in intent.conditions if condition['type'] == 'at' and not condition['fired']] + times += [due for due in map(declared.held_until, declared.matches) if due is not None] return min(times, default=None) diff --git a/src/len_bot/next/chat/freshness.py b/src/len_bot/next/chat/freshness.py new file mode 100644 index 000000000..6bfa4bbfb --- /dev/null +++ b/src/len_bot/next/chat/freshness.py @@ -0,0 +1,71 @@ +"""How fresh the talk is that an unprompted wake would join, stated as facts for the mind. + +An ambient or attention wake can open on a batch that started minutes ago, after the group moved +on. The host does not judge topics; it states when the message that woke the turn was sent, how +much was said after it, how much of that shares words with it, and how long the chat paused, so +the model can leave an old topic alone unless it judges it worth returning to. +""" + +from __future__ import annotations + +from collections.abc import Sequence +from datetime import datetime +import re +from string import Template +from zoneinfo import ZoneInfo + +from ..platform.messages import ChatMessage, plain_text +from ..prompt_files import read_prompt + +# A pause at least this long between member messages is reported. +PAUSE_SECONDS = 300 +HAN_RUN = re.compile(r"[\u4e00-\u9fff]+") +WORD = re.compile(r"[a-z0-9]{2,}") + + +def _words(text: str) -> set[str]: + text = text.casefold() + words = set(WORD.findall(text)) + for run in HAN_RUN.findall(text): + words.update(run[start:start + 2] for start in range(len(run) - 1)) + return words + + +def _age(seconds: float) -> str: + seconds = max(0.0, seconds) + if seconds < 90: + return f"{round(seconds)} 秒" + if seconds < 5400: + return f"{round(seconds / 60)} 分钟" + return f"{seconds / 3600:.1f} 小时" + + +def topic_freshness(pending: Sequence[tuple[int, ChatMessage, float]], *, woke_at: float, now: float, + timezone: str, excluded: Sequence[str]) -> str | None: + """Facts about the batch around the first member message received at or after ``woke_at``. + + None when there is no such message, as for a wake from a timer the model set. + """ + members = [(message, at) for _, message, at in pending + if not message.is_self and message.sender.uid not in excluded] + index = next((number for number, (_, at) in enumerate(members) if at >= woke_at), None) + if index is None: + return None + trigger = members[index][0] + after = [message for message, _ in members[index + 1:]] + clock = datetime.fromtimestamp(trigger.time, ZoneInfo(timezone)).strftime("%H:%M:%S") + facts = [f"叫醒你的是 {clock} 那条({_age(now - trigger.time)}前)"] + if after: + shared = _words(plain_text(trigger)) + related = sum(1 for message in after if shared & _words(plain_text(message))) + people = len({message.sender.uid for message in after}) + facts.append(f"之后又有 {len(after)} 条、{people} 个人在说," + + (f"其中 {related} 条和它用到相同的字词" if related else "都没有和它相同的字词")) + facts.append(f"最后一条在 {_age(now - after[-1].time)}前") + else: + facts.append("之后没有新消息") + times = [trigger.time, *(message.time for message in after)] + pauses = [later - earlier for earlier, later in zip(times, times[1:]) if later - earlier >= PAUSE_SECONDS] + if pauses: + facts.append(f"中间停过 {len(pauses)} 次,最长停了 {_age(max(pauses))}") + return Template(read_prompt("next_topic_freshness.md")).substitute(facts=";".join(facts)) diff --git a/src/len_bot/next/chat/group_voice.py b/src/len_bot/next/chat/group_voice.py new file mode 100644 index 000000000..2c89cea39 --- /dev/null +++ b/src/len_bot/next/chat/group_voice.py @@ -0,0 +1,98 @@ +"""How members of one group usually write, measured from their recent messages. + +The result is reference material for the mind: the length, sentence count, picture-only share and +consecutive sends of the people it talks with. It describes the group, it does not tell the persona +how to talk; what the persona material says about its own voice comes first. +""" + +from __future__ import annotations + +from collections.abc import Sequence +import math +import re +from string import Template + +from ..config import LabConfig +from ..platform.messages import ChatMessage, plain_text +from ..prompt_files import read_prompt +from ..storage.store import Store + +# Messages read back from the scene, the members' share of them is measured. +SAMPLE_MESSAGES = 300 +# Fewer member messages than this say little about how the group writes. +MIN_MESSAGES = 30 +# A member's next message within this many seconds continues the same send. +BURST_GAP_SECONDS = 60 +SENTENCE_BREAK = re.compile(r"[。!?!?;;…~~\n]+|\s{1,}") +WORDLIKE = re.compile(r"[\w\u4e00-\u9fff]") + + +def _quantile(values: Sequence[int], share: float) -> int: + ordered = sorted(values) + return ordered[min(len(ordered) - 1, math.ceil(len(ordered) * share) - 1)] + + +def _sentences(text: str) -> int: + return sum(1 for piece in SENTENCE_BREAK.split(text) if WORDLIKE.search(piece)) + + +def measure(messages: Sequence[ChatMessage], *, excluded: Sequence[str]) -> dict | None: + """Shape figures for the members' messages among ``messages`` (oldest first), or None when too few. + + ``excluded`` are accounts whose messages are not members' (this bot and other bots). Messages + from them still end a member's consecutive send. + """ + members, runs = [], [] + previous: ChatMessage | None = None + for message in messages: + member = (not message.is_self and message.send_status == "received" + and message.sender.uid not in excluded) + if member: + members.append(message) + if (previous is not None and previous.sender.uid == message.sender.uid + and message.time - previous.time <= BURST_GAP_SECONDS): + runs[-1] += 1 + else: + runs.append(1) + previous = message if member else None + if len(members) < MIN_MESSAGES: + return None + texts = [text for text in (plain_text(message).strip() for message in members) if text] + pictures = sum(1 for message in members if not plain_text(message).strip() + and any(segment.type == "image" for segment in message.segments)) + lengths = [len(re.sub(r"\s", "", text)) for text in texts] + sentences = [_sentences(text) for text in texts] + return { + "messages": len(members), + "text_messages": len(texts), + "length_median": _quantile(lengths, 0.5) if texts else None, + "length_p90": _quantile(lengths, 0.9) if texts else None, + "single_sentence_share": round(sum(count <= 1 for count in sentences) / len(texts), 2) if texts else None, + "sentences_p90": _quantile(sentences, 0.9) if texts else None, + "picture_only_share": round(pictures / len(members), 2), + "burst_share": round(sum(1 for run in runs if run > 1) / len(runs), 2), + "burst_p90": _quantile(runs, 0.9), + } + + +def measure_scene(store: Store, config: LabConfig) -> dict | None: + """Measure the scene's latest messages; a read-only store call.""" + return measure(store.recent(config.scene, SAMPLE_MESSAGES), + excluded=(config.bot_id, *config.attention.other_bot_ids)) + + +def describe(figures: dict) -> str: + """The figures as the turn's reference text.""" + parts = [] + if figures["length_median"] is not None: + parts.append(f"文字消息一般 {figures['length_median']} 个字左右,九成不超过 {figures['length_p90']} 个字") + parts.append(f"{round(figures['single_sentence_share'] * 100)}% 的消息只有一句," + f"九成不超过 {figures['sentences_p90']} 句") + parts.append(f"只发图片的占 {round(figures['picture_only_share'] * 100)}%") + if figures["burst_share"]: + parts.append(f"{round(figures['burst_share'] * 100)}% 的时候会一个人连着发几条," + f"九成不超过 {figures['burst_p90']} 条") + else: + parts.append("很少有人连着发几条") + return Template(read_prompt("next_group_voice.md")).substitute( + count=figures["messages"], figures=";".join(parts)) diff --git a/src/len_bot/next/chat/persona_avoid.py b/src/len_bot/next/chat/persona_avoid.py new file mode 100644 index 000000000..7093d64be --- /dev/null +++ b/src/len_bot/next/chat/persona_avoid.py @@ -0,0 +1,84 @@ +"""Repeat the persona material's own negated sentences next to the turn's generation. + +Only sentences the persona wrote are repeated, word for word; the framework adds none of its own. +The persona text sits at the start of a long system message, and the things it says not to do are +the part a model loses first, so they are read once more at the end of each turn's reference +material. Picking them out is a heuristic, so the section presents them as original sentences to +follow as written, not as a list of prohibitions. +""" + +from __future__ import annotations + +import re +from string import Template + +from ..persona.profile import Persona +from ..prompt_files import read_prompt + +# Quoted text is the persona's sample speech; it is left out when judging a sentence. +QUOTED_SPAN = r"「[^」\n]*」|『[^』\n]*』|“[^”\n]*”|\"[^\"\n]*\"|‘[^’\n]*’" +QUOTED = re.compile(QUOTED_SPAN) +# A quoted span ending in 。!? stays inside its sentence. +SENTENCE = re.compile(rf"(?:{QUOTED_SPAN}|[^。!!??;;\n])+[。!!??;;]*") +CLAUSE = re.compile(r"[,,、::()()\s]+") +# A prohibition written anywhere in a sentence. +FORBIDDING = re.compile(r"不要|不准|不许|禁止|避免|切勿") +SUBJECTS = ("她", "他", "你", "我", "它") +ADVERBS = re.compile(r"^(?:也|都|就|从来|从|绝对|一律|永远)") +# Words that start with 不 or 别 without negating an action. +WORDS = ( + "不是", "不开心", "不高兴", "不好意思", "不仅", "不但", "不只", "不光", "不小心", "不久", "不错", + "不得不", "不愧", "不少", "不论", "不管", "不如", "不然", "不过", "不同", "不必", "不一定", + "不知道", "不知不觉", "不一会", "不太", "不算", "不会的", "不禁", "不由得", "不妨", "不停", "不断", "不止", + "别扭", "别致", "别的", "别人", "别处", +) +# A 不 clause that goes on like this is the condition of the sentence, as in 不同意就直说 or +# 不开心的时候会撒娇; a time phrase before the negated action itself, as in 不在人难过时开玩笑, is not. +# A 别 clause is an imperative and is never a condition. +CONDITION = re.compile(r"(?:的时候|时|的话)(?:$|会|就|也|都|要|总|还|才|先|再|可以)|就|起来") +# Words containing 时 or 就 that do not mark a condition; removed before looking for one. +NOT_CONDITION = re.compile( + r"时间|时刻|时常|时事|时政|时期|时代|时机|时光|时髦|时尚|时不时|按时|及时|准时|同时|平时|随时|当时|临时|暂时|小时" + r"|成就|就业|就算|将就|迁就|造就") + + +def _negates_an_action(clause: str, subjects: tuple[str, ...]) -> bool: + for subject in sorted(subjects, key=len, reverse=True): + if subject and clause.startswith(subject): + clause = clause[len(subject):] + break + clause = ADVERBS.sub("", clause) + if not clause.startswith(("不", "别")) or clause.startswith(WORDS): + return False + return clause.startswith("别") or CONDITION.search(NOT_CONDITION.sub("", clause), 1) is None + + +def _counts(sentence: str, subjects: tuple[str, ...]) -> bool: + unquoted = QUOTED.sub(",", sentence) + if FORBIDDING.search(unquoted): + return True + return any(_negates_an_action(clause, subjects) for clause in CLAUSE.split(unquoted) if clause) + + +def avoidances(persona: Persona) -> list[str]: + """The persona's own sentences with a negated action or a prohibition, in the order written.""" + subjects = (*SUBJECTS, persona.name, *persona.self_reference, *persona.aliases) + found: list[str] = [] + for material in (persona.behavior, persona.voice, persona.boundaries): + for line in material.splitlines(): + line = re.sub(r"^\s*(?:[-*+]|\d+[.、)])\s*", "", line) + for match in SENTENCE.finditer(line): + sentence = match.group(0).strip() + if not sentence or sentence.startswith("#") or sentence in found: + continue + if _counts(sentence, subjects): + found.append(sentence) + return found + + +def avoidance_text(persona: Persona) -> str | None: + found = avoidances(persona) + if not found: + return None + return Template(read_prompt("next_persona_avoid.md")).substitute( + items="\n".join(f"- {sentence}" for sentence in found)) diff --git a/src/len_bot/next/chat/reply_feedback.py b/src/len_bot/next/chat/reply_feedback.py new file mode 100644 index 000000000..162b0a16d --- /dev/null +++ b/src/len_bot/next/chat/reply_feedback.py @@ -0,0 +1,50 @@ +"""How members actually reacted to the bot's own recent lines here, as the turn's reference. + +Only facts from judged reply effects: the line, the judged reaction and the judge's one-line reason. Whether the +next line should change is left to the model and the persona. +""" + +from __future__ import annotations + +import re +from string import Template + +from ..config import LabConfig +from ..learning.reply_effect_store import ReplyEffectStore +from ..platform.messages import plain_text +from ..prompt_files import read_prompt +from ..storage.store import Store + +WINDOW_SECONDS = 3 * 86400 +MAX_LINES = 6 +LINE_CHARS = 40 +REASON_CHARS = 40 +TAKEN = {"agree": "附和", "continue": "接着聊"} +NOT_TAKEN = {"unrelated": "没人接这句", "negative": "被嫌弃", "correct": "被纠正"} + + +def _clip(text: str, limit: int) -> str: + text = re.sub(r"\s+", " ", text).strip() + return text if len(text) <= limit else text[:limit] + "…" + + +def reply_feedback(config: LabConfig, store: Store) -> str | None: + """The newest judged lines here, picked-up ones first; None when reply effects are off or nothing is judged.""" + if config.learning is None or not config.learning.reply_effects: + return None + rows = ReplyEffectStore(store).judged_lines( + config.scene, [*TAKEN, *NOT_TAKEN], store.now() - WINDOW_SECONDS, MAX_LINES) + groups: dict[str, list[str]] = {"taken": [], "not_taken": []} + for row in rows: + message = store.read_message(config.scene, row["seq"]) + if message is None: + continue # Removed by retention since it was judged. + line = _clip(plain_text(message), LINE_CHARS) or "(只发了表情)" + label = TAKEN.get(row["reaction"]) or NOT_TAKEN[row["reaction"]] + reason = _clip(row["reason"] or "", REASON_CHARS) + groups["taken" if row["reaction"] in TAKEN else "not_taken"].append( + f"- 「{line}」{label}" + (f":{reason}" if reason else "")) + if not groups["taken"] and not groups["not_taken"]: + return None + return Template(read_prompt("next_reply_feedback.md")).substitute( + taken="\n".join(groups["taken"]) or "- 最近没有", not_taken="\n".join(groups["not_taken"]) or "- 最近没有") diff --git a/src/len_bot/next/chat/session.py b/src/len_bot/next/chat/session.py index 76f4b9ff3..a648f61ab 100644 --- a/src/len_bot/next/chat/session.py +++ b/src/len_bot/next/chat/session.py @@ -15,6 +15,7 @@ from ..media.audio import AudioService from ..media.audio_store import AudioStore from .context import ChatContext, style_text, turn_state +from .group_voice import measure_scene from .expression import ChatExpression, MessageSender from .tools import MemoryPlugins, SceneTools from ..config import LabConfig @@ -43,7 +44,7 @@ from ..reference.library import ReferenceLibrary from ..reference.tool import changes_notice from ..platform.messages import UploadResult -from ..models.client import ChatModel, ModelReply +from ..models.client import ChatModel, ModelConnectionError, ModelHTTPError, ModelOutputLimitError, ModelReply from ..models.request import request_estimate, request_model from ..models.slots import ModelSlots from ..persona.profile import Persona, select_style @@ -87,6 +88,13 @@ def with_images(messages: list[dict], looked: list[tuple[str, bytes]]) -> list[d return [*messages[:-1], {**state, "content": parts}] +def model_failure(error: ModelHTTPError | ModelConnectionError | TimeoutError, at: float) -> dict: + """The kind of provider failure a turn ended on, for the owner's alert: an HTTP status, no connection or a timeout.""" + kind = (f"HTTP {error.status}" if isinstance(error, ModelHTTPError) else + "请求超时" if isinstance(error, TimeoutError) else "连接失败") + return {"kind": kind, "at": at, "error": f"{type(error).__name__}: {error}"} + + def final_step(messages: list[dict], tools: list[dict]) -> tuple[list[dict], list[dict]]: """The last step of a turn that looked something up and has not spoken: only say and react.""" state = messages[-1] @@ -151,6 +159,13 @@ def __init__(self, config: LabConfig, persona: Persona, store: Store, self.turn_stopped = False self.on_update = on_update self.on_compaction = on_compaction + # The input estimate at which an automatic recap last ran out of its allowance; until the history + # has grown by another keep_recent_tokens, turns go on with the existing recap instead of paying + # for the same failing recap again. + self.compaction_held_at: int | None = None + # Successful mind replies in the running turn, and the provider failure that ended it. + self.turn_model_replies = 0 + self.turn_model_failure: dict | None = None if config.models.roles.vision is not None and vision is None: raise ValueError("vision 客户端必须与根配置的视觉模型绑定一起提供") self.skills = () if tasks is None else tasks.skills[config.scene] @@ -229,14 +244,23 @@ def validate(reply: ModelReply) -> None: f"新回想正文估算 {content_tokens} token,超过本步回想预算 " f"{recap_target.summary_budget_tokens}") - return await request_model( - self.config, self.store, model, messages, tools, scene=self.config.scene, role=role, - turn_id=turn_id, slots=self.slots, direct=self.direct_request, - output_tokens=self.config.recap_output_tokens if role == "recap" else None, - validate=validate, append_to_scene=role == "mind", - expression_ids=self.expression_ids if role == "mind" else expression_ids, - input_estimate=input_estimate, notify=self.notify, recall_items=recall_items, quick=quick or role == "recap", - trace=None if self.on_trace is None else lambda event: self.trace('turn.step', turn_id, **event)) + try: + reply = await request_model( + self.config, self.store, model, messages, tools, scene=self.config.scene, role=role, + turn_id=turn_id, slots=self.slots, direct=self.direct_request, + output_tokens=self.config.recap_output_tokens if role == "recap" else None, + validate=validate, append_to_scene=role == "mind", + expression_ids=self.expression_ids if role == "mind" else expression_ids, + input_estimate=input_estimate, notify=self.notify, recall_items=recall_items, quick=quick or role == "recap", + trace=None if self.on_trace is None else lambda event: self.trace('turn.step', turn_id, **event)) + except (ModelHTTPError, ModelConnectionError, TimeoutError) as error: + # A TimeoutError raised here is the request's own limit; the turn's deadline cancels from outside. + if model_role == "mind": + self.turn_model_failure = model_failure(error, self.now()) + raise + if role == "mind": + self.turn_model_replies += 1 + return reply async def compact(self, turn_id: str, plan: CompactionPlan, *, strategy: bool = True, chat: list[dict] | None = None) -> str: @@ -265,7 +289,11 @@ async def compact(self, turn_id: str, plan: CompactionPlan, *, strategy: bool = return recap async def upkeep_fork(self) -> tuple[list[dict], list[dict]]: - """The chat request's system, tools and history without the turn state; caller owns the scene lock.""" + """The chat request's system, tools and history without the turn state; caller owns the scene lock. + + Each upkeep pass starts here, and also measures how members of the scene write for later turns. + """ + self.context.group_voice = await self.store.read(measure_scene, self.store, self.config) recap, entries = await self.store.read(self.store.active_history, self.config.scene) self.toolset.discovered_tools = set(await self.store.read(self.store.load_discovered_tools, self.config.scene)) state = await self.store.read(turn_state, self.config, self.store, now=self.now()) @@ -302,6 +330,7 @@ async def compact_now(self) -> dict: self.store.end_turn(turn_id, "error", f"{type(error).__name__}: {error}") self.notify() raise + self.compaction_held_at = None self.store.end_turn(turn_id, "manual_compaction") if self.on_compaction is not None: self.on_compaction() @@ -406,6 +435,10 @@ async def prepare_context(self, turn_id: str, *, observed_at: float, expression_ method = "usage_scaled_after_recap" if token_scale != 1.0 else method if estimated <= trigger: return PreparedContext(messages, estimated, method) + hold = self.compaction_held_at + fits = estimated + binding.max_output_tokens <= binding.context_window_tokens + if hold is not None and fits and estimated < hold + self.config.compaction.keep_recent_tokens: + return PreparedContext(messages, estimated, method) scale = token_scale plan = plan_compaction( self.context.project_entries(entries), system=messages[0], state=messages[-1], tools=self.toolset.core_tools, @@ -417,7 +450,18 @@ async def prepare_context(self, turn_id: str, *, observed_at: float, expression_ window_tokens=binding.context_window_tokens, source_entries=self.context.conversation_entries(entries), token_scale=scale, ) - await self.compact(turn_id, plan, chat=messages) + try: + await self.compact(turn_id, plan, chat=messages) + except (ModelOutputLimitError, ContextBudgetError) as error: + # A recap that ran out of its allowance or budget keeps the existing recap and history; the + # turn goes on uncompacted while the request still fits the window. + if self.config.recap_plugin is not None or not fits: + raise + self.compaction_held_at = estimated + log_event(logger, 'compaction_held', level=logging.WARNING, error=error, input_tokens=estimated, + trigger_tokens=trigger, window_tokens=binding.context_window_tokens) + return PreparedContext(messages, estimated, method) + self.compaction_held_at = None compacted = True if self.on_compaction is not None: self.on_compaction() @@ -477,6 +521,8 @@ async def run_turn(self, *, batch: tuple[int, list[str]] | None, self.reserve_request = False self.turn_expressions = 0 self.turn_messages = 0 + self.turn_model_replies = 0 + self.turn_model_failure = None observed_at = self.now() self.turn_channels = set() if channels is None else set(channels) scene = self.config.scene @@ -730,4 +776,6 @@ async def run_turn(self, *, batch: tuple[int, list[str]] | None, return {"turn_id": turn_id, "status": status, "error": error_text, "delivery": "simulated" if self.expression.send_message is None else "live", "expressions": expressions, "extensions": extensions, - "failed_tools": failed_tools, "limit_until": limit_until, "limit_kind": limit_kind} + "failed_tools": failed_tools, "limit_until": limit_until, "limit_kind": limit_kind, + "model_replies": self.turn_model_replies, + "model_failure": self.turn_model_failure if status in {"error", "timeout"} else None} diff --git a/src/len_bot/next/chat/upkeep.py b/src/len_bot/next/chat/upkeep.py index 4dd89def3..65e428a03 100644 --- a/src/len_bot/next/chat/upkeep.py +++ b/src/len_bot/next/chat/upkeep.py @@ -25,7 +25,7 @@ from ..learning.jargon_store import JargonStore, contains from ..learning.reply_effect_store import REACTIONS, ReplyEffectStore from ..learning.store import LearningStore -from ..models.client import ChatModel, ModelReply +from ..models.client import ChatModel, ModelConnectionError, ModelHTTPError, ModelReply from ..models.request import request_model from ..models.slots import ModelSlots from ..platform.messages import ChatMessage, plain_text, render_text @@ -117,6 +117,8 @@ def __init__(self, config: SharedConfig, store: Store, mind: ChatModel, *, self._effect_retry: dict[str, int] = {} self._failures: dict[str, int] = {} self._failed_at: dict[str, float] = {} + # Scenes whose automatic retries ran out on provider failures; the chat model's recovery gives them back once. + self._model_stuck: set[str] = set() self._workers: dict[str, asyncio.Task[None]] = {} self.started = False self.errors: dict[str, BaseException] = {} @@ -177,6 +179,7 @@ async def disable(self, scene: str, part: str) -> None: self._effect_retry.pop(scene, None) self._failures.pop(scene, None) self._failed_at.pop(scene, None) + self._model_stuck.discard(scene) def _start_scene(self, scene: str) -> None: with log_context(scene=scene, job="session_upkeep"): @@ -230,6 +233,19 @@ def retry(self, scene: str, part: str, effect_call: int | None = None) -> None: self._retry.add(scene) self._wake[scene].set() + def model_recovered(self) -> None: + """The chat model answered again after failing: passes stopped only by provider failures start over. + + This adds no attempts of its own; it resets the usual automatic retries once per recovery, so a backlog + left by an outage does not wait for a retry from the panel. + """ + for scene in list(self._model_stuck): + self._model_stuck.discard(scene) + self._failures.pop(scene, None) + self._failed_at.pop(scene, None) + if scene in self._wake: + self._wake[scene].set() + def compacted(self, scene: str) -> None: """Older history just left the session: take its range now, below the usual thresholds.""" if scene in self._wake and not self._blocked(scene): @@ -626,9 +642,15 @@ def adjacent(first: str, second: str) -> bool: except Exception as error: self._fail(begun, "failed", _error_text(error)) self._failed(scene) + if isinstance(error, ModelHTTPError | ModelConnectionError | TimeoutError): + if self._failures[scene] >= AUTO_RETRIES: + self._model_stuck.add(scene) + else: + self._model_stuck.discard(scene) LOG.exception("session upkeep failed for %s", scene) else: self._failures.pop(scene, None) + self._model_stuck.discard(scene) if self.on_update is not None: self.on_update() diff --git a/src/len_bot/next/config.py b/src/len_bot/next/config.py index 7ca0b8f73..97b2501b4 100644 --- a/src/len_bot/next/config.py +++ b/src/len_bot/next/config.py @@ -48,12 +48,12 @@ ReplayClockSettings, ) -CONFIG_VERSION = 5 +CONFIG_VERSION = 6 class SharedConfig(BaseModel): model_config = STRICT - config_version: Literal[5] = 5 + config_version: Literal[6] = 6 @property def _instance_root(self) -> Path | None: # Loader location is not a runtime setting and is not serialized or compared. @@ -75,6 +75,9 @@ def _instance_root(self) -> Path | None: delivery: Literal["simulated", "live"] = "simulated" max_steps: int = Field(default=16, gt=0) turn_timeout_seconds: float = Field(default=90.0, gt=0, allow_inf_nan=False) + # Chat turns in a row ending on a provider failure (HTTP error, no connection or request timeout) + # before the owners get one private alert. + model_alert_failed_turns: int = Field(default=3, gt=0, le=1000, strict=True) compaction: Compaction = Field(default_factory=Compaction) text_delivery: TextDelivery = Field(default_factory=TextDelivery) network: NetworkSettings = Field(default_factory=NetworkSettings) @@ -170,12 +173,17 @@ def valid_timezone(cls, value: str) -> str: def recap_output_tokens(self) -> int: return self.compaction.max_output_tokens or self.models.roles.mind.max_output_tokens + @property + def recap_reasoning_effort(self) -> str | None: + """The effort recap requests think at: the compaction setting, else the mind's quick effort.""" + return self.compaction.reasoning_effort or self.models.roles.mind.quick_reasoning_effort + @property def recap_reasoning_reserve(self) -> int: - binding = self.models.roles.mind settings = self.model_settings('mind') - if binding.quick_reasoning_effort is not None: - settings = settings.with_effort(binding.quick_reasoning_effort) + effort = self.recap_reasoning_effort + if effort is not None: + settings = settings.with_effort(effort) if settings.thinking == 'disabled' or settings.reasoning_effort in {'none', 'off'}: return 0 if settings.thinking_budget_tokens is not None: diff --git a/src/len_bot/next/configuration/chat.py b/src/len_bot/next/configuration/chat.py index 825b83440..c05d90f97 100644 --- a/src/len_bot/next/configuration/chat.py +++ b/src/len_bot/next/configuration/chat.py @@ -21,6 +21,9 @@ class Compaction(BaseModel): input_tokens: int = Field(default=60000, gt=0, strict=True) keep_recent_tokens: int = Field(default=20000, gt=0, strict=True) max_output_tokens: int | None = Field(default=None, gt=0) + # Recap requests think at this effort instead of the mind's quick effort, so the thinking a model spends + # on folding history cannot use up the recap's output allowance; null keeps the quick effort. + reasoning_effort: str | None = Field(default=None, min_length=1, pattern=r"\S") class TextDelivery(BaseModel): @@ -205,7 +208,8 @@ class Attention(BaseModel): explore_probability: float = Field(default=0.0, ge=0, le=1, allow_inf_nan=False) max_extensions: int = Field(default=4, ge=0) quiet_hours: QuietHours | None = None - declarations: AttentionDeclarations | None = None + # On by default: the model may declare whom or what to keep following; null turns it off. + declarations: AttentionDeclarations | None = Field(default_factory=AttentionDeclarations) disposition: TurnDisposition | None = None @field_validator("keywords") diff --git a/src/len_bot/next/configuration/migrations.py b/src/len_bot/next/configuration/migrations.py index 1320fd238..6fc5eeae7 100644 --- a/src/len_bot/next/configuration/migrations.py +++ b/src/len_bot/next/configuration/migrations.py @@ -68,7 +68,21 @@ def _format_4_to_5(source: dict) -> dict: return source -STEPS = {1: _format_1_to_2, 2: _format_2_to_3, 3: _format_3_to_4, 4: _format_4_to_5} +def _format_5_to_6(source: dict) -> dict: + # Format 6 adds the recap's own reasoning effort; leaving it out keeps the mind's quick effort. + # It adds model_alert_failed_turns; leaving it out keeps 3. + # It also turns persistent intents on by default. Before, null was how the panel saved the default + # off, so it becomes the new default; from format 6 on, null is a choice to turn them off. + blocks = [source.get("attention"), *(scene.get("attention") for scene in source.get("scenes", {}).values() + if isinstance(scene, dict))] + for attention in blocks: + if isinstance(attention, dict) and "declarations" in attention and attention["declarations"] is None: + del attention["declarations"] + source["config_version"] = 6 + return source + + +STEPS = {1: _format_1_to_2, 2: _format_2_to_3, 3: _format_3_to_4, 4: _format_4_to_5, 5: _format_5_to_6} def migrate_source(source: dict, target: int) -> dict: diff --git a/src/len_bot/next/learning/reply_effect_store.py b/src/len_bot/next/learning/reply_effect_store.py index cc0e13672..ca41e2612 100644 --- a/src/len_bot/next/learning/reply_effect_store.py +++ b/src/len_bot/next/learning/reply_effect_store.py @@ -246,6 +246,13 @@ def reactions(self, scene: str, channels: Sequence[str], since: float, limit: in "WHERE value IN (SELECT value FROM json_each(?))) ORDER BY id DESC LIMIT ?", (scene, since, encode(list(channels)), limit))] + def judged_lines(self, scene: str, reactions: Sequence[str], since: float, limit: int) -> list[dict]: + """The newest judged expressions with one of ``reactions``: first part's seq, reaction and reason.""" + return [{"seq": json.loads(row[0])[0], "reaction": row[1], "reason": row[2]} for row in self.db.execute( + "SELECT message_seqs,reaction,reason FROM reply_effects WHERE scene=? AND first_sent_at>=? " + "AND reaction IN (SELECT value FROM json_each(?)) ORDER BY id DESC LIMIT ?", + (scene, since, encode(list(reactions)), limit))] + def effect(self, scene: str, id: int) -> dict | None: row = self.db.execute( f"SELECT e.*,{STATE_SQL} AS state FROM reply_effects e LEFT JOIN reply_effect_calls c ON c.id=e.call_id " diff --git a/src/len_bot/next/models/request.py b/src/len_bot/next/models/request.py index b003f7dc8..4d292f5b8 100644 --- a/src/len_bot/next/models/request.py +++ b/src/len_bot/next/models/request.py @@ -94,8 +94,10 @@ async def request_model(config: SharedConfig, store: Store, model: ChatModel, raise ContextBudgetError( f"{role} {scope}含预留输出估算 {estimated} token,超过配置窗口 {binding.context_window_tokens},请调大窗口或调小输出上限") effective = model.settings - if quick and getattr(binding, "quick_reasoning_effort", None) is not None: - effective = effective.with_effort(binding.quick_reasoning_effort) + effort = (config.recap_reasoning_effort if role == "recap" + else getattr(binding, "quick_reasoning_effort", None) if quick else None) + if effort is not None: + effective = effective.with_effort(effort) settings = {**effective.model_dump(exclude={"api_key"}), "max_output_tokens": tokens} async with (slots.slot(direct=direct, scene=scene) if slots is not None else nullcontext()): request = {"settings": settings, "messages": messages, "tools": tools, "provider": binding.provider, diff --git a/src/len_bot/next/runtime/model_alerts.py b/src/len_bot/next/runtime/model_alerts.py new file mode 100644 index 000000000..6c4fdefe4 --- /dev/null +++ b/src/len_bot/next/runtime/model_alerts.py @@ -0,0 +1,130 @@ +"""Telling the owners once when chat turns keep failing at the model provider, and once when they recover. + +The host does not switch models or retry for the owner; it only says that the bot is away and why. +""" + +from __future__ import annotations + +import asyncio +import logging +from dataclasses import dataclass +from datetime import datetime +from typing import TYPE_CHECKING +from uuid import uuid4 +from zoneinfo import ZoneInfo + +from ...plugin import Text +from ..platform.messages import ChatMessage, Segment, Sender +from .logs import log_event + +if TYPE_CHECKING: + from .network import NetworkRuntime + +logger = logging.getLogger(__name__) + +# The sender name of the host's own lines in an owner's private chat. +ALERT_SOURCE = "模型提醒" + + +@dataclass +class FailureRun: + """Consecutive failed chat turns on one mind binding, across every scene.""" + binding: tuple[str, str] + first_at: float + turns: int + kind: str + error: str + scenes: set[str] + alerted: bool = False + + +class ModelAlerts: + def __init__(self, runtime: NetworkRuntime): + self.runtime = runtime + self.run: FailureRun | None = None + self.background: set[asyncio.Task] = set() + + def binding(self) -> tuple[str, str]: + mind = self.runtime.config.models.roles.mind + return mind.provider, mind.model + + def observe(self, scene: str, result: dict) -> bool: + """One finished chat turn: a provider failure extends the run, a turn the model answered ends it. + + Returns whether this turn ended a run of failures. + """ + failure, replies = result.get("model_failure"), result.get("model_replies", 0) + binding = self.binding() + if failure is not None: + run = self.run + if run is None or (run.binding != binding and not run.alerted): + run = self.run = FailureRun(binding, failure["at"], 0, failure["kind"], failure["error"], set()) + run.turns += 1 + run.kind, run.error = failure["kind"], failure["error"] + run.scenes.add(scene) + if not run.alerted and run.turns >= self.runtime.config.model_alert_failed_turns and run.binding == binding: + run.alerted = True + self._deliver("failed", self.failure_text(run)) + elif replies and self.run is not None: + run, self.run = self.run, None + if run.alerted: + self._deliver("recovered", self.recovery_text(run)) + return True + return False + + def _time(self, at: float) -> str: + return datetime.fromtimestamp(at, ZoneInfo(self.runtime.config.timezone)).isoformat(timespec="seconds") + + def failure_text(self, run: FailureRun) -> str: + provider, model = run.binding + return (f"[宿主模型提醒] 聊天模型 {provider}/{model} 已连续 {run.turns} 个回合失败," + f"错误类别:{run.kind},最早失败时间:{self._time(run.first_at)},涉及 {len(run.scenes)} 个场景。" + f"最近一次错误:{run.error[:300]}\n" + "这段时间 Bot 不在场;宿主不会自动切换模型,恢复后会再提醒一次。") + + def recovery_text(self, run: FailureRun) -> str: + provider, model = run.binding + return (f"[宿主模型提醒] 聊天模型 {provider}/{model} 已恢复({self._time(self.runtime.store.now())})。" + f"自 {self._time(run.first_at)} 起共 {run.turns} 个回合失败,错误类别:{run.kind}。" + "恢复后只接最近的群聊,不逐条补回旧消息。") + + def targets(self) -> list[str]: + """Each owner's private chat on a configured platform.""" + platforms = set(self.runtime.config.platform_names) + return [f"{owner.split(':', 1)[0]}:private:{owner.split(':', 1)[1]}" for owner in self.runtime.config.owners + if owner.split(":", 1)[0] in platforms] + + def _deliver(self, kind: str, text: str) -> None: + targets = self.targets() + if self.runtime.config.delivery != "live" or not targets: + log_event(logger, "model_alert", level=logging.WARNING, kind=kind, status="not_sent", + reason="未开启真实发送或没有可私聊的主人", text=text) + return + for scene in targets: + task = asyncio.create_task(self._send(kind, scene, text)) + self.background.add(task) + task.add_done_callback(self.background.discard) + + async def _send(self, kind: str, scene: str, text: str) -> None: + try: + async with asyncio.timeout(30): + chat = self.runtime.chats.get(scene) + if chat is not None: + # An owner's private chat already open here records the line like a host notice. + sent = await chat.expression.send_plugin_content(ALERT_SOURCE, [Text(text)], reply_to=None, + report_to_mind=False) + status, error = sent.status, None if sent.status in {"sent", "simulated"} else sent.report + else: + result = await self.runtime.send_message(self.message(scene, text)) + status, error = result.status, result.error + except Exception as failure: + status, error = "failed", f"{type(failure).__name__}: {failure}" + log_event(logger, "model_alert", level=logging.WARNING if error else logging.INFO, + kind=kind, scene=scene, status=status, error=error) + + def message(self, scene: str, text: str) -> ChatMessage: + bot = self.runtime.config.bot_for(scene) + return ChatMessage(id=str(uuid4()), platform=scene.split(":", 1)[0], bot_id=bot, scene=scene, + platform_message_id=None, sender=Sender(bot, None, None, None), + time=self.runtime.store.now(), segments=[Segment("text", {"text": text})], + reply_to=None, mentions_bot=False, is_self=True, send_status="simulated") diff --git a/src/len_bot/next/runtime/network.py b/src/len_bot/next/runtime/network.py index b4983d9a0..d565fae10 100644 --- a/src/len_bot/next/runtime/network.py +++ b/src/len_bot/next/runtime/network.py @@ -23,6 +23,7 @@ from ..config import LabConfig, SharedConfig from .logs import credentials, log_context, log_event from .lifecycle import HostLifecycle +from .model_alerts import ModelAlerts from ..platform.messages import ChatMessage, Notice, SendResult, UploadResult from ..models.client import ChatModel from ..models.slots import ModelSlots @@ -119,6 +120,7 @@ def __init__(self, config: SharedConfig, scene_configs: list[tuple[LabConfig, Pe self.background: set[asyncio.Task] = set() self.personas: PersonaService | None = None self.scene_titles = SceneTitles(self) + self.model_alerts = ModelAlerts(self) self.reference = open_library(config._instance_root, memory, store, slots) self.accepting = not config.platform_names self.storage_error: sqlite3.Error | None = None @@ -285,6 +287,9 @@ def _emit(self, result: dict, *, error: BaseException | None = None) -> None: failed = error if error is not None else result.get('error') log_event(logger, result['type'], level=logging.ERROR if failed else logging.INFO, error=failed, **fields) self.events.publish(result['type'], **fields, error=None if failed is None else str(failed)) + if result['type'] == 'turn' and self.model_alerts.observe(result['scene'], result) and self.upkeep is not None: + # Upkeep that ran out of automatic retries during the outage starts over instead of waiting for the panel. + self.upkeep.model_recovered() if result['type'] == 'turn' and self.upkeep is not None: # Upkeep skips a scene while it is in a turn; the turn's end lets it look again. self.upkeep.wake(result['scene']) diff --git a/src/len_bot/next/runtime/settings.py b/src/len_bot/next/runtime/settings.py index 710bd9319..19e3e1c65 100644 --- a/src/len_bot/next/runtime/settings.py +++ b/src/len_bot/next/runtime/settings.py @@ -92,7 +92,7 @@ async def apply(self, saved: HostConfig) -> None: await self.apply_connection(saved) for name in ('owners', 'owner_private_chat', 'permissions', 'timezone', 'max_steps', 'turn_timeout_seconds', 'text_delivery', 'compaction', 'images', 'audio', 'commands', 'network', 'web_read', 'web_search', - 'task_archive', 'plugin_catalog'): + 'task_archive', 'plugin_catalog', 'model_alert_failed_turns'): setattr(running, name, deepcopy(getattr(saved, name))) self.apply_logging(running, saved) runtime.log_secrets = credentials(saved) diff --git a/src/len_bot/prompts/next_attention.md b/src/len_bot/prompts/next_attention.md index 71ae1e766..b4ce47cf9 100644 --- a/src/len_bot/prompts/next_attention.md +++ b/src/len_bot/prompts/next_attention.md @@ -1,5 +1,6 @@ 你回复谁以后,只会在一小段时间内留意这个人的下一句;别人的消息要 @ 你、回复你或叫到你的名字才叫得醒你,过了这段时间,再有相关消息也叫不醒你。对方说等下发、晚点补、回头再说,或者事情还要等某个人回来才有结果时,在回复的同一轮用 attention 的 intend 记下要等什么;之后相关的消息来了会叫醒你,中间隔着别的话题也一样。对方马上就补充用 wait,答应到点去做的事用 schedule。 +你自己还想接着跟的人或话题,比如刚聊开、你还有话想说的事,也可以用 intend 记下,minutes 按这件事大概还会聊多久给;没有记下什么时,只是隔一阵看一眼群。同一项刚叫醒过你,之后两分钟里再命中的消息会先攒着,两分钟一到一起叫醒你。 - when 写能观察到的条件:等某个人发言用 person,等对某条消息的回复用 thread,等某个话题再出现用 topic 加关键词,到时候主动看一眼用 at。minutes 按对方说的时间留足余量;reason 写清楚在等什么。 - 因为关注事项被叫醒时,状态里会列出是哪一项、哪条消息匹配。不是你在等的内容就 keep,不必回复;等到了就接着处理,处理完 resolve。对方说不用了或事情已经结束时 drop。 - 关注事项到有效期自动结束,生效中的各项在 attention 的返回里。 diff --git a/src/len_bot/prompts/next_group_voice.md b/src/len_bot/prompts/next_group_voice.md new file mode 100644 index 000000000..99dc8825e --- /dev/null +++ b/src/len_bot/prompts/next_group_voice.md @@ -0,0 +1,4 @@ +<本群发言形态> +群友在这里最近 $count 条消息:$figures。 +这是这个群平常说话的样子,供你掂量这一句的长短和要不要分几条发;角色材料对说话方式另有写法时,按角色材料。 + diff --git a/src/len_bot/prompts/next_persona_avoid.md b/src/len_bot/prompts/next_persona_avoid.md new file mode 100644 index 000000000..c7fb07af6 --- /dev/null +++ b/src/len_bot/prompts/next_persona_avoid.md @@ -0,0 +1,4 @@ +<角色材料里带否定的原句> +这几句是角色材料的原文,开口前按原句的意思再对一遍: +$items + diff --git a/src/len_bot/prompts/next_recent_habits.md b/src/len_bot/prompts/next_recent_habits.md index c0b3b9a9c..4f514e6a3 100644 --- a/src/len_bot/prompts/next_recent_habits.md +++ b/src/len_bot/prompts/next_recent_habits.md @@ -1,3 +1,3 @@ <最近的说法> -你在这里最近 $count 次发言里,$habits。这是你前面几条自己带出来的,下一条换个说法或换张图;角色材料里本来就有的口头禅,照材料写的场合用。 +你在这里最近 $count 次发言里,$habits。这是你前面几条自己带出来的,下一条换个说法、换种句式或换张图;角色材料里本来就有的口头禅,照材料写的场合用。 diff --git a/src/len_bot/prompts/next_reply_feedback.md b/src/len_bot/prompts/next_reply_feedback.md new file mode 100644 index 000000000..473ebabf3 --- /dev/null +++ b/src/len_bot/prompts/next_reply_feedback.md @@ -0,0 +1,7 @@ +<群友对你最近几句话的反应> +这是你在这里最近几句话之后群友的实际反应,按群友原话判定,只是记录;这一句怎么说,还是看当下的话题和角色材料。 +有人接: +$taken +没接上: +$not_taken + diff --git a/src/len_bot/prompts/next_topic_freshness.md b/src/len_bot/prompts/next_topic_freshness.md new file mode 100644 index 000000000..33988b924 --- /dev/null +++ b/src/len_bot/prompts/next_topic_freshness.md @@ -0,0 +1,4 @@ +<话题新鲜度> +$facts。 +隔得久、后面已经接着聊了别的,就不回头去接那条;你判断确实值得回头再说时可以接,接的时候让人看得出接的是哪件事。 + diff --git a/src/len_bot/web/frontend/src/views/groups/live/VerdictMark.vue b/src/len_bot/web/frontend/src/views/groups/live/VerdictMark.vue index 04d24a5d0..80ff042d4 100644 --- a/src/len_bot/web/frontend/src/views/groups/live/VerdictMark.vue +++ b/src/len_bot/web/frontend/src/views/groups/live/VerdictMark.vue @@ -53,7 +53,7 @@ const parts = computed(() => { -