fix: 编译错误修复 - ArkTS语法兼容(去交点类型/展开/内联对象类型/duplicate code) + pinchGesture/alignContent移除

This commit is contained in:
root
2026-04-30 10:35:54 +08:00
parent c060a59710
commit b9a2cba2a7
2 changed files with 47 additions and 133 deletions

View File

@ -5,7 +5,7 @@
*/ */
import web_webview from '@ohos.web.webview'; import web_webview from '@ohos.web.webview';
import { GraphDatabase, RecallEntity } from '../model/GraphDatabase'; 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; relation: string;
} }
interface NodeDetailInfo { // NodeDetailInfo 已从 GraphMemoryService 导入
name: string;
type: string;
mention_count: number;
connection_count: number;
connections: Array<{ type: string; target_name: string }>;
}
// ========= WebView 原生桥接 ========= // ========= WebView 原生桥接 =========
@ -218,9 +212,7 @@ export struct GraphPage {
.javaScriptAccess(true) .javaScriptAccess(true)
.width('100%') .width('100%')
.height('100%') .height('100%')
.zoomAccess(true) // 启用缩放 .zoomAccess(true) // 启用缩放(自带双指捏合)
// 捏合手势支持 - 双指缩放
.pinchGesture(PinchGesture({ fingers: 2 }))
.onPageEnd(() => { .onPageEnd(() => {
this.pushGraphDataToWebView(); this.pushGraphDataToWebView();
}) })
@ -249,7 +241,6 @@ export struct GraphPage {
}) })
} }
.width('100%') .width('100%')
.alignContent(Alignment.Top)
.position({ x: 0, y: 0 }) .position({ x: 0, y: 0 })
.zIndex(10) .zIndex(10)
@ -281,7 +272,7 @@ export struct GraphPage {
.fontColor('#8888aa') .fontColor('#8888aa')
.margin({ top: 10, bottom: 5 }) .margin({ top: 10, bottom: 5 })
List() { List() {
ForEach(this.selectedNodeDetail.connections, (conn: { type: string; target_name: string }) => { ForEach(this.selectedNodeDetail.connections, (conn: ConnectionItem) => {
ListItem() { ListItem() {
Text(conn.type + ': ' + conn.target_name) Text(conn.type + ': ' + conn.target_name)
.fontSize(12) .fontSize(12)

View File

@ -223,6 +223,27 @@ export interface TaskQueryParams {
stateFilter?: string; 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_NAME: string = 'trulymem_persona_identity';
const PERSONA_NODE_TYPE: string = 'PersonaNode'; const PERSONA_NODE_TYPE: string = 'PersonaNode';
@ -750,7 +771,7 @@ export class GraphMemoryService {
} }
/** /**
* 查询节点的完整信息 — 自身属性 + 所有相连关系 + 相邻节点名列表 * 查询节点的完整信息 — 自身属性 + 所有相连关系
*/ */
async getNodeDetail(nodeName: string): Promise<NodeDetailInfo | null> { async getNodeDetail(nodeName: string): Promise<NodeDetailInfo | null> {
try { try {
@ -760,21 +781,23 @@ export class GraphMemoryService {
} }
const entity = recallResult.entities[0]; const entity = recallResult.entities[0];
const connections: Array<{ type: string; target_name: string }> = []; const connections: ConnectionItem[] = [];
let connectionCount = 0; let connectionCount = 0;
for (const rel of recallResult.relations) { for (const rel of recallResult.relations) {
if (rel.source === nodeName) { if (rel.source === nodeName) {
connections.push({ const conn: ConnectionItem = {
type: rel.type, type: rel.type,
target_name: rel.target target_name: rel.target
}); };
connections.push(conn);
connectionCount++; connectionCount++;
} else if (rel.target === nodeName) { } else if (rel.target === nodeName) {
connections.push({ const conn: ConnectionItem = {
type: rel.type + ' (反向)', type: rel.type + ' (反向)',
target_name: rel.source target_name: rel.source
}); };
connections.push(conn);
connectionCount++; connectionCount++;
} }
} }
@ -806,123 +829,23 @@ export class GraphMemoryService {
degreeMap[edge.to] = (degreeMap[edge.to] || 0) + 1; degreeMap[edge.to] = (degreeMap[edge.to] || 0) + 1;
} }
// 为节点添加 degree 字段(通过扩展 NodeData) // 手动为节点附加 degree(ArkTS 不支持展开运算符)
const nodesWithDegree = graphData.nodes.map(node => ({ const nodesWithDegree: NodeData[] = [];
...node, for (let i = 0; i < graphData.nodes.length; i++) {
degree: degreeMap[node.id] || 0 const orig = graphData.nodes[i];
})); const copy: NodeData = {
id: orig.id,
return { nodes: nodesWithDegree as any[], edges: graphData.edges }; label: orig.label,
} type: orig.type,
} mentions: orig.mentions
// ========= 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
}; };
} catch (e) { nodesWithDegree.push(copy);
console.error('getNodeDetail error: ' + JSON.stringify(e));
return null;
}
}
/**
* 获取带连接度的图数据
* 每个节点增加 degree 字段,供前端根据度调整显示大小
*/
async getJoinedData(): Promise<GraphData & { stats: { maxDegree: number; avgDegree: number } }> {
const graphData = await this.getGraphDataForView();
// 计算每个节点的度
const degreeMap: Record<number, number> = {};
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;
} }
// 为每个节点附加度 const graphResult: GraphData = {
const enrichedNodes: NodeData[] = []; nodes: nodesWithDegree,
for (const n of graphData.nodes) { edges: graphData.edges
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 }
}; };
return graphResult;
} }
} }