新增 memory 和 skill 存储,实现 Agent 持续学习,并增加工具支持;增加 LLM progress detail 输出
This commit is contained in:
+64
-5
@@ -1,22 +1,26 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
import cors from "cors";
|
||||
import express from "express";
|
||||
|
||||
import { ChatSessionBridge } from "./chat/sessionBridge.js";
|
||||
import { config } from "./config.js";
|
||||
import { logger } from "./logger.js";
|
||||
import { MemoryStore } from "./memory/store.js";
|
||||
import { ResultReferenceStore } from "./results/store.js";
|
||||
import { buildChatRouter } from "./routes/chat.js";
|
||||
import { opencodeRuntime } from "./runtime/opencode.js";
|
||||
import { SessionRegistry } from "./session/registry.js";
|
||||
import { dynamicHttpExecutor } from "./tools/dynamicHttpExecutor.js";
|
||||
import { DynamicHttpExecutor } from "./tools/dynamicHttpExecutor.js";
|
||||
|
||||
const app = express();
|
||||
const registry = new SessionRegistry(config.SESSION_TTL_SECONDS);
|
||||
const sessionBridge = new ChatSessionBridge(registry, opencodeRuntime);
|
||||
const memoryStore = new MemoryStore();
|
||||
const resultReferenceStore = new ResultReferenceStore();
|
||||
const dynamicHttpExecutor = new DynamicHttpExecutor(resultReferenceStore);
|
||||
const internalToken = config.AGENT_INTERNAL_TOKEN ?? randomUUID();
|
||||
|
||||
// 这个 token 只用于 .opencode/tools 回调本服务,避免把 internal endpoint 暴露成无鉴权入口。
|
||||
// 这个 token 只用于仍需服务端上下文的工具桥(dynamic_http_call / fetch_result_ref)。
|
||||
process.env.TJWATER_AGENT_INTERNAL_TOKEN = internalToken;
|
||||
|
||||
app.use(cors());
|
||||
@@ -66,7 +70,15 @@ app.post("/internal/tools/dynamic-http-call", async (req, res) => {
|
||||
method: req.body?.method,
|
||||
arguments: req.body?.arguments,
|
||||
},
|
||||
context,
|
||||
{
|
||||
accessToken: context.accessToken,
|
||||
actorKey: context.actorKey,
|
||||
clientSessionId: context.clientSessionId,
|
||||
projectId: context.projectId,
|
||||
projectKey: context.projectKey,
|
||||
sessionId,
|
||||
traceId: context.traceId,
|
||||
},
|
||||
);
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
@@ -78,7 +90,53 @@ app.post("/internal/tools/dynamic-http-call", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.use("/api/v1/agent/chat", buildChatRouter(sessionBridge, opencodeRuntime));
|
||||
app.post("/internal/tools/fetch-result-ref", async (req, res) => {
|
||||
if (req.header("x-agent-internal-token") !== internalToken) {
|
||||
res.status(403).json({ message: "forbidden" });
|
||||
return;
|
||||
}
|
||||
|
||||
const sessionId = typeof req.body?.sessionId === "string" ? req.body.sessionId : "";
|
||||
const resultRef = typeof req.body?.result_ref === "string" ? req.body.result_ref : "";
|
||||
const context = sessionBridge.getSessionContext(sessionId);
|
||||
if (!context) {
|
||||
res.status(404).json({
|
||||
message: "session context not found",
|
||||
detail: sessionId,
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!resultRef) {
|
||||
res.status(400).json({ message: "result_ref is required" });
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await resultReferenceStore.getAuthorized(resultRef, {
|
||||
actorKey: context.actorKey,
|
||||
maxItems:
|
||||
typeof req.body?.max_items === "number" ? req.body.max_items : undefined,
|
||||
projectId: context.projectId,
|
||||
});
|
||||
|
||||
if (!result) {
|
||||
res.status(404).json({ message: "result_ref not found" });
|
||||
return;
|
||||
}
|
||||
|
||||
res.json(result);
|
||||
});
|
||||
|
||||
app.use(
|
||||
"/api/v1/agent/chat",
|
||||
buildChatRouter(sessionBridge, opencodeRuntime, memoryStore),
|
||||
);
|
||||
|
||||
const bootstrap = async () => {
|
||||
await Promise.all([memoryStore.initialize(), resultReferenceStore.initialize()]);
|
||||
resultReferenceStore.startCleanupLoop();
|
||||
};
|
||||
|
||||
await bootstrap();
|
||||
|
||||
const server = app.listen(config.PORT, config.HOST, () => {
|
||||
logger.info(
|
||||
@@ -90,6 +148,7 @@ const server = app.listen(config.PORT, config.HOST, () => {
|
||||
const shutdown = async () => {
|
||||
logger.info("shutting down TJWaterAgent");
|
||||
server.close();
|
||||
resultReferenceStore.stopCleanupLoop();
|
||||
// 同步关闭 embedded opencode server,避免本服务退出后留下孤儿进程。
|
||||
await opencodeRuntime.dispose();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user