Files
openclaw/extensions/memory-core/src/tools.shared.ts
Tak Hoffman f74983e442 fix(memory): preserve active recall tool agent context (#76380)
Summary:
- The PR threads the embedded run's trusted requester agent id into plugin tool context and memory-core tool availability/execution, adds regression tests, and records an Active Memory changelog fix.
- Reproducibility: yes. Current main shows Active Memory passing a synthetic `:active-memory:` session key plu ... ently derive memory scope from the session key; I did not run the regression test in this read-only review.

Automerge notes:
- No ClawSweeper repair was needed after automerge opt-in.

Validation:
- ClawSweeper review passed for head 33ab3d7fc7.
- Required merge gates passed before the squash merge.

Prepared head SHA: 33ab3d7fc7
Review: https://github.com/openclaw/openclaw/pull/76380#issuecomment-4365186657

Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
2026-05-03 02:16:48 +00:00

198 lines
5.6 KiB
TypeScript

import {
listMemoryCorpusSupplements,
resolveMemorySearchConfig,
resolveSessionAgentIds,
type MemoryCorpusSearchResult,
type AnyAgentTool,
type OpenClawConfig,
} from "openclaw/plugin-sdk/memory-core-host-runtime-core";
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/text-runtime";
import { Type } from "typebox";
type MemoryToolRuntime = typeof import("./tools.runtime.js");
type MemorySearchManagerResult = Awaited<
ReturnType<(typeof import("./memory/index.js"))["getMemorySearchManager"]>
>;
type MemoryToolOptions = {
config?: OpenClawConfig;
getConfig?: () => OpenClawConfig | undefined;
agentId?: string;
agentSessionKey?: string;
};
let memoryToolRuntimePromise: Promise<MemoryToolRuntime> | null = null;
export async function loadMemoryToolRuntime(): Promise<MemoryToolRuntime> {
memoryToolRuntimePromise ??= import("./tools.runtime.js");
return await memoryToolRuntimePromise;
}
export const MemorySearchSchema = Type.Object({
query: Type.String(),
maxResults: Type.Optional(Type.Number()),
minScore: Type.Optional(Type.Number()),
corpus: Type.Optional(
Type.Union([
Type.Literal("memory"),
Type.Literal("wiki"),
Type.Literal("all"),
Type.Literal("sessions"),
]),
),
});
export const MemoryGetSchema = Type.Object({
path: Type.String(),
from: Type.Optional(Type.Number()),
lines: Type.Optional(Type.Number()),
corpus: Type.Optional(
Type.Union([Type.Literal("memory"), Type.Literal("wiki"), Type.Literal("all")]),
),
});
function resolveMemoryToolContext(options: MemoryToolOptions) {
const cfg = options.getConfig?.() ?? options.config;
if (!cfg) {
return null;
}
const { sessionAgentId: agentId } = resolveSessionAgentIds({
sessionKey: options.agentSessionKey,
config: cfg,
agentId: options.agentId,
});
if (!resolveMemorySearchConfig(cfg, agentId)) {
return null;
}
return { cfg, agentId };
}
export async function getMemoryManagerContext(params: {
cfg: OpenClawConfig;
agentId: string;
}): Promise<
| {
manager: NonNullable<MemorySearchManagerResult["manager"]>;
}
| {
error: string | undefined;
}
> {
return await getMemoryManagerContextWithPurpose({ ...params, purpose: undefined });
}
export async function getMemoryManagerContextWithPurpose(params: {
cfg: OpenClawConfig;
agentId: string;
purpose?: "default" | "status" | "cli";
}): Promise<
| {
manager: NonNullable<MemorySearchManagerResult["manager"]>;
}
| {
error: string | undefined;
}
> {
const { getMemorySearchManager } = await loadMemoryToolRuntime();
const { manager, error } = await getMemorySearchManager({
cfg: params.cfg,
agentId: params.agentId,
purpose: params.purpose,
});
return manager ? { manager } : { error };
}
export function createMemoryTool(params: {
options: MemoryToolOptions;
label: string;
name: string;
description: string;
parameters: typeof MemorySearchSchema | typeof MemoryGetSchema;
execute: (ctx: { cfg: OpenClawConfig; agentId: string }) => AnyAgentTool["execute"];
}): AnyAgentTool | null {
const ctx = resolveMemoryToolContext(params.options);
if (!ctx) {
return null;
}
return {
label: params.label,
name: params.name,
description: params.description,
parameters: params.parameters,
execute: async (toolCallId, toolParams) => {
const latestCtx = resolveMemoryToolContext(params.options) ?? ctx;
return await params.execute(latestCtx)(toolCallId, toolParams);
},
};
}
export function buildMemorySearchUnavailableResult(error: string | undefined) {
const reason = (error ?? "memory search unavailable").trim() || "memory search unavailable";
const isQuotaError = /insufficient_quota|quota|429/.test(normalizeLowercaseStringOrEmpty(reason));
const warning = isQuotaError
? "Memory search is unavailable because the embedding provider quota is exhausted."
: "Memory search is unavailable due to an embedding/provider error.";
const action = isQuotaError
? "Top up or switch embedding provider, then retry memory_search."
: "Check embedding provider configuration and retry memory_search.";
return {
results: [],
disabled: true,
unavailable: true,
error: reason,
warning,
action,
debug: {
warning,
action,
error: reason,
},
};
}
export async function searchMemoryCorpusSupplements(params: {
query: string;
maxResults?: number;
agentSessionKey?: string;
corpus?: "memory" | "wiki" | "all" | "sessions";
}): Promise<MemoryCorpusSearchResult[]> {
if (params.corpus === "memory" || params.corpus === "sessions") {
return [];
}
const supplements = listMemoryCorpusSupplements();
if (supplements.length === 0) {
return [];
}
const results = (
await Promise.all(
supplements.map(async (registration) => await registration.supplement.search(params)),
)
).flat();
return results
.toSorted((left, right) => {
if (left.score !== right.score) {
return right.score - left.score;
}
return left.path.localeCompare(right.path);
})
.slice(0, Math.max(1, params.maxResults ?? 10));
}
export async function getMemoryCorpusSupplementResult(params: {
lookup: string;
fromLine?: number;
lineCount?: number;
agentSessionKey?: string;
corpus?: "memory" | "wiki" | "all" | "sessions";
}) {
if (params.corpus === "memory" || params.corpus === "sessions") {
return null;
}
for (const registration of listMemoryCorpusSupplements()) {
const result = await registration.supplement.get(params);
if (result) {
return result;
}
}
return null;
}