UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

128 lines (127 loc) • 7.28 kB
import { t as asNonArrayRecord } from "./record-coerce-CVPuDqcc.mjs"; import { S as resolveSessionAgentIdStrict } from "./agent-scope-config-DmXgFGKP.mjs"; import "./agent-scope-runtime-CDpr-vor.mjs"; import { r as resolveMemorySearchIndexConfig } from "./memory-search-dZuH_5z-.mjs"; import "./memory-core-host-runtime-core-vt7Kf2j2.mjs"; //#region extensions/memory-core/src/memory-tool-contract.ts const MemorySearchSchema = { type: "object", properties: { query: { type: "string" }, maxResults: { type: "integer", minimum: 1 }, minScore: { type: "number" }, corpus: { type: "string", enum: [ "memory", "wiki", "all", "sessions" ] } }, required: ["query"], additionalProperties: false }; const MemoryGetSchema = { type: "object", properties: { path: { type: "string" }, from: { type: "integer", minimum: 1 }, lines: { type: "integer", minimum: 1 }, corpus: { type: "string", enum: [ "memory", "wiki", "all" ] } }, required: ["path"], additionalProperties: false }; function resolveMemorySourceContract(e) { const t = ["MEMORY.md, USER.md, Markdown files recursively under memory/", e.extraPaths.length > 0 ? "configured extra paths" : ""].filter(Boolean).join(", "); return { files: t, search: e.searchSources.includes("sessions") ? `${t}, indexed session transcripts` : t }; } function resolveMemoryToolContext(e) { const t = e.getConfig ? e.getConfig() : e.config; if (!t) return null; const n = resolveSessionAgentIdStrict({ sessionKey: e.agentSessionKey, config: t, agentId: e.agentId }); const r = resolveMemorySearchIndexConfig(t, n); return r ? { cfg: t, agentId: n, settings: r, sources: resolveMemorySourceContract(r) } : null; } const SEARCH_CORPUS_OUTCOME_GUIDANCE = "Corpus outcomes cover each requested corpus; only a top-level corpus warning means results are partial and must be surfaced to the user. An optional corpus outcome of not-registered in a multi-corpus result is informational."; const GET_READ_OUTCOME_GUIDANCE = "status=ok means the requested excerpt was read; status=not_found means every requested available corpus missed; status=error means the requested read failed, not that memory is disabled."; const MEMORY_SEARCH_TOOL_CONTRACT = { label: "Memory Search", name: "memory_search", parameters: MemorySearchSchema, prepareArguments: (e) => { const t = asNonArrayRecord(e); const n = [ ["min_score", "minScore"], ["max_results", "maxResults"], ["limit", "maxResults"] ]; if (!n.some(([e]) => Object.hasOwn(t, e))) return e; const r = { ...t }; for (const [e, t] of n) { if (Object.hasOwn(r, e) && !Object.hasOwn(r, t)) r[t] = r[e]; delete r[e]; } return r; }, describe: ({ search: e }) => `Semantically search ${e} when an answer depends on prior work, decisions, dates, people, preferences, or todos not already present in the current turn. Do not repeat memory retrieval when supplied context fully answers the question. Omit \`corpus\` to search those configured sources. Session results are transcript search references, not readable memory-file paths. Optional \`corpus=wiki\` or \`corpus=all\` also searches registered compiled-wiki supplements; use \`corpus=all\` only when compiled wiki supplements are needed. \`corpus=memory\` restricts hits to indexed memory files (excludes session transcript chunks from ranking). \`corpus=sessions\` searches indexed session transcripts under the same visibility rules as session history tools and returns unavailable when semantic session indexing is disabled. ${SEARCH_CORPUS_OUTCOME_GUIDANCE} If response has disabled=true or stale=true, tell the user and include the warning/action guidance.` }; const MEMORY_GET_TOOL_CONTRACT = { label: "Memory Get", name: "memory_get", parameters: MemoryGetSchema, describe: ({ files: e }) => `Safe exact excerpt read from ${e}. Session transcript paths are unsupported; use the available session-history workflow for session hits. Defaults to a bounded excerpt when lines are omitted and includes truncation/continuation info when more content exists. \`corpus=wiki\` reads registered compiled-wiki supplements. ${GET_READ_OUTCOME_GUIDANCE} ${SEARCH_CORPUS_OUTCOME_GUIDANCE}` }; function buildMemoryPromptSection({ availableTools: e, citationsMode: t }) { const n = e.has("memory_search"); const r = e.has("memory_get"); if (!n && !r) return []; const i = n ? `When an answer depends on prior work, decisions, dates, people, preferences, or todos not already present in the current turn, run memory_search without corpus to search the configured sources${r ? "; for memory-file hits, use memory_get to pull only the needed lines" : ""}. Do not repeat memory retrieval when supplied context fully answers the question. Use corpus=all only when compiled wiki supplements are needed. If low confidence after search, say you checked.` : "When an answer depends on prior work, decisions, dates, people, preferences, or todos not already present in the current turn and points to a specific memory file, run memory_get to pull only the needed lines. Do not repeat memory retrieval when supplied context fully answers the question. If low confidence after reading, say you checked."; const a = !n ? [] : [e.has("sessions_search") ? `For session hits, use sessions_search with distinctive snippet text (and sessionKey set to the transcript ID when known)${e.has("sessions_history") ? ", then sessions_history with the returned sessionKey, messageId, and sessionId for a bounded sanitized excerpt" : "; exact session history is unavailable with the enabled tools"}.` : e.has("sessions_history") ? "For session hits, use sessions_history with a known session key or transcript ID and a small limit; paginate its returned history metadata to locate the excerpt." : "Session hits are search snippets only; exact session history is unavailable with the enabled tools.", "Session search line numbers are not history offsets. Never read raw transcript files to expand session hits."]; const o = "Report recall as partial, unavailable, or stale when the result includes a top-level warning or action guidance, or explicitly sets disabled=true or stale=true. In a multi-corpus result without a top-level warning or action guidance, treat an optional corpus outcome of not-registered as informational."; const s = t === "off" ? "Citations are disabled: do not mention file paths or line numbers in replies unless the user explicitly asks." : "Citations: include Source: <path#line> when it helps the user verify memory snippets."; return [ "## Memory Recall", i, ...a, o, s, "" ]; } /** Ask agents to tag repository-specific curated entries with the run's safe project key. */ function buildProjectMemoryWriteInstruction(e) { return e && !/[\r\n<>]/u.test(e) ? [`For every repository-specific memory entry you write, add <!-- project: ${e} --> on the same line. Do not project-scope user-level preferences, standing intents, or facts that are not specific to this repository.`, ""] : []; } //#endregion export { resolveMemoryToolContext as a, buildProjectMemoryWriteInstruction as i, MEMORY_SEARCH_TOOL_CONTRACT as n, buildMemoryPromptSection as r, MEMORY_GET_TOOL_CONTRACT as t };