切换到使用pg数据库

This commit is contained in:
2026-05-28 18:22:39 +08:00
parent 4c47841483
commit 5ac50bfeaa
38 changed files with 2760 additions and 1331 deletions
+32 -28
View File
@@ -14,7 +14,7 @@ import { ResultReferenceResolver } from "./results/resolver.js";
import { ResultReferenceStore } from "./results/store.js";
import { buildChatRouter } from "./routes/chat.js";
import { opencodeRuntime } from "./runtime/opencode.js";
import { ToolSessionContextStore } from "./session/toolContextStore.js";
import { RuntimeSessionStore } from "./session/runtimeSessionStore.js";
import { DynamicHttpExecutor } from "./tools/dynamicHttpExecutor.js";
const app = express();
@@ -23,7 +23,7 @@ const conversationStore = new ConversationStore();
const conversationStateStore = new ConversationStateStore();
const memoryStore = new MemoryStore();
const sessionHistoryStore = new SessionHistoryStore();
const toolContextStore = new ToolSessionContextStore();
const runtimeSessionStore = new RuntimeSessionStore();
const learningOrchestrator = new LearningOrchestrator(
opencodeRuntime,
memoryStore,
@@ -65,22 +65,26 @@ app.post("/internal/tools/dynamic-http-call", async (req, res) => {
return;
}
const sessionScopeKey =
typeof req.body?.sessionScopeKey === "string" ? req.body.sessionScopeKey : "";
const threadContext = await toolContextStore.read(sessionScopeKey);
const runtimeContext = sessionBridge.getActiveSensitiveContext(sessionScopeKey);
if (!threadContext && !runtimeContext) {
const runtimeSessionId =
typeof req.body?.runtimeSessionId === "string" ? req.body.runtimeSessionId.trim() : "";
const persistedContext = runtimeSessionId
? await runtimeSessionStore.read(runtimeSessionId)
: null;
const runtimeContext = runtimeSessionId
? sessionBridge.getActiveSensitiveContext(runtimeSessionId)
: null;
if (!persistedContext && !runtimeContext) {
res.status(404).json({
message: "runtime or session context not found",
detail: sessionScopeKey,
detail: runtimeSessionId,
});
return;
}
const context = runtimeContext ?? threadContext;
const context = persistedContext;
if (!context) {
res.status(404).json({
message: "runtime or session context not found",
detail: sessionScopeKey,
detail: runtimeSessionId,
});
return;
}
@@ -93,14 +97,14 @@ app.post("/internal/tools/dynamic-http-call", async (req, res) => {
path: req.body?.path,
method: req.body?.method,
arguments: req.body?.arguments,
body: req.body?.body,
},
{
accessToken: runtimeContext?.accessToken,
actorKey: context.actorKey,
clientSessionId: context.clientSessionId,
sessionId: context.sessionId,
projectId: context.projectId,
projectKey: context.projectKey,
sessionId: context.clientSessionId,
traceId: context.traceId,
},
);
@@ -120,14 +124,14 @@ app.post("/internal/tools/fetch-result-ref", async (req, res) => {
return;
}
const sessionScopeKey =
typeof req.body?.sessionScopeKey === "string" ? req.body.sessionScopeKey : "";
const runtimeSessionId =
typeof req.body?.runtimeSessionId === "string" ? req.body.runtimeSessionId.trim() : "";
const resultRef = typeof req.body?.result_ref === "string" ? req.body.result_ref : "";
const context = await toolContextStore.read(sessionScopeKey);
const context = runtimeSessionId ? await runtimeSessionStore.read(runtimeSessionId) : null;
if (!context) {
res.status(404).json({
message: "session context not found",
detail: sessionScopeKey,
detail: runtimeSessionId,
});
return;
}
@@ -140,7 +144,7 @@ app.post("/internal/tools/fetch-result-ref", async (req, res) => {
resultRef,
{
actorKey: context.actorKey,
clientSessionId: context.clientSessionId,
sessionId: context.sessionId,
projectId: context.projectId,
},
{
@@ -163,14 +167,14 @@ app.post("/internal/tools/store-render-ref", async (req, res) => {
return;
}
const sessionScopeKey =
typeof req.body?.sessionScopeKey === "string" ? req.body.sessionScopeKey : "";
const runtimeSessionId =
typeof req.body?.runtimeSessionId === "string" ? req.body.runtimeSessionId.trim() : "";
const filePath = typeof req.body?.file_path === "string" ? req.body.file_path.trim() : "";
const context = await toolContextStore.read(sessionScopeKey);
const context = runtimeSessionId ? await runtimeSessionStore.read(runtimeSessionId) : null;
if (!context) {
res.status(404).json({
message: "session context not found",
detail: sessionScopeKey,
detail: runtimeSessionId,
});
return;
}
@@ -182,10 +186,9 @@ app.post("/internal/tools/store-render-ref", async (req, res) => {
try {
const record = await resultReferenceResolver.registerRenderPayloadFile(filePath, {
actorKey: context.actorKey,
clientSessionId: context.clientSessionId,
projectId: context.projectId,
projectKey: context.projectKey,
sessionId: context.clientSessionId,
sessionId: context.sessionId,
source: "migration",
traceId: context.traceId,
});
@@ -213,14 +216,14 @@ app.post("/internal/tools/session-search", async (req, res) => {
return;
}
const sessionScopeKey =
typeof req.body?.sessionScopeKey === "string" ? req.body.sessionScopeKey : "";
const runtimeSessionId =
typeof req.body?.runtimeSessionId === "string" ? req.body.runtimeSessionId.trim() : "";
const query = typeof req.body?.query === "string" ? req.body.query : "";
const context = await toolContextStore.read(sessionScopeKey);
const context = runtimeSessionId ? await runtimeSessionStore.read(runtimeSessionId) : null;
if (!context) {
res.status(404).json({
message: "session context not found",
detail: sessionScopeKey,
detail: runtimeSessionId,
});
return;
}
@@ -264,8 +267,9 @@ const bootstrap = async () => {
memoryStore.initialize(),
resultReferenceStore.initialize(),
sessionHistoryStore.initialize(),
toolContextStore.initialize(),
runtimeSessionStore.initialize(),
]);
await conversationStore.resetStreamingSessions();
resultReferenceStore.startCleanupLoop();
};