diff --git a/entry/src/main/ets/pages/ChatPage.ets b/entry/src/main/ets/pages/ChatPage.ets index 9288371..55e6c79 100644 --- a/entry/src/main/ets/pages/ChatPage.ets +++ b/entry/src/main/ets/pages/ChatPage.ets @@ -15,7 +15,7 @@ export struct ChatPage { async aboutToAppear() { // 初始化图记忆服务和 Agent const memoryService = new GraphMemoryService(this.db); - this.agentService = new AIAgentService(memoryService); + this.agentService = new AIAgentService(memoryService, getContext(this)); // 加载历史消息(兼容旧数据:无 session_id 时加载全部) const rawHistory = await this.db.getChatHistory(50, this.agentService.getSessionId()); diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 032a3ad..41561f1 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -8,11 +8,18 @@ import { ImmersiveTabNavigation } from '../components/ImmersiveTabNavigation'; struct Index { @State currentIndex: number = 0; private db: GraphDatabase = new GraphDatabase(); + private displayCallback?: (data: display.Display) => void; aboutToAppear() { this.db.init(getContext(this)); } + aboutToDisappear() { + if (this.displayCallback) { + display.off('change', this.displayCallback); + } + } + @Builder tabContentBuilder() { Column() { diff --git a/entry/src/main/ets/services/AIAgentService.ets b/entry/src/main/ets/services/AIAgentService.ets index 6d2fba2..d66cd38 100644 --- a/entry/src/main/ets/services/AIAgentService.ets +++ b/entry/src/main/ets/services/AIAgentService.ets @@ -6,6 +6,7 @@ */ import http from '@ohos.net.http'; import dataPreferences from '@ohos.data.preferences'; +import { Context } from '@ohos.app.ability.UIAbility'; import { GraphMemoryService, EntityInfo, RelationInfo, TaskInfo, MemoryRecallParams, MemoryCommitParams, MemoryPurgeParams, TimeRangeParams, PurgeCriteriaParams, NewRelationParams, PersonaUpdateParams, TaskCreateParams, TaskSetStateParams, TaskDeleteParams, TaskLinkInfoParams, TaskArchiveParams, TaskQueryParams, TripletInput, PersonaQueryResult, TaskQueryResult, MemoryRecallResult } from './GraphMemoryService'; export interface ChatMessage { @@ -382,8 +383,11 @@ export class AIAgentService { private currentSessionId: string; private turnCounter: number = 0; - constructor(memoryService: GraphMemoryService, sessionId?: string) { + private appContext: Context; + + constructor(memoryService: GraphMemoryService, appContext: Context, sessionId?: string) { this.memoryService = memoryService; + this.appContext = appContext; this.currentSessionId = sessionId || `session-hm-${Date.now()}`; } @@ -413,7 +417,7 @@ export class AIAgentService { const taskResult = await this.memoryService.memoryRecall(recallParams); // === 读取 API 配置 === - const context = getContext(this); + const context = this.appContext; const pref = await dataPreferences.getPreferences(context, 'trulymem_config'); const baseUrl: string = String(await pref.get('base_url', 'https://api.deepseek.com')); const model: string = String(await pref.get('model', 'deepseek-chat'));