重构会话管理功能,由后端 opencode 发放 sessionId,后端做 scope

This commit is contained in:
2026-05-21 15:41:46 +08:00
parent 7e63d38cf5
commit 5d80961930
20 changed files with 816 additions and 390 deletions
+9 -1
View File
@@ -1,7 +1,10 @@
import { tool } from "@opencode-ai/plugin";
import { ToolSessionContextStore } from "../../src/session/toolContextStore.js";
const internalBaseUrl = process.env.TJWATER_AGENT_INTERNAL_BASE_URL ?? "http://127.0.0.1:8787";
const internalToken = process.env.TJWATER_AGENT_INTERNAL_TOKEN ?? "";
const toolContextStore = new ToolSessionContextStore();
const initializePromise = toolContextStore.initialize();
export default tool({
description:
@@ -21,6 +24,11 @@ export default tool({
.describe("Query arguments object."),
},
async execute(args, context) {
await initializePromise;
const sessionContext = await toolContextStore.read(context.sessionID);
if (!sessionContext) {
throw new Error(`session context not found for ${context.sessionID}`);
}
// 工具本身不直接持有用户 token;通过 sessionID 回调 Agent 服务,由服务侧补齐用户上下文。
const response = await fetch(`${internalBaseUrl}/internal/tools/dynamic-http-call`, {
method: "POST",
@@ -29,7 +37,7 @@ export default tool({
"x-agent-internal-token": internalToken,
},
body: JSON.stringify({
sessionId: context.sessionID,
sessionScopeKey: sessionContext.sessionScopeKey,
reason: args.reason,
path: args.path,
method: args.method,
+9 -1
View File
@@ -1,7 +1,10 @@
import { tool } from "@opencode-ai/plugin";
import { ToolSessionContextStore } from "../../src/session/toolContextStore.js";
const internalBaseUrl = process.env.TJWATER_AGENT_INTERNAL_BASE_URL ?? "http://127.0.0.1:8787";
const internalToken = process.env.TJWATER_AGENT_INTERNAL_TOKEN ?? "";
const toolContextStore = new ToolSessionContextStore();
const initializePromise = toolContextStore.initialize();
export default tool({
description:
@@ -19,6 +22,11 @@ export default tool({
.describe("Optional maximum number of top-level items or fields to return."),
},
async execute(args, context) {
await initializePromise;
const sessionContext = await toolContextStore.read(context.sessionID);
if (!sessionContext) {
throw new Error(`session context not found for ${context.sessionID}`);
}
const response = await fetch(`${internalBaseUrl}/internal/tools/fetch-result-ref`, {
method: "POST",
headers: {
@@ -26,7 +34,7 @@ export default tool({
"x-agent-internal-token": internalToken,
},
body: JSON.stringify({
sessionId: context.sessionID,
sessionScopeKey: sessionContext.sessionScopeKey,
result_ref: args.result_ref,
max_items: args.max_items,
}),
+2 -2
View File
@@ -80,7 +80,7 @@ export default tool({
if (args.action === "add") {
const result = await memoryStore.upsert(scope, scopeKey, {
content: args.content ?? "",
sessionId: context.sessionID,
sessionId: sessionContext.clientSessionId,
source: "tool",
traceId: sessionContext.traceId,
});
@@ -105,7 +105,7 @@ export default tool({
if (args.action === "replace") {
const result = await memoryStore.replace(scope, scopeKey, args.target_id ?? "", {
content: args.content ?? "",
sessionId: context.sessionID,
sessionId: sessionContext.clientSessionId,
source: "tool",
traceId: sessionContext.traceId,
});
+9 -1
View File
@@ -1,8 +1,11 @@
import { tool } from "@opencode-ai/plugin";
import { ToolSessionContextStore } from "../../src/session/toolContextStore.js";
const internalBaseUrl =
process.env.TJWATER_AGENT_INTERNAL_BASE_URL ?? "http://127.0.0.1:8787";
const internalToken = process.env.TJWATER_AGENT_INTERNAL_TOKEN ?? "";
const toolContextStore = new ToolSessionContextStore();
const initializePromise = toolContextStore.initialize();
export default tool({
description:
@@ -22,6 +25,11 @@ export default tool({
.describe("Optional maximum number of hits to return."),
},
async execute(args, context) {
await initializePromise;
const sessionContext = await toolContextStore.read(context.sessionID);
if (!sessionContext) {
throw new Error(`session context not found for ${context.sessionID}`);
}
const response = await fetch(`${internalBaseUrl}/internal/tools/session-search`, {
method: "POST",
headers: {
@@ -31,7 +39,7 @@ export default tool({
body: JSON.stringify({
max_results: args.max_results,
query: args.query,
sessionId: context.sessionID,
sessionScopeKey: sessionContext.sessionScopeKey,
}),
});
const text = await response.text();
+9 -1
View File
@@ -1,7 +1,10 @@
import { tool } from "@opencode-ai/plugin";
import { ToolSessionContextStore } from "../../src/session/toolContextStore.js";
const internalBaseUrl = process.env.TJWATER_AGENT_INTERNAL_BASE_URL ?? "http://127.0.0.1:8787";
const internalToken = process.env.TJWATER_AGENT_INTERNAL_TOKEN ?? "";
const toolContextStore = new ToolSessionContextStore();
const initializePromise = toolContextStore.initialize();
export default tool({
description:
@@ -15,6 +18,11 @@ export default tool({
.describe("Absolute path to a local JSON file containing the render payload or a wrapper object with data."),
},
async execute(args, context) {
await initializePromise;
const sessionContext = await toolContextStore.read(context.sessionID);
if (!sessionContext) {
throw new Error(`session context not found for ${context.sessionID}`);
}
const response = await fetch(`${internalBaseUrl}/internal/tools/store-render-ref`, {
method: "POST",
headers: {
@@ -22,7 +30,7 @@ export default tool({
"x-agent-internal-token": internalToken,
},
body: JSON.stringify({
sessionId: context.sessionID,
sessionScopeKey: sessionContext.sessionScopeKey,
file_path: args.file_path,
}),
});