@@ -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