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 { 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)

View File

@ -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<NodeDetailInfo | null> {
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
// 手动为节点加 degreeArkTS 不支持展开运算符
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<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;
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;
}
}