Skip to content

Commit 244da66

Browse files
committed
feat(affective): make emotion attributions visible in the panel (#619)
情绪记进去了,但界面上一眼看不到:ego 端点把边上的 metadata 丢了,sleep 相位的失败只写 "llm failed",也没有任何地方能回答「这库一共记了哪些情绪」。 A — 失败原因不再被吞。affective 相位失败时把 stream 层原文写进 warn 并挂在 返回值的 failure 上,与 conflict 相位早就这么做的方式一致。实测出处 2026-11-10T16:09 那一轮,outcome 只有 {"status":"failed","error":"llm failed"}, 真正的 502 只能去 llm_audit_logs 捞。相位自身不重试 5xx,这里记的就是一次性 失败的真因。 B — metadata 透传 + 关系行可读。ego 的边缘映射新增 metadata(不裁剪字段,无 metadata 时给 null 而不是 {},前端据此区分老边与字段缺失)。点边后的侧卡片在 source 行下补一行情绪归属,稳定倾向与单次场合用不同文案;根实体的关系行加一颗 情绪小标签,trait 加重。中英各六条文案,沿用面板既有色,未引入新色相。 C — 单开一栏「情绪」。ego 只能从某个实体往外看,回答不了全局的反向问题,所以 store 新增 relationsByType(一次 SQL,JOIN 取端点名字),API 新增 /semantic/graph/affective,按 person/target/emotion/attribution 聚合并把 trait 排在 occasion 之前。trait 带 traitSessionCount,面板显示跨几个独立会话。 加载中、取失败、真没数据三种状态严格分开:失败给 500 + role=alert,不返回空 列表——空列表在界面上跟「你还没记过情绪」长得一样,含义却相反。截断与「没有 情绪名的边」都显式报出。 测试:全量 3268 / 3267 pass / 0 fail / 1 skipped;check-sync 119 个文件一致。 四处变异自证:去掉 metadata 透传 → graph-api 两条红;退回只写 "llm failed" → 失败原因用例红;把 catch 里的失败退化成空列表 → 前端状态区分用例红;沿用不 存在的 --mneme-fg → 对比度锁红。
1 parent ef8f9ce commit 244da66

12 files changed

Lines changed: 695 additions & 4 deletions

File tree

‎dsh-mneme/lib/api.js‎

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,16 @@ export function createApi(ctx, service, settings, commands, embedder, semantic =
16701670
// 少了它们,「这条边被抬过」在面板上根本看不出来。取不到时给
16711671
// 初值形状而不是 undefined,前端不必到处判空。
16721672
weight: e.weight ?? 1.0,
1673-
source: e.source ?? "manual"
1673+
source: e.source ?? "manual",
1674+
// #619 可见化 B:边上的 metadata 必须原样透传,否则情感边写进去的
1675+
// emotion / attribution / sessionCount / evidence 在面板上永远取不到
1676+
// ——数据在库里、在 store.toRelation 里(src/store.js 已 JSON.parse),
1677+
// 只在最后一跳被这个映射丢掉,读起来就像「这个功能不存在」。
1678+
// 不裁剪字段:情感边的 metadata 由 sleep 相位写入,形状本来就会随版本
1679+
// 演进,在 API 层挑字段等于给未来的字段再挖一个同样的坑。
1680+
// 无 metadata 时给 null,不伪造空对象——前端要靠 null 区分「老边(抽取器
1681+
// 写的,本来就没有情绪)」和「有 metadata 但没有 emotion」。
1682+
metadata: e.metadata ?? null
16741683
}))
16751684
});
16761685
} catch {
@@ -1723,6 +1732,92 @@ export function createApi(ctx, service, settings, commands, embedder, semantic =
17231732
}
17241733
});
17251734

