From b9a2cba2a74df48a89deaefe7039cfde4ecfaacc Mon Sep 17 00:00:00 2001 From: root Date: Thu, 30 Apr 2026 10:35:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BC=96=E8=AF=91=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20-=20ArkTS=E8=AF=AD=E6=B3=95=E5=85=BC?= =?UTF-8?q?=E5=AE=B9(=E5=8E=BB=E4=BA=A4=E7=82=B9=E7=B1=BB=E5=9E=8B/?= =?UTF-8?q?=E5=B1=95=E5=BC=80/=E5=86=85=E8=81=94=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B/duplicate=20code)=20+=20pinchGesture/alignCo?= =?UTF-8?q?ntent=E7=A7=BB=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/pages/GraphPage.ets | 17 +- .../main/ets/services/GraphMemoryService.ets | 163 +++++------------- 2 files changed, 47 insertions(+), 133 deletions(-) diff --git a/entry/src/main/ets/pages/GraphPage.ets b/entry/src/main/ets/pages/GraphPage.ets index 0b5f950..9dc5b7e 100644 --- a/entry/src/main/ets/pages/GraphPage.ets +++ b/entry/src/main/ets/pages/GraphPage.ets @@ -5,7 +5,7 @@ */ import web_webview from '@ohos.web.webview'; import { GraphDatabase, RecallEntity } from '../model/GraphDatabase'; -import { GraphMemoryService } from '../services/GraphMemoryService'; +import { GraphMemoryService, ConnectionItem, NodeDetailInfo } from '../services/GraphMemoryService'; // ========= 图数据结构定义 ========= @@ -24,13 +24,7 @@ interface GraphEdgeItem { relation: string; } -interface NodeDetailInfo { - name: string; - type: string; - mention_count: number; - connection_count: number; - connections: Array<{ type: string; target_name: string }>; -} +// NodeDetailInfo 已从 GraphMemoryService 导入 // ========= WebView 原生桥接 ========= @@ -218,9 +212,7 @@ export struct GraphPage { .javaScriptAccess(true) .width('100%') .height('100%') - .zoomAccess(true) // 启用缩放 - // 捏合手势支持 - 双指缩放 - .pinchGesture(PinchGesture({ fingers: 2 })) + .zoomAccess(true) // 启用缩放(自带双指捏合) .onPageEnd(() => { this.pushGraphDataToWebView(); }) @@ -249,7 +241,6 @@ export struct GraphPage { }) } .width('100%') - .alignContent(Alignment.Top) .position({ x: 0, y: 0 }) .zIndex(10) @@ -281,7 +272,7 @@ export struct GraphPage { .fontColor('#8888aa') .margin({ top: 10, bottom: 5 }) List() { - ForEach(this.selectedNodeDetail.connections, (conn: { type: string; target_name: string }) => { + ForEach(this.selectedNodeDetail.connections, (conn: ConnectionItem) => { ListItem() { Text(conn.type + ': ' + conn.target_name) .fontSize(12) diff --git a/entry/src/main/ets/services/GraphMemoryService.ets b/entry/src/main/ets/services/GraphMemoryService.ets index ba7b817..25e2d41 100644 --- a/entry/src/main/ets/services/GraphMemoryService.ets +++ b/entry/src/main/ets/services/GraphMemoryService.ets @@ -223,6 +223,27 @@ export interface TaskQueryParams { stateFilter?: string; } +// 节点详情连接项接口 +export interface ConnectionItem { + type: string; + target_name: string; +} + +// 节点详情返回接口 +export interface NodeDetailInfo { + name: string; + type: string; + mention_count: number; + connection_count: number; + connections: ConnectionItem[]; +} + +// 图数据统计接口 +export interface GraphStats { + maxDegree: number; + avgDegree: number; +} + // 人设节点的固定名称 const PERSONA_NODE_NAME: string = 'trulymem_persona_identity'; const PERSONA_NODE_TYPE: string = 'PersonaNode'; @@ -750,7 +771,7 @@ export class GraphMemoryService { } /** - * 查询节点的完整信息 — 自身属性 + 所有相连关系 + 相邻节点名列表 + * 查询节点的完整信息 — 自身属性 + 所有相连关系 */ async getNodeDetail(nodeName: string): Promise { try { @@ -760,21 +781,23 @@ export class GraphMemoryService { } const entity = recallResult.entities[0]; - const connections: Array<{ type: string; target_name: string }> = []; + const connections: ConnectionItem[] = []; let connectionCount = 0; for (const rel of recallResult.relations) { if (rel.source === nodeName) { - connections.push({ + const conn: ConnectionItem = { type: rel.type, target_name: rel.target - }); + }; + connections.push(conn); connectionCount++; } else if (rel.target === nodeName) { - connections.push({ + const conn: ConnectionItem = { type: rel.type + ' (反向)', target_name: rel.source - }); + }; + connections.push(conn); connectionCount++; } } @@ -806,123 +829,23 @@ export class GraphMemoryService { degreeMap[edge.to] = (degreeMap[edge.to] || 0) + 1; } - // 为节点添加 degree 字段(通过扩展 NodeData) - const nodesWithDegree = graphData.nodes.map(node => ({ - ...node, - degree: degreeMap[node.id] || 0 - })); - - return { nodes: nodesWithDegree as any[], edges: graphData.edges }; - } -} - -// ========= NodeDetailInfo 接口定义 ========= - -interface NodeDetailInfo { - name: string; - type: string; - mention_count: number; - connection_count: number; - connections: Array<{ type: string; target_name: string }>; -} - - for (const rel of recallResult.relations) { - const from = nameToId[rel.source]; - const to = nameToId[rel.target]; - if (from !== undefined && to !== undefined) { - const edge: EdgeData = { - from, - to, - label: rel.type, - weight: rel.confidence - }; - edges.push(edge); - } - } - - const graphData: GraphData = { nodes, edges }; - return graphData; - } - - /** - * 获取节点详细信息——自身属性 + 所有相连关系 + 相邻节点名列表 - * 供 GraphPage 节点点击后查询用 - */ - async getNodeDetail(nodeName: string): Promise<{ - name: string; - type: string; - mention_count: number; - connection_count: number; - connections: Array<{ type: string; target_name: string }>; - } | null> { - try { - // 搜索节点 - const searchResults = await this.db.search(nodeName); - const node = searchResults.find(n => n.name === nodeName); - if (!node) { - return null; - } - - // 查询与该节点相关的所有关系 - const recallResult = await this.db.recall(nodeName, [nodeName], 1); - const connections: Array<{ type: string; target_name: string }> = []; - - for (const rel of recallResult.relations) { - if (rel.source === nodeName) { - connections.push({ type: rel.type, target_name: rel.target }); - } else if (rel.target === nodeName) { - connections.push({ type: rel.type + '(反向)', target_name: rel.source }); - } - } - - return { - name: node.name, - type: node.type, - mention_count: node.mentions, - connection_count: connections.length, - connections + // 手动为节点附加 degree(ArkTS 不支持展开运算符) + const nodesWithDegree: NodeData[] = []; + for (let i = 0; i < graphData.nodes.length; i++) { + const orig = graphData.nodes[i]; + const copy: NodeData = { + id: orig.id, + label: orig.label, + type: orig.type, + mentions: orig.mentions }; - } catch (e) { - console.error('getNodeDetail error: ' + JSON.stringify(e)); - return null; - } - } - - /** - * 获取带连接度的图数据 - * 每个节点增加 degree 字段,供前端根据度调整显示大小 - */ - async getJoinedData(): Promise { - const graphData = await this.getGraphDataForView(); - - // 计算每个节点的度 - const degreeMap: Record = {}; - for (const node of graphData.nodes) { - degreeMap[node.id] = 0; - } - for (const edge of graphData.edges) { - degreeMap[edge.from] = (degreeMap[edge.from] || 0) + 1; - degreeMap[edge.to] = (degreeMap[edge.to] || 0) + 1; + nodesWithDegree.push(copy); } - // 为每个节点附加度 - const enrichedNodes: NodeData[] = []; - for (const n of graphData.nodes) { - const enriched: NodeData & { degree: number } = n as (NodeData & { degree: number }); - enriched.degree = degreeMap[n.id] || 0; - enrichedNodes.push(enriched); - } - - const degrees = Object.values(degreeMap); - const maxDegree = Math.max(...degrees, 1); - const avgDegree = degrees.length > 0 - ? degrees.reduce((a, b) => a + b, 0) / degrees.length - : 0; - - return { - nodes: enrichedNodes, - edges: graphData.edges, - stats: { maxDegree, avgDegree } + const graphResult: GraphData = { + nodes: nodesWithDegree, + edges: graphData.edges }; + return graphResult; } }