tmp
This commit is contained in:
+27
-2
@@ -1,4 +1,5 @@
|
||||
import 'dotenv/config';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { createDeepAgent, FilesystemBackend, CompositeBackend } from "deepagents";
|
||||
import { ChatOpenAI } from "@langchain/openai";
|
||||
@@ -7,6 +8,10 @@ import { HumanMessage } from "@langchain/core/messages";
|
||||
import { ChatDeepSeek } from "@langchain/deepseek";
|
||||
import config from '../../conf.json' with { type: 'json' };
|
||||
import logger from '../../utils/logger.js';
|
||||
|
||||
// 密钥由 conf.json 统一提供;百度地图 skill 通过 env_get 读环境变量,此处桥接
|
||||
process.env.TAVILY_API_KEY ??= config.agent.tavily?.apiKey;
|
||||
process.env.BAIDU_MAP_AUTH_TOKEN ??= config.agent.baiduMap?.authToken;
|
||||
import Prompts from "./prompts.js";
|
||||
import {
|
||||
getEnvTool, webFetchTool, webSearchTool, getCalendarInfoTool,
|
||||
@@ -19,6 +24,7 @@ export default class EscortAgent {
|
||||
static MAX_HISTORY_CHARS = config.agent.maxHistoryChars || 40000;
|
||||
|
||||
constructor() {
|
||||
this.messages = [];
|
||||
}
|
||||
|
||||
clearMessages() {
|
||||
@@ -137,12 +143,19 @@ export default class EscortAgent {
|
||||
}
|
||||
|
||||
_genAgent(userInfo) {
|
||||
const rootDir = process.cwd();
|
||||
const memoryFile = userInfo
|
||||
? path.join(rootDir, "data", userInfo._id, "memories", "user_memory.txt")
|
||||
: null;
|
||||
|
||||
// 会话中记忆文件被更新后重建 agent,使 systemPrompt 中的用户记忆保持最新
|
||||
if (this.agent && this._memoryMtime !== this._statMemory(memoryFile)) {
|
||||
this.agent = null;
|
||||
}
|
||||
if (this.agent) {
|
||||
return this.agent;
|
||||
}
|
||||
|
||||
const rootDir = process.cwd();
|
||||
this.messages = [];
|
||||
let backend = new FilesystemBackend({ rootDir });
|
||||
|
||||
if (userInfo) {
|
||||
@@ -164,6 +177,8 @@ export default class EscortAgent {
|
||||
temperature: 0.0
|
||||
});
|
||||
|
||||
this._memoryMtime = this._statMemory(memoryFile);
|
||||
|
||||
this.agent = createDeepAgent({
|
||||
name: "deep-agent",
|
||||
model: this.flashModel,
|
||||
@@ -178,4 +193,14 @@ export default class EscortAgent {
|
||||
|
||||
return this.agent;
|
||||
}
|
||||
|
||||
// 返回记忆文件 mtime,文件不存在时返回 null
|
||||
_statMemory(memoryFile) {
|
||||
if (!memoryFile) return null;
|
||||
try {
|
||||
return fs.statSync(memoryFile).mtimeMs;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user