mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-26 02:04:37 +08:00
feat(ai): integrate memory with global agent
This commit is contained in:
1 parent
bdc61d1b10
commit
ed8ab84b4b
23 files changed
+741
-19
No files matched your search
@@ -13,6 +13,7 @@ import type {
|
||||
SemanticIndexRuntime,
|
||||
CrossChatAnalysisService,
|
||||
SessionRuntimeAdapter,
|
||||
AIMemoryService,
|
||||
} from '@openchatlab/node-runtime'
|
||||
import type { AgentStreamRequest } from '@openchatlab/http-routes'
|
||||
import { getDefaultGeneralAssistantId, type ChartAutoMode } from '@openchatlab/shared-types'
|
||||
@@ -77,6 +78,8 @@ export function createElectronRunAgentStream(
|
||||
crossChatOptions?: {
|
||||
analysisService: CrossChatAnalysisService
|
||||
sessionAdapter: SessionRuntimeAdapter
|
||||
memoryService: AIMemoryService
|
||||
getAllowProactiveMemory: () => boolean
|
||||
}
|
||||
): (
|
||||
params: AgentStreamRequest,
|
||||
@@ -150,6 +153,9 @@ export function createElectronRunAgentStream(
|
||||
locale,
|
||||
preprocessConfig: params.preprocessConfig,
|
||||
maxToolResultTokens: resolveCrossChatToolResultTokenBudget(resolvedContextWindow),
|
||||
memoryService: crossChatOptions.memoryService,
|
||||
aiChatId,
|
||||
allowProactiveMemory: crossChatOptions.getAllowProactiveMemory(),
|
||||
})
|
||||
await runCrossChatAgent({
|
||||
userMessage,
|
||||
@@ -161,6 +167,7 @@ export function createElectronRunAgentStream(
|
||||
apiKey: activeAIConfig.apiKey,
|
||||
tools,
|
||||
aiChatManager: getAIChatManager(),
|
||||
memoryService: crossChatOptions.memoryService,
|
||||
onEvent,
|
||||
abortSignal,
|
||||
thinkingLevel: thinkingLevel as import('@openchatlab/core').ThinkingLevel | undefined,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type {
|
||||
AgentTool,
|
||||
AIMemoryService,
|
||||
CrossChatAnalysisService,
|
||||
PreprocessConfig,
|
||||
SessionRuntimeAdapter,
|
||||
@@ -18,10 +19,16 @@ export function createElectronCrossChatTools(options: {
|
||||
locale?: string
|
||||
preprocessConfig?: Record<string, unknown>
|
||||
maxToolResultTokens: number
|
||||
memoryService: AIMemoryService
|
||||
aiChatId: string
|
||||
allowProactiveMemory: boolean
|
||||
}): AgentTool<any, any>[] {
|
||||
const context: Omit<CrossChatToolExecutionContext, 'abortSignal' | 'reportProgress'> = {
|
||||
locale: options.locale,
|
||||
analysisService: options.analysisService,
|
||||
memoryService: options.memoryService,
|
||||
aiChatId: options.aiChatId,
|
||||
allowProactiveMemory: options.allowProactiveMemory,
|
||||
maxToolResultTokens: options.maxToolResultTokens,
|
||||
countTokens,
|
||||
preprocessMessagesBySession: (sessionId, messages) =>
|
||||
|
||||
@@ -29,6 +29,7 @@ import {
|
||||
createCrossChatAnalysisService,
|
||||
createGlobalInsightService,
|
||||
PreferencesManager,
|
||||
AIMemoryService,
|
||||
} from '@openchatlab/node-runtime'
|
||||
import type { StreamImportDeps, SemanticIndexRuntime } from '@openchatlab/node-runtime'
|
||||
import { getLoadablePath as getSqliteVecLoadablePath } from 'sqlite-vec'
|
||||
@@ -66,6 +67,7 @@ let endpoint: InternalEndpoint | null = null
|
||||
let dbManager: DatabaseManager | null = null
|
||||
let mergeCache: MergeSessionCache | null = null
|
||||
let semanticIndexService: SemanticIndexRuntime | null = null
|
||||
let aiMemoryService: AIMemoryService | null = null
|
||||
|
||||
const JSON_BODY_LIMIT = 50 * 1024 * 1024 // 50 MB
|
||||
|
||||
@@ -87,6 +89,7 @@ export async function startInternalServer(
|
||||
let newServer: FastifyInstance | null = null
|
||||
let newDbManager: DatabaseManager | null = null
|
||||
let newSemanticIndexService: SemanticIndexRuntime | null = null
|
||||
let newAIMemoryService: AIMemoryService | null = null
|
||||
|
||||
try {
|
||||
const token = `int_${randomBytes(32).toString('hex')}`
|
||||
@@ -117,6 +120,7 @@ export async function startInternalServer(
|
||||
})
|
||||
|
||||
const aiDataDir = pathProvider.getAiDataDir()
|
||||
newAIMemoryService = new AIMemoryService(aiDataDir, { nativeBinding })
|
||||
const llmRuntimeStores = getDesktopLlmRuntimeStores(aiDataDir)
|
||||
const { llmConfigStore, customProviderStore, customModelStore } = llmRuntimeStores
|
||||
appLogger.info('ai-config', 'Shared LLM configuration store initialized', { aiDataDir })
|
||||
@@ -223,6 +227,8 @@ export async function startInternalServer(
|
||||
runAgentStream: createElectronRunAgentStream(llmConfigStore, newSemanticIndexService ?? undefined, {
|
||||
analysisService: crossChatAnalysisService,
|
||||
sessionAdapter,
|
||||
memoryService: newAIMemoryService,
|
||||
getAllowProactiveMemory: () => preferencesManager.load().aiGlobalSettings.allowProactiveMemory,
|
||||
}),
|
||||
executeAiTool: createExecuteElectronAiTool(newSemanticIndexService ?? undefined),
|
||||
}
|
||||
@@ -256,6 +262,7 @@ export async function startInternalServer(
|
||||
dbManager = newDbManager
|
||||
mergeCache = newMergeCache
|
||||
semanticIndexService = newSemanticIndexService
|
||||
aiMemoryService = newAIMemoryService
|
||||
endpoint = { baseUrl: `http://127.0.0.1:${port}`, token }
|
||||
console.log(`[InternalAPI] Server started on port ${port}`)
|
||||
|
||||
@@ -276,10 +283,16 @@ export async function startInternalServer(
|
||||
} catch {
|
||||
/* best-effort */
|
||||
}
|
||||
try {
|
||||
newAIMemoryService?.close()
|
||||
} catch {
|
||||
/* best-effort */
|
||||
}
|
||||
server = null
|
||||
dbManager = null
|
||||
mergeCache = null
|
||||
semanticIndexService = null
|
||||
aiMemoryService = null
|
||||
endpoint = null
|
||||
throw err
|
||||
}
|
||||
@@ -321,11 +334,17 @@ export async function stopInternalServer(): Promise<void> {
|
||||
} catch {
|
||||
/* best-effort */
|
||||
}
|
||||
try {
|
||||
aiMemoryService?.close()
|
||||
} catch {
|
||||
/* best-effort */
|
||||
}
|
||||
server = null
|
||||
endpoint = null
|
||||
dbManager = null
|
||||
mergeCache = null
|
||||
semanticIndexService = null
|
||||
aiMemoryService = null
|
||||
console.log('[InternalAPI] Server stopped')
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user