mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-25 17:54:34 +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
@@ -11,6 +11,7 @@ import type {
|
||||
SemanticIndexRuntime,
|
||||
CrossChatAnalysisService,
|
||||
SessionRuntimeAdapter,
|
||||
AIMemoryService,
|
||||
} from '@openchatlab/node-runtime'
|
||||
import {
|
||||
CHART_CAPABILITY_CORE_TOOLS,
|
||||
@@ -84,6 +85,8 @@ export function createCliRunAgentStream(
|
||||
semanticIndexService?: SemanticIndexRuntime
|
||||
crossChatAnalysisService?: CrossChatAnalysisService
|
||||
sessionAdapter?: SessionRuntimeAdapter
|
||||
memoryService?: AIMemoryService
|
||||
getAllowProactiveMemory?: () => boolean
|
||||
} = {}
|
||||
): (params: AgentStreamRequest, onEvent: (chunk: AgentStreamChunk) => void, abortSignal: AbortSignal) => Promise<void> {
|
||||
const aiDataDir = getAiDir(dbManager)
|
||||
@@ -144,7 +147,7 @@ export function createCliRunAgentStream(
|
||||
onEvent({ type: 'done', isFinished: true })
|
||||
return
|
||||
}
|
||||
if (!options.crossChatAnalysisService || !options.sessionAdapter) {
|
||||
if (!options.crossChatAnalysisService || !options.sessionAdapter || !options.memoryService) {
|
||||
onEvent({ type: 'error', error: { name: 'RuntimeError', message: 'Cross-chat analysis is unavailable' } })
|
||||
onEvent({ type: 'done', isFinished: true })
|
||||
return
|
||||
@@ -155,6 +158,9 @@ export function createCliRunAgentStream(
|
||||
locale,
|
||||
preprocessConfig: params.preprocessConfig,
|
||||
maxToolResultTokens: resolveCrossChatToolResultTokenBudget(contextWindow),
|
||||
memoryService: options.memoryService,
|
||||
aiChatId,
|
||||
allowProactiveMemory: options.getAllowProactiveMemory?.() ?? true,
|
||||
})
|
||||
await runCrossChatAgent({
|
||||
userMessage,
|
||||
@@ -166,6 +172,7 @@ export function createCliRunAgentStream(
|
||||
apiKey: llmConfig.apiKey,
|
||||
tools,
|
||||
aiChatManager,
|
||||
memoryService: options.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 createCliCrossChatTools(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) =>
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
createGlobalInsightService,
|
||||
createDatabaseManagerAdapter,
|
||||
PreferencesManager,
|
||||
AIMemoryService,
|
||||
} from '@openchatlab/node-runtime'
|
||||
import type { SemanticIndexRuntime } from '@openchatlab/node-runtime'
|
||||
import { createServer } from './server'
|
||||
@@ -44,6 +45,7 @@ import { resolveCliLocalEmbeddingRuntimeConfig } from '../semantic-index/local-r
|
||||
let server: FastifyInstance | null = null
|
||||
let dbManager: DatabaseManager | null = null
|
||||
let aiChatManager: AIChatManager | null = null
|
||||
let aiMemoryService: AIMemoryService | null = null
|
||||
|
||||
export interface HttpServerOptions {
|
||||
port?: number
|
||||
@@ -165,6 +167,7 @@ export async function startHttpServer(options?: HttpServerOptions): Promise<{
|
||||
|
||||
const aiDataDir = pathProvider.getAiDataDir()
|
||||
aiChatManager = new AIChatManager(aiDataDir, { nativeBinding })
|
||||
aiMemoryService = new AIMemoryService(aiDataDir, { nativeBinding })
|
||||
|
||||
const assistantManager = getAssistantManager(aiDataDir)
|
||||
const skillManagerCore = getSkillManagerCore(aiDataDir)
|
||||
@@ -228,6 +231,8 @@ export async function startHttpServer(options?: HttpServerOptions): Promise<{
|
||||
semanticIndexService,
|
||||
crossChatAnalysisService,
|
||||
sessionAdapter,
|
||||
memoryService: aiMemoryService,
|
||||
getAllowProactiveMemory: () => preferencesManager.load().aiGlobalSettings.allowProactiveMemory,
|
||||
}),
|
||||
},
|
||||
})
|
||||
@@ -270,6 +275,10 @@ export async function stopHttpServer(): Promise<void> {
|
||||
aiChatManager.close()
|
||||
aiChatManager = null
|
||||
}
|
||||
if (aiMemoryService) {
|
||||
aiMemoryService.close()
|
||||
aiMemoryService = null
|
||||
}
|
||||
if (dbManager) {
|
||||
dbManager.closeAll()
|
||||
dbManager = null
|
||||
|
||||
Reference in new issue
Block a user