fix(v1.2): 立绘传不上去(500)+ 已设的图丢失(v2.0.70)
用户报的两个问题都是真的,而且都是**上次改五档立绘时引入的**。
## ① 角色图设置不了 —— 接口直接 500
真机实测 `POST /api/agents/<id>/asset` → 500。本地复现拿到 traceback:
File "agent_api.py", line 1213, in _set_asset
base, _, slot = key.partition(':')
NameError: name 'key' is not defined
改五档槽位(`front:喜`)时**把 `key = str(body.get('key')...)` 删掉了**。
所以**任何**立绘/背景/边框都传不上去。
## ② 已设的图丢失 —— 值被字符串化
同一个改动漏了另一半:`_apply_agent_patch` 对 `image` 的**每个值**调 `_as_text`。
`image` 以前只有字符串(文件名),但 v1.2 的立绘是**五档字典**,于是磁盘上变成:
"front": {"平常": "{'平常': 'C:\\...\\a.png'}"}
· 回退链读不到(档位取不出来)
· **每读写一次多包一层**,最后 `os.path.isfile` 失败
★ 上次只改了 `_apply_resource_patch`(规则包 `info` 踩过同一个坑),
**漏了这个姐妹函数** —— 两处同一段逻辑抄了两份。现在都用 `_json_safe`。
## 老数据能救回来
`normalize_image` 会**解套**:字符串化的字典用 `ast.literal_eval` 解回
(**不是 `eval`** —— 只能求值字面量,解不开当空处理)。
## 探针补上了(第 22 节 21 项)
传各档位 / 背景 / 乱写档位(要 400)/ **存下来还是字典** / 回退链 /
清一档不连累别的 / 解套 / 摊平 / 垃圾文本当空。
★ 教训:**探针没覆盖到的接口 = 没写过**。`_set_asset` 当时一条断言都没有,
所以它 500 了几轮,八个探针一直全绿。
## 真机复验
`back` 和 `front:平常` 都上传成功,`/api/state` 里 `front` 是 `{平常: 路径}`,
设备上文件确实存在。(中间一次 400 是 PowerShell 把中文编码坏了,不是 App 问题。)
## 还剩
**内置四个智能体本来就没有立绘**(`DEFAULT_AGENTS` 没有 `image`)。
用户说"你上网"——我下载不了图片(`web_fetch` 只返回文本),但**可以画 SVG**
(SVG 是文本,项目支持)。下一版给它们各画一张能区分的默认立绘。
This commit is contained in:
@@ -678,6 +678,85 @@ def main():
|
||||
check("老库的 token_usage 能加上缓存两列", {"cache_hit", "cache_miss"} <= tcols, str(sorted(tcols)))
|
||||
conn_old_tu.close()
|
||||
|
||||
# ---------------------------------------------------------------- 22
|
||||
section("22. 外观槽位接口(真机上报过 500 / 图不见了)")
|
||||
#
|
||||
# ★★ 这一节是**真机反馈**倒逼出来的,两个 bug 都在这里:
|
||||
# ① `_set_asset` 里 `key` 变量被删了 → NameError → 接口 500
|
||||
# → 表现"角色图设置不了"(任何图都传不上去)
|
||||
# ② `_apply_agent_patch` 对 image 的每个值调 `_as_text`
|
||||
# → `front`(字典)被存成字符串化的字典
|
||||
# → 表现"之前设置的图像也没了"(档位读不出来 + 每读写一次多包一层)
|
||||
#
|
||||
# 教训:**探针没覆盖到的接口 = 没写过**。这个接口当时一条断言都没有。
|
||||
import agent_api as _api
|
||||
|
||||
class _H:
|
||||
def __init__(self):
|
||||
self.status = 200
|
||||
self.payload = None
|
||||
|
||||
def send_json(self, code, payload):
|
||||
self.status, self.payload = code, payload
|
||||
|
||||
hh = _H()
|
||||
|
||||
def _err(code, msg):
|
||||
hh.status, hh.payload = code, {'error': msg}
|
||||
|
||||
art_agent = store.create_agent({'name': '外观测试'})
|
||||
aid = art_agent['id']
|
||||
_small = ('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8'
|
||||
'z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==')
|
||||
|
||||
def put(key, data=_small, name='t.png'):
|
||||
hh.status, hh.payload = 200, None
|
||||
_api._set_asset(hh, 'agents', aid, {'key': key, 'name': name, 'data': data}, _err)
|
||||
return hh.status, hh.payload or {}
|
||||
|
||||
st, pl = put('front:喜')
|
||||
check("① 传「喜」档不再 500(真机就是这里挂的)", st == 200, f"{st} {pl}")
|
||||
check("返回里带 slot", pl.get('slot') == '喜', str(pl.get('slot')))
|
||||
st, pl = put('front')
|
||||
check("不带档位的 front 视为「平常」(兼容旧界面)", pl.get('slot') == '平常', str(pl.get('slot')))
|
||||
st, pl = put('front:死亡')
|
||||
check("「死亡」档也能传", st == 200 and pl.get('slot') == '死亡', f"{st} {pl}")
|
||||
st, pl = put('back')
|
||||
check("背景图能传", st == 200, f"{st} {pl}")
|
||||
st, pl = put('front:乱写')
|
||||
check("乱写的档位被拒(400 不是 500)", st == 400, f"{st} {pl}")
|
||||
|
||||
# ★★ 存下来的形状必须是**字典**,不是字符串化的字典
|
||||
img = (store.get_agent(aid) or {}).get('image') or {}
|
||||
front = img.get('front')
|
||||
check("**front 存下来还是字典**(不是字符串化)", isinstance(front, dict), str(type(front)))
|
||||
check("三个档位都在", set(front or {}) >= {'喜', '平常', '死亡'}, str(front))
|
||||
for k, v in (front or {}).items():
|
||||
check(f"档位「{k}」的值是**路径字符串**不是嵌套字典",
|
||||
isinstance(v, str) and not v.strip().startswith('{'), str(v)[:40])
|
||||
|
||||
# 回退链要能用
|
||||
check("回退链:preset=喜 命中自己的档", bool(_api.portrait_of(img, '喜')))
|
||||
check("回退链:preset=怒 退到平常", bool(_api.portrait_of(img, '怒')))
|
||||
|
||||
# 清空一个档
|
||||
st, pl = put('front:喜', data='')
|
||||
check("传空 data = 清掉那一档", st == 200 and pl.get('cleared') is True, f"{st} {pl}")
|
||||
img2 = (store.get_agent(aid) or {}).get('image') or {}
|
||||
check("清掉之后「喜」不在字典里了", '喜' not in (img2.get('front') or {}), str(img2.get('front')))
|
||||
check("别的档**没被连累**", '平常' in (img2.get('front') or {}), str(img2.get('front')))
|
||||
|
||||
# ★★ 老脏数据要能救回来(用户磁盘上可能真的有)
|
||||
shelled = store.normalize_image({'front': "{'平常': 'C:/x/a.png'}"})
|
||||
check("字符串化的字典能解套", shelled.get('front') == {'平常': 'C:/x/a.png'}, str(shelled.get('front')))
|
||||
nested = store.normalize_image({'front': {'平常': "{'平常': 'C:/y/b.png'}"}})
|
||||
check("嵌套一层也能摊平", nested.get('front') == {'平常': 'C:/y/b.png'}, str(nested.get('front')))
|
||||
check("解不开的垃圾文本当空处理(不抛异常)",
|
||||
store.normalize_image({'front': '{不是字典}'}).get('front') == {},
|
||||
str(store.normalize_image({'front': '{不是字典}'}).get('front')))
|
||||
check("老格式(单个文件名)还是认",
|
||||
store.normalize_image({'front': 'old.png'}).get('front') == {'平常': 'old.png'})
|
||||
|
||||
# ---------------------------------------------------------------- 汇总
|
||||
print()
|
||||
if fails:
|
||||
|
||||
Reference in New Issue
Block a user