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:
@@ -30,8 +30,8 @@ android {
|
||||
* versionCode 每次发版 +1(Android 靠它判断"能不能覆盖安装",
|
||||
* 改小了会装不上)。
|
||||
*/
|
||||
versionCode = 255
|
||||
versionName = "2.0.55"
|
||||
versionCode = 256
|
||||
versionName = "2.0.56"
|
||||
|
||||
ndk {
|
||||
// Chaquopy 只支持这两个;Termux bootstrap 也只准备了这两份
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.doubao.lynxclone.ui
|
||||
package com.doubao.lynxclone.ui
|
||||
|
||||
import android.util.Base64
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
@@ -43,6 +43,7 @@ import androidx.compose.ui.unit.sp
|
||||
import com.doubao.lynxclone.R
|
||||
import com.doubao.lynxclone.data.Agent
|
||||
import com.doubao.lynxclone.data.AgentImage
|
||||
import com.doubao.lynxclone.data.PRESETS
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.foundation.Image
|
||||
|
||||
@@ -65,14 +66,22 @@ import androidx.compose.foundation.Image
|
||||
* ③ **限额 0 就是无限制**,不是"限额为 0"。
|
||||
*/
|
||||
|
||||
/** 外观四件套在界面上的顺序和文案 */
|
||||
/** 除立绘以外的三件套(立绘是**五档**的,单独处理) */
|
||||
private val ASSET_SLOTS = listOf(
|
||||
Triple("front", "角色图", "展示区中间的主体形象;缺了用内置默认人物"),
|
||||
Triple("back", "背景图", "铺在展示区最底层;缺了用内置默认背景"),
|
||||
Triple("border-main", "主体边框", "框住整个展示区;缺了用简单方框"),
|
||||
Triple("border-info", "介绍边框", "围住介绍文本;缺了跟主体边框一个样式"),
|
||||
)
|
||||
|
||||
/** 立绘五档各档的说明 —— 每档都要写清"缺了会退回哪张",不然用户不知道该传几张 */
|
||||
private fun portraitHint(preset: String): String = when (preset) {
|
||||
"喜" -> "高兴时用这张;缺了用「平常」那张"
|
||||
"怒" -> "发怒时用这张;缺了用「平常」那张"
|
||||
"哀" -> "悲伤时用这张;缺了用「平常」那张"
|
||||
"死亡" -> "死亡时用这张(那时不能对话);缺了用「平常」那张"
|
||||
else -> "其他档都缺时兜底用这张;连它也没有就用内置默认人物"
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun AgentEditScreen(
|
||||
vm: AppViewModel,
|
||||
@@ -173,6 +182,35 @@ fun AgentEditScreen(
|
||||
color = doubaoTextSecondary(),
|
||||
)
|
||||
} else {
|
||||
// ---- 立绘:五档(v1.2 需求 2.5 / 2.6)----
|
||||
//
|
||||
// ★ 槽位名带档位(`front:喜`),内核按这个落到不同文件 ——
|
||||
// 五档存成一张的话会互相覆盖。
|
||||
Text("角色立绘(按情绪换)", fontSize = 13.sp, color = doubaoText())
|
||||
Spacer(Modifier.height(2.dp))
|
||||
Hint(
|
||||
"情绪的五档各自一张图,缺哪档就退回「平常」那张。" +
|
||||
"现在显示的是「${draft.image.portrait.ifBlank { "内置默认人物" }}」那张。",
|
||||
)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
for (preset in PRESETS) {
|
||||
AssetRow(
|
||||
title = preset,
|
||||
desc = portraitHint(preset),
|
||||
path = draft.image.slot(preset),
|
||||
onPick = {
|
||||
pendingSlot = "front:$preset"
|
||||
pickImage.launch(arrayOf("image/*"))
|
||||
},
|
||||
onClear = { vm.uploadAgentAsset(draft.id, "front:$preset", "", "") },
|
||||
)
|
||||
Spacer(Modifier.height(6.dp))
|
||||
}
|
||||
Spacer(Modifier.height(6.dp))
|
||||
|
||||
// ---- 其余三件套 ----
|
||||
Text("其他三件套", fontSize = 13.sp, color = doubaoText())
|
||||
Spacer(Modifier.height(6.dp))
|
||||
for ((key, title, desc) in ASSET_SLOTS) {
|
||||
AssetRow(
|
||||
title = title,
|
||||
@@ -387,7 +425,9 @@ fun AgentEditScreen(
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
private fun AgentImage.pathOf(key: String): String = when (key) {
|
||||
"front" -> front
|
||||
// v1.2:立绘是五档表,编辑界面的**预览**用内核算好的那张
|
||||
// (其他三件套还是单张)
|
||||
"front" -> portrait
|
||||
"back" -> back
|
||||
"border-main" -> borderMain
|
||||
else -> borderInfo
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.doubao.lynxclone.ui
|
||||
package com.doubao.lynxclone.ui
|
||||
|
||||
import androidx.compose.animation.core.Animatable
|
||||
import androidx.compose.animation.core.FastOutSlowInEasing
|
||||
@@ -256,7 +256,7 @@ fun AgentSelectScreen(
|
||||
Modifier.fillMaxSize().offset(x = (frontShift.value * 200).dp),
|
||||
) {
|
||||
ArtworkFill(
|
||||
shownAgent.image.front,
|
||||
shownAgent.image.portrait,
|
||||
R.drawable.agent_default_character,
|
||||
alpha = frontAlpha.value,
|
||||
scale = ContentScale.Fit,
|
||||
@@ -467,7 +467,7 @@ private fun AgentList(
|
||||
// ★ alignment = TopCenter:角色图是竖长的,居中裁剪只会
|
||||
// 裁出"腰到大腿",一排缩略图全是裤子
|
||||
AssetImage(
|
||||
path = agent.image.front,
|
||||
path = agent.image.portrait,
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentScale = ContentScale.Crop,
|
||||
alignment = Alignment.TopCenter,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.doubao.lynxclone.ui
|
||||
package com.doubao.lynxclone.ui
|
||||
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
@@ -459,7 +459,7 @@ private fun ChatHeader(vm: AppViewModel, agentName: String, speech: SpeechContro
|
||||
) {
|
||||
// 有自定义角色图就显示它(需求 2.5 的资源回退在内核侧已经落定,
|
||||
// 这里 path 为空就还是原来的图标)
|
||||
val art = vm.activeAgent?.image?.front ?: ""
|
||||
val art = vm.activeAgent?.image?.portrait.orEmpty()
|
||||
if (art.isNotBlank()) {
|
||||
AssetImage(
|
||||
path = art,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.doubao.lynxclone.ui
|
||||
package com.doubao.lynxclone.ui
|
||||
|
||||
import android.util.Base64
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
@@ -265,7 +265,7 @@ fun ResourcesScreen(vm: AppViewModel, onBack: () -> Unit) {
|
||||
.border(0.5.dp, Doubao.Border, RoundedCornerShape(8.dp)),
|
||||
) {
|
||||
AssetImage(
|
||||
path = image.front,
|
||||
path = image.slot("平常"),
|
||||
modifier = Modifier.fillMaxSize().clip(RoundedCornerShape(8.dp)),
|
||||
contentScale = ContentScale.Crop,
|
||||
)
|
||||
@@ -501,7 +501,7 @@ private fun ResourceEditScreen(
|
||||
)
|
||||
for ((key, title, desc) in slots) {
|
||||
val path = when (key) {
|
||||
"front" -> worldImage.front
|
||||
"front" -> worldImage.slot("平常")
|
||||
"back" -> worldImage.back
|
||||
"border-main" -> worldImage.borderMain
|
||||
else -> worldImage.borderInfo
|
||||
|
||||
@@ -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,9 +952,18 @@ 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):
|
||||
@@ -931,15 +971,38 @@ def _set_asset(handler, kind: str, owner_id: str, body: dict, err) -> bool:
|
||||
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 = image.pop(key, '')
|
||||
if old and os.path.isfile(old):
|
||||
old = fronts.pop(slot, '')
|
||||
image['front'] = fronts
|
||||
_drop_file(old)
|
||||
return _send_json_ok(handler, {'item': save(image), 'cleared': True})
|
||||
try:
|
||||
os.remove(old)
|
||||
except OSError:
|
||||
pass
|
||||
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(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 改了情绪,
|
||||
# 用户完全无感知,这个功能就成了一个看不见的字段。
|
||||
|
||||
@@ -556,6 +556,53 @@ def main():
|
||||
and agentdb.emotion_state(did)["text"] == "气消了一半",
|
||||
str(agentdb.emotion_state(did)))
|
||||
|
||||
# ---------------------------------------------------------------- 19
|
||||
section("19. v1.2 立绘表:五档 + 回退链(需求 2.5 / 2.6)")
|
||||
#
|
||||
# 真造几个文件出来测 —— 回退链里有一环是 `os.path.isfile`(用户可能
|
||||
# 手动删过 agent-assets 里的图),不落真文件就测不到那一环。
|
||||
pic_dir = tempfile.mkdtemp(prefix="doubao-pics-")
|
||||
|
||||
def pic(name):
|
||||
p = os.path.join(pic_dir, name)
|
||||
with open(p, "wb") as fh:
|
||||
fh.write(b"\x89PNG\r\n\x1a\n" + name.encode())
|
||||
return p
|
||||
|
||||
normal, happy, sad, gone = pic("normal.png"), pic("happy.png"), pic("sad.png"), pic("deleted.png")
|
||||
os.remove(gone) # 造一个"文件被删了"的档位
|
||||
|
||||
from agent_api import portrait_of
|
||||
|
||||
# ① 旧格式(单文件名)经 normalize 之后变成只有「平常」档
|
||||
old = store.normalize_image({"front": normal})
|
||||
check("旧格式归一化成「平常」档", old["front"] == {"平常": normal}, str(old["front"]))
|
||||
check("旧格式的立绘任何 preset 都能用(回退到平常)",
|
||||
portrait_of(old, "怒") == normal, portrait_of(old, "怒"))
|
||||
|
||||
# ② 五档表:命中当前档
|
||||
five = store.normalize_image({"front": {"喜": happy, "哀": sad, "平常": normal}})
|
||||
check("命中当前档", portrait_of(five, "喜") == happy)
|
||||
check("没那一档时回退到「平常」", portrait_of(five, "怒") == normal)
|
||||
# ③ 没有平常档 → 拿字典里任何一个还能用的(用户可能只传了一档)
|
||||
only_happy = store.normalize_image({"front": {"喜": happy}})
|
||||
check("连平常档都没有时,用字典里任何一个能用的",
|
||||
portrait_of(only_happy, "死亡") == happy, portrait_of(only_happy, "死亡"))
|
||||
# ④ **文件被删了**的那一档不许返回不存在的路径(那会让界面显示空白)
|
||||
broken = store.normalize_image({"front": {"喜": gone, "平常": normal}})
|
||||
check("档位指向的文件不存在时,跳过它去回退",
|
||||
portrait_of(broken, "喜") == normal, portrait_of(broken, "喜"))
|
||||
# ⑤ 完全没图 → 空串(界面据此用内置默认人物)
|
||||
check("没有立绘时返回空串(界面用内置默认人物)",
|
||||
portrait_of(store.normalize_image({}), "平常") == "")
|
||||
check("全是坏路径时也返回空串",
|
||||
portrait_of(store.normalize_image({"front": {"喜": gone}}), "喜") == "")
|
||||
# ⑥ 归一化不丢自定义档位(用户可能自己加了需求之外的档)
|
||||
custom = store.normalize_image({"front": {"醉": sad, "平常": normal}})
|
||||
check("归一化保留自定义档位(不丢信息)", custom["front"].get("醉") == sad, str(custom["front"]))
|
||||
check("自定义档位也能被选中", portrait_of(custom, "醉") == sad)
|
||||
shutil.rmtree(pic_dir, ignore_errors=True)
|
||||
|
||||
# ---------------------------------------------------------------- 汇总
|
||||
print()
|
||||
if fails:
|
||||
|
||||
Reference in New Issue
Block a user