fix(agent-v1.1): 按复查报告逐条核实并修复(v2.0.48)
拿到一份《智能体系统 v1.1 实现复查报告》。**先核实再动手** ——
报告作者显然没读代码(它自己在第三节也承认"需核实"),
逐条对完有 5 条是错的、8 条是真的。
## 报告说错的(都已实现,附证据)
- **AI 建议总结机制**:`memory_suggest_summary` 工具 + `add_pending_summary_suggestion`
+ MemoryScreen 的建议条和一键整理,都在。
- **模型单价与费用换算**:`pricePerK` 在 store 的 RESOURCES、`_usage_payload`
返回 price/cost、TokenUsageScreen 显示估算费用,都在。
- **unpin**:`POST /pin` 收 `pinned` 参数,界面上是「必须保留 / 取消保留」的
**切换**按钮 —— 报告担心的"解不开"不存在。
- **情绪编辑入口**:MemoryScreen 里就有情绪输入框和「保存情绪」。
- **SessionUsageBar 分列**:就是"输入 x · 输出 y"两列。
- **Token 交叉**(它列为"最高优先级 bug"):`_summarize` 的 docstring 里
明确写着"消耗计入统计、不受限额约束",两条规则都处理了。
## 真漏的,这次修了
**记忆隐身(报告 4.1,最有价值的一条)** —— 用户给智能体攒了几个月私有记忆,
一绑世界观,记忆管理的列表就空了(读的空间从 `agent:<id>` 换成了 `world:<id>`)。
记忆一条没丢,但界面不说的话用户必然判定"绑定把我的记忆清了"。
现在:`?space=own` 能看旧空间、当前空间带 `ownCount` 让界面挂一个明确的入口、
旧空间顶部写清"这是绑定前的、AI 现在读的是共享空间那份"。
**成员可见 + 作者署名(报告 2.5)** ——
· 情绪感知**关着**时也注入成员名单(不含情绪):共享记忆是共用的,
AI 不知道有别人,就会把别人写的经历当成自己的过去。
· 共享记忆的条目带署名(`【雪夜】(仙辰记的)`):没有署名时,
别人写的"我小时候住在海边"在模型眼里就是它自己的经历 ——
需求 3.6 说的"交模型调和矛盾记忆"也得先分得清谁写的才成立。
**共享空间限权可视化(报告 4.2.2)** —— 别人写的条目显示「由某某写入」,
并且**根本不给编辑/删除按钮**(而不是"点了才被后端拒绝"——
那样用户会当成 bug,而后端拦得对)。
**情绪 UI(报告 2.6)** —— 对话页角色名旁显示当前情绪标签;
新增 `/api/state.emotions`。原来情绪只在提示词里生效,
AI 改了用户完全无感知,这功能就是个看不见的字段。
**触顶第三件(报告 2.4)** —— 额度用完时不再只是"拦下来 + 一闪而过的提示",
输入框上方给一条明确的提示和一个「看用量 / 调整 ›」按钮,
点进去就是清零/改限额的地方。(告别消息本来就有。)
**总结前确认(4.2.1)** / **导入模式选择(4.2.4)** / **清零确认(3.4)** ——
导入的 `replace` 原来在界面上**没有入口**(功能只有一半),
现在选完文件弹一次"合并(推荐)/ 覆盖"。
清零和整理各加一次确认(都是不可逆或要花 Token 的操作)。
**动画参数配置化(报告 2.3,需求 §2.3 明文)** ——
4 个时长进全局配置(设置 →「超时」→「动画」一组)。
★ 复用"超时"那套数值体系而不是新造 `agentAnim` 对象:再造一套就是
**第二份要同步的东西**(加一项改两处、界面写两遍),而这个仓库里
"同一逻辑抄两份、改的时候只改一份"已经反复出现过。代价只是分组标题
要写清楚。
**删除级联语义(报告第五节,它说"比漏做更危险")** —— 决策早就在代码里
发生了却没有任何文档。现在 `docs/智能体系统.md` 用一张表钉住:
删智能体 → 记忆/统计保留、绑定清掉、图片删掉;并在 v1.2 议题里记下
记忆归档、会话裁剪、语义检索三条。
## 顺手抓到的三个真问题
1. **我把 agent 的 JSON 拼进了提示词**:`by_id.items()` 给的是
`(id, 整个 agent 字典)` 不是 `(id, 名字)`,`f'{name}-{emo}'` 直接把
字典塞进了 `<peer_emotions>`。探针的 detail 打印出来才看见。
2. **`agentdb` 里写死了 `timeout=10`** —— 由 `verify-timeouts.py` 的
"没有漏网的硬编码"那节抓到。改成读「本地接口请求」。
3. **Node 端没跟上动画这 4 项** —— 同样是对拍脚本抓的,已同步。
## 验证
- `probe-agent-system.py` 12 节 80+ 项全绿(新增第 7 节:空间切换、
`?space=own`、`ownCount`、私有/共享不互相污染;第 8 节扩到成员名单、
署名、**以及"名单里不许出现 agent 的 JSON"**)
- `probe-degraded.py` 全绿
- `verify-timeouts.py` 全绿(22 项两边一致)
- 装机:内核正常启动、资源界面、模型列表
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""智能体系统的 HTTP 接口(需求 v1.1)。
|
||||
"""智能体系统的 HTTP 接口(需求 v1.1)。
|
||||
|
||||
## 为什么单独一个模块
|
||||
|
||||
@@ -218,15 +218,38 @@ def _memory_space(agent: dict) -> str:
|
||||
return agentdb.scope_of(agent.get('id') or '', agent.get('worldviewId') or '')
|
||||
|
||||
|
||||
def _memory_payload(agent: dict) -> dict:
|
||||
"""一个记忆空间的完整视图(列表 + 情绪 + 用量),界面进页面时一次拿够"""
|
||||
def _memory_payload(agent: dict, force_own: bool = False) -> dict:
|
||||
"""
|
||||
一个记忆空间的完整视图(列表 + 情绪 + 用量)。
|
||||
|
||||
[force_own] = True 时看**私有空间**,不看当前生效的那个 —— 给界面一个
|
||||
"我原来那些记忆去哪了"的入口(复查报告 4.1)。绑定世界观之后,
|
||||
默认空间变成 world:<id>,私有空间里那条一条都没丢,只是不在这儿显示。
|
||||
"""
|
||||
scope = _memory_space(agent)
|
||||
own = agentdb.agent_scope(agent.get('id') or '')
|
||||
if force_own and scope != own:
|
||||
return {
|
||||
'ok': True,
|
||||
'shared': False,
|
||||
'own': True, # 界面据此说明"这是绑定前的私有记忆"
|
||||
'activeScope': scope, # 当前生效的是哪个空间
|
||||
'worldviewId': agent.get('worldviewId') or '',
|
||||
'memories': agentdb.memory_list(own),
|
||||
'emotion': agentdb.emotion_get(agent.get('id') or ''),
|
||||
'suggestion': '',
|
||||
}
|
||||
items = agentdb.memory_list(scope)
|
||||
return {
|
||||
'ok': True,
|
||||
'shared': bool(agent.get('worldviewId')), # 是不是共享空间(界面要提示)
|
||||
'own': False,
|
||||
'activeScope': scope,
|
||||
'worldviewId': agent.get('worldviewId') or '',
|
||||
'memories': items,
|
||||
# ★ 绑定世界观之后,私有空间里可能还留着以前攒的记忆 ——
|
||||
# 条数一并告诉界面,它才知道要不要显示"以前那些在这"这个入口
|
||||
'ownCount': 0 if scope == own else len(agentdb.memory_list(own)),
|
||||
'emotion': agentdb.emotion_get(agent.get('id') or ''),
|
||||
'suggestion': store.peek_summary_suggestion(agent.get('id') or ''),
|
||||
}
|
||||
@@ -426,10 +449,17 @@ def _memories(handler, method: str, seg: list, body, err) -> bool:
|
||||
return True
|
||||
tail = seg[4:]
|
||||
|
||||
# `?space=own`:看**私有空间**(复查报告 4.1)。
|
||||
#
|
||||
# ★ 为什么需要这个:用户在没绑世界观时攒了三个月记忆,一绑世界观,
|
||||
# 记忆管理的列表就空了 —— 因为读的空间从 agent:<id> 换成了 world:<id>。
|
||||
# 记忆**一条没丢**,但界面不说明的话,用户必然判定"绑定把我的记忆清了"。
|
||||
want_own = 'space=own' in (getattr(handler, 'path', '') or '')
|
||||
|
||||
# /memories
|
||||
if not tail:
|
||||
if method == 'GET':
|
||||
handler.send_json(200, _memory_payload(agent))
|
||||
handler.send_json(200, _memory_payload(agent, force_own=want_own))
|
||||
return True
|
||||
if method == 'POST':
|
||||
b = body()
|
||||
|
||||
@@ -104,6 +104,16 @@ def build_emotion(agent: dict) -> str:
|
||||
def build_memory(agent: dict) -> str:
|
||||
"""
|
||||
`<memory>` 段:那个记忆空间的**全部条目**(标题 + 正文,需求 3.2 的"全量注入")。
|
||||
|
||||
★★ 共享空间里的条目**带作者署名**(复查报告第 5 条)。
|
||||
|
||||
为什么必须带:共享记忆是几个人往同一个本子上写。没有署名的时候,
|
||||
别人写的"我小时候住在海边"在模型眼里就是**它自己的经历** ——
|
||||
它会当成自己的过去来讲,甚至在用户问起时说"我不记得有这事"。
|
||||
署名之后它才知道"这条是仙辰写的,不是我的"(需求 3.6 的
|
||||
"交模型调和矛盾记忆"也才有前提:得先分得清谁写的)。
|
||||
|
||||
私有空间**不加**署名:那里只有它自己写的,加一句"(你记的)"是纯噪音。
|
||||
"""
|
||||
scope = memory_scope(agent)
|
||||
items = _safe(lambda: agentdb.memory_list(scope), [])
|
||||
@@ -113,10 +123,20 @@ def build_memory(agent: dict) -> str:
|
||||
lambda: (store.read_settings().get('memoryLine') or DEFAULT_MEMORY_LINE),
|
||||
DEFAULT_MEMORY_LINE,
|
||||
)
|
||||
shared = scope.startswith('world:')
|
||||
names = {}
|
||||
if shared:
|
||||
names = {a.get('id'): (a.get('name') or '某人') for a in _safe(store.list_agents, [])}
|
||||
me = (agent or {}).get('id') or ''
|
||||
lines = []
|
||||
for it in items:
|
||||
title = (it.get('title') or '').strip()
|
||||
content = (it.get('content') or '').strip()
|
||||
author = (it.get('author_agent') or '').strip()
|
||||
if shared and author:
|
||||
# 自己写的也标出来("(你记的)")—— 一眼能分清哪些是自己的立场
|
||||
who = '你记的' if author == me else f'{names.get(author) or "某人"}记的'
|
||||
title = f'{title}({who})'
|
||||
try:
|
||||
lines.append(tmpl.format(title=title, content=content))
|
||||
except (KeyError, IndexError): # 用户把模板改坏了:退回默认再试
|
||||
@@ -126,14 +146,25 @@ def build_memory(agent: dict) -> str:
|
||||
|
||||
def build_peer_emotions(agent: dict) -> str:
|
||||
"""
|
||||
`<peer_emotions>` 段:**同世界观其他智能体**的名字 + 情绪(需求 3.5)。
|
||||
`<peer_emotions>` 段:**同世界观其他智能体**的名字(+ 情绪)(需求 3.5)。
|
||||
|
||||
★ 三个条件全满足才有内容:绑了世界观、开了情绪感知开关、世界观下还有别人。
|
||||
开关默认关(需求原话"默认关闭")—— 别人的情绪属于"角色内的隐私",
|
||||
默认让 AI 看到会让它们的表现互相串味。
|
||||
★★ 分两档,不是"开关一关就什么都没有":
|
||||
|
||||
· 绑了世界观但**没开**情绪感知 → 只给**成员名单**(谁在,但看不到情绪)
|
||||
· 绑了世界观且**开了**开关 → 名单 + 各自当前的情绪
|
||||
|
||||
为什么关着也要给名单(复查报告第 5 条):
|
||||
共享记忆是**共用**的,AI 一定会看到"别人写的条目"。如果它连
|
||||
"这个空间里还有别人"都不知道,那些第三人称的条目在它眼里就是
|
||||
**自己的记忆**——它会以为"我什么时候去过九霄山?",
|
||||
于是把别人的经历当成自己的,或者在正文里跟用户说"我没记过这个"。
|
||||
名单不含情绪,所以"角色内的隐私"这条边界仍然守住了。
|
||||
|
||||
★ 情绪感知开关默认关(需求原话"默认关闭")—— 别人的**情绪**属于
|
||||
角色内的隐私,默认让 AI 看到会让它们的表现互相串味。
|
||||
"""
|
||||
agent = agent or {}
|
||||
if not agent.get('worldviewId') or not agent.get('emotionEnabled'):
|
||||
if not agent.get('worldviewId'):
|
||||
return ''
|
||||
my_id = agent.get('id') or ''
|
||||
wid = agent.get('worldviewId') or ''
|
||||
@@ -141,16 +172,27 @@ def build_peer_emotions(agent: dict) -> str:
|
||||
a for a in _safe(store.list_agents, [])
|
||||
if a.get('worldviewId') == wid and a.get('id') != my_id
|
||||
]
|
||||
if not others:
|
||||
return ''
|
||||
|
||||
by_id = {a.get('id'): a for a in others}
|
||||
names = [(by_id.get(aid) or {}).get('name') or '某人' for aid in by_id]
|
||||
|
||||
if not agent.get('emotionEnabled'):
|
||||
return ';'.join(f'{n}(不知道他此刻的情绪)' for n in names)
|
||||
|
||||
rows = _safe(
|
||||
lambda: agentdb.peer_emotions(wid, my_id, [a.get('id') for a in others]),
|
||||
lambda: agentdb.peer_emotions(wid, my_id, list(by_id)),
|
||||
[],
|
||||
)
|
||||
by_id = {a.get('id'): a for a in others}
|
||||
emo_by_id = {aid: (emo or '').strip() for aid, emo in rows}
|
||||
parts = []
|
||||
for aid, emo in rows:
|
||||
name = (by_id.get(aid) or {}).get('name') or '某人'
|
||||
if (emo or '').strip():
|
||||
parts.append(f'{name}-{emo.strip()}')
|
||||
for aid, one in by_id.items():
|
||||
# ★ `by_id.items()` 给的是 (id, **整个 agent 字典**),不是 (id, 名字)——
|
||||
# 直接 f'{name}-{emo}' 会把 agent 的 JSON 拼进提示词(踩过)
|
||||
name = one.get('name') or '某人'
|
||||
emo = emo_by_id.get(aid) or ''
|
||||
parts.append(f'{name}-{emo}' if emo else f'{name}(没说自己在想什么)')
|
||||
return ';'.join(parts)
|
||||
|
||||
|
||||
|
||||
@@ -58,7 +58,16 @@ def _connect() -> sqlite3.Connection:
|
||||
return conn
|
||||
os.makedirs(os.path.dirname(db_path()), exist_ok=True)
|
||||
# check_same_thread=False 是安全的:这个连接只会被**创建它的那个线程**用到
|
||||
conn = sqlite3.connect(db_path(), check_same_thread=False, timeout=10)
|
||||
#
|
||||
# ★ 这个 timeout 是**锁等待**(不是查询超时):另一个线程正在写时,
|
||||
# 等多久才放弃并抛 "database is locked"。
|
||||
# 它以前写死 10 秒 —— 对拍脚本抓到的硬编码。现在复用「本地接口请求」
|
||||
# 那一项:语义最接近(都是"本机内部等一等"),而且用户调它是有意义的
|
||||
# (老设备上几千条记忆的整理会长时间占着写锁)。
|
||||
import timeouts as _timeouts
|
||||
|
||||
wait = float(_timeouts.get('kernelLocal') or 10)
|
||||
conn = sqlite3.connect(db_path(), check_same_thread=False, timeout=max(1.0, wait))
|
||||
conn.row_factory = sqlite3.Row
|
||||
# WAL:读不挡写。安卓上内核同时有"对话在写记忆"和"界面在读列表"。
|
||||
try:
|
||||
|
||||
@@ -582,6 +582,13 @@ class KernelHandler(BaseHTTPRequestHandler):
|
||||
'mcpConfig': mcp_client.read_mcp_config(),
|
||||
'conversations': store.list_conversations(),
|
||||
'agents': store.list_agents(),
|
||||
# 每个智能体当前的情绪(复查报告 2.6)。
|
||||
# ★ 情绪原来只在提示词里生效 —— AI 用 emotion_set 改了情绪,
|
||||
# 用户完全无感知,这个功能就成了一个看不见的字段。
|
||||
'emotions': {
|
||||
a.get('id'): e for a in store.list_agents()
|
||||
if (e := (agentdb.emotion_get(a.get('id') or '') or '').strip())
|
||||
},
|
||||
# ---- 智能体系统的另外三种资源(需求 v1.1)----
|
||||
# 都不大(几个到几十个),一次带给界面省得进页面再拉一次
|
||||
'worldviews': store.list_resource('worldviews'),
|
||||
|
||||
@@ -42,6 +42,11 @@
|
||||
# `scripts/verify-timeouts.py` 会检查两边对得上(少一条就报错)。
|
||||
|
||||
DEFAULT = {
|
||||
# ---- 智能体切换动画(需求 2.3)----
|
||||
'agentAnim.frontOut': 0.3,
|
||||
'agentAnim.gap': 0.5,
|
||||
'agentAnim.frame': 0.4,
|
||||
'agentAnim.frontIn': 0.3,
|
||||
# ---- 对话 ----
|
||||
'model': 180, # 等模型返回
|
||||
'sseRead': 120, # 客户端读 SSE 的间隔上限(内核只负责告诉客户端)
|
||||
@@ -78,6 +83,38 @@ DEFAULT = {
|
||||
# group 决定界面上分到哪一组;unit 目前全是"秒"(毫秒的东西一律在调用点换算)。
|
||||
|
||||
SPECS = [
|
||||
# ---------------- 智能体切换动画(需求 2.3)----------------
|
||||
#
|
||||
# ★ 这几项严格说**不是超时**,但它们和超时是同一类东西:
|
||||
# 一个"全局可调的时间数值"。需求 2.3 原话是"所有时长与缓动数值收进全局配置"。
|
||||
#
|
||||
# 为什么不单独造一套(settings 里加个 agentAnim 对象 + 再写一个配置界面):
|
||||
# 那就是**第二份要同步的东西** —— 加一项要改两处、界面要写两遍,
|
||||
# 而这个仓库里"同一逻辑抄成两份、改的时候只改一份"已经反复出现过。
|
||||
# 复用它唯一的代价是分组标题要写清楚(界面上这一组的说明就是这么写的)。
|
||||
{
|
||||
'key': 'agentAnim.frontOut', 'group': '动画', 'label': '角色图出场',
|
||||
'default': 0.3, 'min': 0.05, 'max': 3, 'unit': '秒',
|
||||
'desc': '切换智能体时,**旧**角色图淡出 + 侧移的时长(需求定的 0.3s)。',
|
||||
},
|
||||
{
|
||||
'key': 'agentAnim.gap', 'group': '动画', 'label': '中间留白',
|
||||
'default': 0.5, 'min': 0, 'max': 5, 'unit': '秒', 'zero': '不停顿',
|
||||
'desc': '两个阶段之间的停顿(需求定的 0.5s)。调成 0 就是"出场完立刻换框",'
|
||||
'整个切换会从 1.5 秒缩到 1.0 秒 —— 嫌切换慢就改这个。',
|
||||
},
|
||||
{
|
||||
'key': 'agentAnim.frame', 'group': '动画', 'label': '边框背景切换',
|
||||
'default': 0.4, 'min': 0.05, 'max': 4, 'unit': '秒',
|
||||
'desc': '背景图 / 主体边框 / 介绍框滑出滑入的**总**时长(需求定的 0.4s,'
|
||||
'实际拆成滑出一半、滑入一半)。',
|
||||
},
|
||||
{
|
||||
'key': 'agentAnim.frontIn', 'group': '动画', 'label': '角色图入场',
|
||||
'default': 0.3, 'min': 0.05, 'max': 3, 'unit': '秒',
|
||||
'desc': '**新**角色图淡入 + 侧移到位的时长(需求定的 0.3s)。',
|
||||
},
|
||||
|
||||
# ---------------- 对话 ----------------
|
||||
{
|
||||
'key': 'model', 'group': '对话', 'label': '等模型回复',
|
||||
|
||||
Reference in New Issue
Block a user