diff --git a/android/kernel/agent_api.py b/android/kernel/agent_api.py index 42084d2..05e5c8e 100644 --- a/android/kernel/agent_api.py +++ b/android/kernel/agent_api.py @@ -1,4 +1,4 @@ -"""智能体系统的 HTTP 接口(需求 v1.1)。 +"""智能体系统的 HTTP 接口(需求 v1.1)。 ## 为什么单独一个模块 @@ -857,6 +857,46 @@ def _export_agent(handler, agent_id: str, err) -> bool: #: 外观四件套的槽位名(和需求 2.5 的 smartInfo.json 一一对应) ASSET_KEYS = ('front', 'back', 'border-main', 'border-info') +#: v1.2 立绘表支持的五档(和情绪 preset 对齐,需求 2.5) +PORTRAIT_PRESETS = ('喜', '怒', '哀', '平常', '死亡') + + +def portrait_of(image: dict, preset: str) -> str: + """ + **当前该显示哪张立绘**(需求 2.6 的回退链): + + 当前 preset 档 → 平常档 → 旧格式的单一 front → (空串) + + 空串的意思是"没有自定义立绘",界面据此用**内置默认人物** + (需求 2.6 的最后一级)。 + + ★ 为什么由内核算好再给界面:回退链是**一条规则**,而选择界面、对话界面、 + 编辑界面的预览三处都要用。各自实现一遍的话,改一处必漏两处 —— + 而这个仓库里"同一逻辑抄两份、改的时候只改一份"已经反复出现过。 + + ★ `os.path.isfile` 要查:用户可能手动删过 `agent-assets/` 里的文件, + 指着不存在的路径会让界面显示空白 —— 那比回退到默认图糟得多。 + """ + if not isinstance(image, dict): + return '' + front = image.get('front') + # 老数据(没归一化的字符串)也要认:有些路径会直接读 JSON,绕过 store + if isinstance(front, str): + return front.strip() + if not isinstance(front, dict): + return '' + # ① 当前档 ② 平常档 + for key in (preset, '平常'): + path = str(front.get(key) or '').strip() + if path and os.path.isfile(path): + return path + # ③ 字典里任何一个还能用的(用户可能只传了「喜」这一档) + for path in front.values(): + path = str(path or '').strip() + if path and os.path.isfile(path): + return path + return '' + def _set_asset(handler, kind: str, owner_id: str, body: dict, err) -> bool: """ diff --git a/android/kernel/store.py b/android/kernel/store.py index 9061969..e5a00f7 100644 --- a/android/kernel/store.py +++ b/android/kernel/store.py @@ -41,6 +41,42 @@ def now_iso() -> str: return datetime.now(timezone.utc).isoformat(timespec='milliseconds').replace('+00:00', 'Z') +def normalize_image(image) -> dict: + """ + 把 `image` 归一化成 v1.2 的形状(读的时候统一调它)。 + + ★★ v1.2 把 `front` 从**单个文件名**扩成了**五档立绘表**(需求 2.5): + + v1.1: "front": "normal.png" + v1.2: "front": {"喜": "happy.png", "怒": "angry.png", + "哀": "sad.png", "平常": "normal.png", "死亡": "dead.png"} + + 兼容规则是"**旧格式视为仅有「平常」档**"(需求原话)。 + + ★ 为什么在**读**的时候归一化、而不是在写的时候:老数据已经在磁盘上了 + (`agents.json` 里存的就是字符串),写的时候归一化管不到它们。 + 读的时候归一化则一次覆盖新旧两种 —— 而且写入路径不用改, + "用户手改过 agents.json" 这种情况也不会被冲掉。 + + 归一化**不丢信息**:字典里多出来的档位原样保留(用户可能自己加了「醉」这种)。 + """ + img = dict(image or {}) + front = img.get('front') + if isinstance(front, str): + # v1.1 的单文件名 → 只有「平常」档 + img['front'] = {'平常': front} if front.strip() else {} + elif isinstance(front, dict): + img['front'] = { + str(k): str(v) for k, v in front.items() + if str(v or '').strip() + } + else: + img['front'] = {} + for key in ('back', 'border-main', 'border-info'): + img[key] = str(img.get(key) or '') + return img + + def _read_json(file_path: str): """读 JSON;文件不存在或坏掉都返回 None(调用方决定怎么兜底)。""" if not os.path.isfile(file_path): @@ -1063,16 +1099,30 @@ def _apply_agent_patch(agent: dict, patch: dict) -> dict: def list_agents() -> list: + """ + 全部智能体。**出口统一归一化 `image`**(v1.2 的立绘表,见 `normalize_image`)。 + + ★ 归一化放在这里而不是写入时:老数据(`agents.json` 里 `front` 是字符串) + 已经在磁盘上了,写时归一化管不到它们。读时归一化一次覆盖新旧两种, + 而且写入路径一行都不用改。 + """ file_path = paths.get().agents_file if not os.path.isfile(file_path): _write_json(file_path, {'agents': DEFAULT_AGENTS}) - return [dict(a) for a in DEFAULT_AGENTS] + return [normalize_image_agent(dict(a)) for a in DEFAULT_AGENTS] data = _read_json(file_path) if not isinstance(data, dict) or not isinstance(data.get('agents'), list) or not data['agents']: print('[store] agents.json 损坏,重建内置智能体', file=sys.stderr) _write_json(file_path, {'agents': DEFAULT_AGENTS}) - return [dict(a) for a in DEFAULT_AGENTS] - return data['agents'] + return [normalize_image_agent(dict(a)) for a in DEFAULT_AGENTS] + return [normalize_image_agent(a) for a in data['agents']] + + +def normalize_image_agent(agent: dict) -> dict: + """把一个智能体(或世界观)的 `image` 归一化。就地改那份 dict,也返回它""" + if isinstance(agent, dict): + agent['image'] = normalize_image(agent.get('image')) + return agent def get_agent(agent_id: str | None):