feat(v1.2): 阶段1 —— 立绘表五档 + 回退链 + 编辑界面(v2.0.56)
按 v1.2 需求,`front` 从单文件名扩成五档立绘表,立绘随情绪自动切换。 ## 数据层 `store.normalize_image`:兼容规则是需求原话的"旧格式视为仅有「平常」档"。 ★ 放在**读**的时候归一化(`list_agents` 出口),不是写的时候: 老数据已经在 agents.json 里了,写时归一化管不到它们;读时归一化一次 覆盖新旧两种,而且写入路径一行都不用改。 ★ 归一化不丢信息:字典里多出来的自定义档位原样保留。 ## 回退链 `agent_api.portrait_of`:`当前 preset 档 → 平常档 → 旧单一 front → 空串` (空串 = 界面用内置默认人物)。 ★ 由内核算好、经 `/api/state` 的 `portrait` 字段给界面 —— 回退链是**一条规则**, 而选择界面、对话界面、编辑预览三处都要用。 ★ 查 `os.path.isfile`:用户可能手动删过 agent-assets 里的文件, 指着不存在的路径会让界面显示空白,比回退到默认图糟得多。 ## 落盘与槽位 - `_save_assets`:front 单独处理,每档文件名带档位(`front-喜-happy.png`), 否则五档会互相覆盖;老格式落成「平常」档 - `_set_asset`:槽位名支持 `front:喜`(不带档位视为「平常」,兼容旧调用); 换图时把旧文件删掉,不然越攒越多 ## 界面 - `AgentImage.front` 从 String 改成 `Map<String, String>` + 新增 `portrait` - 所有显示处改用 `portrait`(编译器一次列出 5 个文件 11 处,逐个改的) - 编辑界面外观组拆两段:**角色立绘(五档,每档带"缺了退回哪张"的说明)** + 其他三件套 ## 验证 探针第 19 节 10 项,**真造文件**测(回退链里有一环是 os.path.isfile, 不落真文件测不到):老格式归一化、命中当前档、回退平常、 连平常都没有时用任意可用档、**档位文件被删时跳过它**、完全没图返回空串、 归一化保留自定义档位、自定义档位也能被选中。 装机实测五档编辑器。全部探针 + audit-ui-tree 全绿。 ## 阶段 1 剩余(很小的尾巴) 立绘切换的 crossfade(0.3s)放到阶段 5 的 §4.4 一起做 —— 那时要同时处理"群聊背景跟随发言者",两处共用同一套缓动参数。
This commit is contained in:
@@ -134,10 +134,41 @@ def _save_assets(owner_id: str, files: dict, image_spec: dict) -> dict:
|
||||
|
||||
★ 为什么存路径而不是存"包内文件名":包解完就没了(用户不会留着那个 zip),
|
||||
而界面每次都要显示它 —— 存文件名的话每次都得再去找一遍包。
|
||||
|
||||
★ v1.2:`front` 是**五档立绘表**,所以它单独处理 —— 每一档各自落盘,
|
||||
文件名带上档位(`front-喜-happy.png`),否则五档会互相覆盖。
|
||||
老格式(单个文件名)落成「平常」档,和需求说的兼容规则一致。
|
||||
"""
|
||||
out = {}
|
||||
target_dir = _asset_dir(owner_id)
|
||||
for key in ('front', 'back', 'border-main', 'border-info'):
|
||||
|
||||
front = (image_spec or {}).get('front')
|
||||
fronts = {}
|
||||
if isinstance(front, dict):
|
||||
for preset, fname in front.items():
|
||||
slot = str(preset or '').strip()
|
||||
if not slot:
|
||||
continue
|
||||
hit = _pick_asset(files, fname)
|
||||
if not hit:
|
||||
continue
|
||||
name, data = hit
|
||||
dest = os.path.join(target_dir, f'front-{_safe_segment(slot)}-{_safe_segment(name)}')
|
||||
with open(dest, 'wb') as fh:
|
||||
fh.write(data)
|
||||
fronts[slot] = dest
|
||||
elif front:
|
||||
hit = _pick_asset(files, front)
|
||||
if hit:
|
||||
name, data = hit
|
||||
dest = os.path.join(target_dir, f'front-{_safe_segment(name)}')
|
||||
with open(dest, 'wb') as fh:
|
||||
fh.write(data)
|
||||
fronts['平常'] = dest
|
||||
if fronts:
|
||||
out['front'] = fronts
|
||||
|
||||
for key in ('back', 'border-main', 'border-info'):
|
||||
hit = _pick_asset(files, (image_spec or {}).get(key))
|
||||
if not hit:
|
||||
continue
|
||||
@@ -921,25 +952,57 @@ def _set_asset(handler, kind: str, owner_id: str, body: dict, err) -> bool:
|
||||
err(404, '没有这一项')
|
||||
return True
|
||||
|
||||
key = str(body.get('key') or '').strip()
|
||||
if key not in ASSET_KEYS:
|
||||
err(400, f'不认识的外观槽位:{key}(可选:{"、".join(ASSET_KEYS)})')
|
||||
# ★ v1.2:立绘是**五档**,所以槽位名允许带档位 —— `front:喜`。
|
||||
# 不带档位的 `front` 视为「平常」档(兼容旧界面和旧调用)。
|
||||
base, _, slot = key.partition(':')
|
||||
base = base.strip()
|
||||
slot = slot.strip()
|
||||
if base not in ASSET_KEYS:
|
||||
err(400, f'不认识的外观槽位:{base}(可选:{"、".join(ASSET_KEYS)})')
|
||||
return True
|
||||
if base == 'front':
|
||||
slot = slot or '平常'
|
||||
if slot not in PORTRAIT_PRESETS:
|
||||
err(400, f'立绘档位只能是:{"、".join(PORTRAIT_PRESETS)}')
|
||||
return True
|
||||
|
||||
def save(image: dict):
|
||||
if kind == 'agents':
|
||||
return store.update_agent(owner_id, {'image': image})
|
||||
return store.update_resource('worldviews', owner_id, {'image': image})
|
||||
|
||||
image = dict(owner.get('image') or {})
|
||||
image = store.normalize_image(owner.get('image') or {})
|
||||
raw = body.get('data') or body.get('base64') or ''
|
||||
|
||||
if base == 'front':
|
||||
fronts = dict(image.get('front') or {})
|
||||
if not raw:
|
||||
old = fronts.pop(slot, '')
|
||||
image['front'] = fronts
|
||||
_drop_file(old)
|
||||
return _send_json_ok(handler, {'item': save(image), 'cleared': True})
|
||||
try:
|
||||
blob = base64.b64decode(raw)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
err(400, f'图片数据解不开:{exc}')
|
||||
return True
|
||||
if len(blob) > 20 * 1024 * 1024:
|
||||
err(400, '图片太大了(上限 20MB)')
|
||||
return True
|
||||
name = _safe_segment(str(body.get('name') or f'{slot}.png'))
|
||||
dest = os.path.join(_asset_dir(owner_id), f'front-{_safe_segment(slot)}-{name}')
|
||||
with open(dest, 'wb') as fh:
|
||||
fh.write(blob)
|
||||
_drop_file(fronts.get(slot)) # 换图时把旧的那张删掉,不然越攒越多
|
||||
fronts[slot] = dest
|
||||
image['front'] = fronts
|
||||
return _send_json_ok(handler, {
|
||||
'item': save(image), 'path': dest, 'bytes': len(blob), 'slot': slot,
|
||||
})
|
||||
|
||||
if not raw:
|
||||
old = image.pop(key, '')
|
||||
if old and os.path.isfile(old):
|
||||
try:
|
||||
os.remove(old)
|
||||
except OSError:
|
||||
pass
|
||||
old = image.pop(base, '')
|
||||
_drop_file(old)
|
||||
return _send_json_ok(handler, {'item': save(image), 'cleared': True})
|
||||
|
||||
try:
|
||||
@@ -951,11 +1014,12 @@ def _set_asset(handler, kind: str, owner_id: str, body: dict, err) -> bool:
|
||||
err(400, '图片太大了(上限 20MB)')
|
||||
return True
|
||||
|
||||
name = _safe_segment(str(body.get('name') or f'{key}.png'))
|
||||
dest = os.path.join(_asset_dir(owner_id), f'{key}-{name}')
|
||||
name = _safe_segment(str(body.get('name') or f'{base}.png'))
|
||||
dest = os.path.join(_asset_dir(owner_id), f'{base}-{name}')
|
||||
with open(dest, 'wb') as fh:
|
||||
fh.write(blob)
|
||||
image[key] = dest
|
||||
_drop_file(image.get(base))
|
||||
image[base] = dest
|
||||
return _send_json_ok(handler, {
|
||||
'item': save(image),
|
||||
'path': dest,
|
||||
@@ -963,6 +1027,17 @@ def _set_asset(handler, kind: str, owner_id: str, body: dict, err) -> bool:
|
||||
})
|
||||
|
||||
|
||||
def _drop_file(path) -> None:
|
||||
"""删掉一个不再被引用的资源文件(失败就算了,不值得让整个请求报错)"""
|
||||
p = str(path or '').strip()
|
||||
if not p or not os.path.isfile(p):
|
||||
return
|
||||
try:
|
||||
os.remove(p)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def _import_agent(handler, body, err) -> bool:
|
||||
"""
|
||||
导入智能体包(需求 2.5)。
|
||||
|
||||
@@ -581,7 +581,19 @@ class KernelHandler(BaseHTTPRequestHandler):
|
||||
'mcp': mcp_client.get_mcp_state(),
|
||||
'mcpConfig': mcp_client.read_mcp_config(),
|
||||
'conversations': store.list_conversations(),
|
||||
'agents': store.list_agents(),
|
||||
# ★ 给每个智能体算好"当前该显示哪张立绘"(需求 2.6 的回退链:
|
||||
# 当前 preset 档 → 平常档 → 旧单一 front → 空 = 内置默认人物)。
|
||||
#
|
||||
# 界面(选择界面、对话界面、编辑界面的预览)直接用这个字段,
|
||||
# 不用各自实现一遍回退链 —— 那种"同一逻辑抄三份"的写法,
|
||||
# 改一处必漏两处,而这个仓库里已经反复出现过。
|
||||
'agents': [
|
||||
{**a, 'portrait': agent_api.portrait_of(
|
||||
a.get('image') or {},
|
||||
(agentdb.emotion_state(a.get('id') or '').get('preset') or '平常'),
|
||||
)}
|
||||
for a in store.list_agents()
|
||||
],
|
||||
# 每个智能体当前的情绪(v1.2 双字段)。
|
||||
# ★ 情绪原来只在提示词里生效 —— AI 用 emotion_set 改了情绪,
|
||||
# 用户完全无感知,这个功能就成了一个看不见的字段。
|
||||
|
||||
Reference in New Issue
Block a user