1735+
// --- affective: 情绪归因一览(#619 可见化 C) ---
1736+
// `ego` 端点只能从某个实体往外看,回答不了「这库里一共记了哪些情绪」。
1737+
// 所以单开一条:按 person/target/emotion 聚合,occasion 与 trait 分开。
1738+
//
1739+
// 三处刻意的选择,都是为了让面板不撒谎:
1740+
// - 聚键含 attribution(occasion / trait),两类不混在一条里。同一个「焦虑」
1741+
// 在一件事上是 occasion,跨三件事才是 trait,合成一条会把「这次」说成「一贯」。
1742+
// - trait 组带 independentSessions(跨几个独立会话)。trait 的判据本来就是会话数,
1743+
// 面板只显示「稳定倾向」而不给数字,用户没法判断它是攒够了还是被猜测出来的。
1744+
// - 完全读不到时返回 500 + error,不返回空列表:空列表在面板上长得跟
1745+
// 「你还没记过任何情绪」一样,而这两件事对用户的含义完全相反。
1746+
// 前端靠 httpStatusCode 区分,不靠 items.length。
1747+
register({
1748+
kind: "exact",
1749+
path: "/api/dsh-mneme/semantic/graph/affective",
1750+
policy: "open",
1751+
handler(req, res) {
1752+
try {
1753+
const url = new URL(req.url, "http://localhost");
1754+
const limit = Math.max(1, Math.min(500, Number(url.searchParams.get("limit") ?? 200) || 200));
1755+
if (typeof service.relationsByType !== "function") {
1756+
sendJson(res, 500, { error: "unavailable" });
1757+
return;
1758+
}
1759+
const edges = service.relationsByType("affective_context", limit) ?? [];
1760+
const groups = new Map();
1761+
for (const e of edges) {
1762+
const meta = e.metadata && typeof e.metadata === "object" ? e.metadata : {};
1763+
const emotion = String(meta.emotion ?? "").trim();
1764+
// 没有情绪名的边不进聚合:它是老版本或手工写的空壳,凑进「情绪一览」
1765+
// 会造出一个名字为空的条目。但也不能假装它不存在——计数单列。
1766+
const attribution = String(meta.attribution ?? "").trim() || "occasion";
1767+
if (!emotion) continue;
1768+
const key = `${e.from_entity}\u0000${e.to_entity}\u0000${emotion}\u0000${attribution}`;
1769+
if (!groups.has(key)) {
1770+
groups.set(key, {
1771+
person: e.from_name ?? null,
1772+
personId: e.from_entity,
1773+
target: e.to_name ?? null,
1774+
targetId: e.to_entity,
1775+
emotion,
1776+
attribution,
1777+
promotedToTrait: meta.promotedToTrait === true,
1778+
sessionCount: Number.isFinite(meta.sessionCount) ? meta.sessionCount : null,
1779+
traitSessionCount: Number.isFinite(meta.traitSessionCount) ? meta.traitSessionCount : null,
1780+
evidenceCount: Array.isArray(meta.evidence) ? meta.evidence.length : (e.memory_id ? 1 : 0),
1781+
firstSeen: e.created_at ?? null,
1782+
lastSeen: e.created_at ?? null,
1783+
edgeIds: []
1784+
});
1785+
}
1786+
const g = groups.get(key);
1787+
g.edgeIds.push(e.id);
1788+
if (e.created_at && (!g.lastSeen || e.created_at > g.lastSeen)) g.lastSeen = e.created_at;
1789+
if (e.created_at && (!g.firstSeen || e.created_at < g.firstSeen)) g.firstSeen = e.created_at;
1790+
if (e.from_name) g.person = e.from_name;
1791+
if (e.to_name) g.target = e.to_name;
1792+
if (Number.isFinite(meta.sessionCount)) {
1793+
g.sessionCount = Math.max(g.sessionCount ?? 0, meta.sessionCount);
1794+
}
1795+
if (Number.isFinite(meta.traitSessionCount)) {
1796+
g.traitSessionCount = Math.max(g.traitSessionCount ?? 0, meta.traitSessionCount);
1797+
}
1798+
if (meta.promotedToTrait === true) g.promotedToTrait = true;
1799+
}
1800+
const items = [...groups.values()].sort((a, b) => {
1801+
// trait 在前(更值得看),同类按最近一次出现倒序。
1802+
if ((b.attribution === "trait") !== (a.attribution === "trait")) {
1803+
return a.attribution === "trait" ? -1 : 1;
1804+
}
1805+
return String(b.lastSeen ?? "").localeCompare(String(a.lastSeen ?? ""));
1806+
});
1807+
sendJson(res, 200, {
1808+
items,
1809+
// 读到了多少条原始边、其中多少条因为没情绪名没进聚合。给出来是为了
1810+
// 让「只有 0 条」能自我解释:是库里真没有,还是有边但都不带情绪。
1811+
totalEdges: edges.length,
1812+
skippedEdges: edges.length - items.reduce((n, g) => n + g.edgeIds.length, 0),
1813+
truncated: edges.length >= limit
1814+
});
1815+
} catch {
1816+
sendJson(res, 500, { error: "internal" });
1817+
}
1818+
}
1819+
});
1820+
17261821
// --- entity attrs: current valid attrs for one entity (graph hover panel) ---
17271822
// Read-only; mirrors getCurrentAttrs (valid_until IS NULL). Also used as the
17281823
// graph panel's fallback list when the ego graph is too sparse to draw.

0 commit comments

Comments
 (0)