feat(agent): add credential refresh and unify learning tools
This commit is contained in:
@@ -1,11 +1,8 @@
|
||||
import { tool } from "@opencode-ai/plugin";
|
||||
|
||||
import { MemoryStore } from "../../src/memory/store.js";
|
||||
import { readBridgedRuntimeSessionContext } from "../../src/runtime/internalSessionContextBridge.js";
|
||||
import { setRuntimeSessionContext } from "../../src/runtime/sessionContext.js";
|
||||
|
||||
const memoryStore = new MemoryStore();
|
||||
const initializePromise = memoryStore.initialize();
|
||||
const internalBaseUrl =
|
||||
process.env.TJWATER_AGENT_INTERNAL_BASE_URL ?? "http://127.0.0.1:8787";
|
||||
const internalToken = process.env.TJWATER_AGENT_INTERNAL_TOKEN ?? "";
|
||||
|
||||
export default tool({
|
||||
description:
|
||||
@@ -25,132 +22,31 @@ export default tool({
|
||||
content: tool.schema
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
"The durable fact or preference to remember, written as one concise sentence.",
|
||||
),
|
||||
.describe("The durable fact or preference to remember, written as one concise sentence."),
|
||||
target_id: tool.schema
|
||||
.string()
|
||||
.optional()
|
||||
.describe("Stable memory entry id used by replace/remove."),
|
||||
},
|
||||
async execute(args, context) {
|
||||
await initializePromise;
|
||||
const sessionContext = await readBridgedRuntimeSessionContext(
|
||||
context.sessionID,
|
||||
);
|
||||
if (!sessionContext) {
|
||||
throw new Error(`session context not found for ${context.sessionID}`);
|
||||
}
|
||||
const scope =
|
||||
args.scope === "user"
|
||||
? "user"
|
||||
: args.scope === "workspace"
|
||||
? "workspace"
|
||||
: null;
|
||||
if (!scope) {
|
||||
return JSON.stringify({
|
||||
ok: true,
|
||||
kind: "memory",
|
||||
decision: "rejected",
|
||||
detail: `unsupported scope: ${args.scope}; use exact keyword 'user' or 'workspace'`,
|
||||
});
|
||||
}
|
||||
if (sessionContext.allowLearningWrite === false && args.action !== "list") {
|
||||
return JSON.stringify({
|
||||
ok: true,
|
||||
kind: "memory",
|
||||
decision: "rejected",
|
||||
detail: "memory writes are disabled for this session",
|
||||
});
|
||||
}
|
||||
|
||||
const scopeKey =
|
||||
scope === "user" ? sessionContext.actorKey : sessionContext.projectKey;
|
||||
if (args.action === "list") {
|
||||
const readScopes = {
|
||||
...(sessionContext.memoryListReadScopes ?? {}),
|
||||
[scope]: true,
|
||||
};
|
||||
setRuntimeSessionContext({
|
||||
...sessionContext,
|
||||
memoryListReadScopes: readScopes,
|
||||
});
|
||||
return JSON.stringify({
|
||||
ok: true,
|
||||
kind: "memory",
|
||||
decision: "accepted",
|
||||
detail: "memory listed",
|
||||
items: await memoryStore.list(scope, scopeKey),
|
||||
target: scope,
|
||||
});
|
||||
}
|
||||
|
||||
if (args.action === "add") {
|
||||
if (sessionContext.memoryListReadScopes?.[scope] !== true) {
|
||||
return JSON.stringify({
|
||||
ok: true,
|
||||
kind: "memory",
|
||||
decision: "rejected",
|
||||
detail: `must list ${scope} memory and review existing entries before add`,
|
||||
target: scope,
|
||||
});
|
||||
}
|
||||
const result = await memoryStore.upsert(scope, scopeKey, {
|
||||
content: args.content ?? "",
|
||||
sessionId: sessionContext.clientSessionId,
|
||||
source: "tool",
|
||||
traceId: sessionContext.traceId,
|
||||
});
|
||||
if (!result.entry) {
|
||||
return JSON.stringify({
|
||||
ok: true,
|
||||
kind: "memory",
|
||||
decision: "rejected",
|
||||
detail: "content rejected by persistence policy",
|
||||
});
|
||||
}
|
||||
return JSON.stringify({
|
||||
ok: true,
|
||||
kind: "memory",
|
||||
decision: result.changed ? "accepted" : "deduped",
|
||||
detail: result.detail,
|
||||
entry: result.entry,
|
||||
target: scope,
|
||||
});
|
||||
}
|
||||
|
||||
if (args.action === "replace") {
|
||||
const result = await memoryStore.replace(
|
||||
scope,
|
||||
scopeKey,
|
||||
args.target_id ?? "",
|
||||
{
|
||||
content: args.content ?? "",
|
||||
sessionId: sessionContext.clientSessionId,
|
||||
source: "tool",
|
||||
traceId: sessionContext.traceId,
|
||||
const response = await fetch(
|
||||
`${internalBaseUrl}/internal/tools/memory-manager`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"x-agent-internal-token": internalToken,
|
||||
},
|
||||
);
|
||||
return JSON.stringify({
|
||||
ok: true,
|
||||
kind: "memory",
|
||||
decision: result.changed ? "accepted" : "rejected",
|
||||
detail: result.detail,
|
||||
target: scope,
|
||||
});
|
||||
}
|
||||
|
||||
const result = await memoryStore.remove(
|
||||
scope,
|
||||
scopeKey,
|
||||
args.target_id ?? "",
|
||||
body: JSON.stringify({
|
||||
...args,
|
||||
session_id: context.sessionID,
|
||||
}),
|
||||
},
|
||||
);
|
||||
return JSON.stringify({
|
||||
ok: true,
|
||||
kind: "memory",
|
||||
decision: result.changed ? "accepted" : "rejected",
|
||||
detail: result.detail,
|
||||
target: scope,
|
||||
});
|
||||
const text = await response.text();
|
||||
if (!response.ok) {
|
||||
throw new Error(text);
|
||||
}
|
||||
return text;
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user