mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 23:44:33 +00:00
Memory: clamp QMD citations to injected budget
This commit is contained in:
committed by
Vignesh
parent
c248da0317
commit
1861e76360
@@ -2,6 +2,7 @@ import { Type } from "@sinclair/typebox";
|
|||||||
|
|
||||||
import type { MoltbotConfig } from "../../config/config.js";
|
import type { MoltbotConfig } from "../../config/config.js";
|
||||||
import type { MemoryCitationsMode } from "../../config/types.memory.js";
|
import type { MemoryCitationsMode } from "../../config/types.memory.js";
|
||||||
|
import { resolveMemoryBackendConfig } from "../../memory/backend-config.js";
|
||||||
import { getMemorySearchManager } from "../../memory/index.js";
|
import { getMemorySearchManager } from "../../memory/index.js";
|
||||||
import type { MemorySearchResult } from "../../memory/types.js";
|
import type { MemorySearchResult } from "../../memory/types.js";
|
||||||
import { resolveSessionAgentId } from "../agent-scope.js";
|
import { resolveSessionAgentId } from "../agent-scope.js";
|
||||||
@@ -61,7 +62,12 @@ export function createMemorySearchTool(options: {
|
|||||||
sessionKey: options.agentSessionKey,
|
sessionKey: options.agentSessionKey,
|
||||||
});
|
});
|
||||||
const status = manager.status();
|
const status = manager.status();
|
||||||
const results = decorateCitations(rawResults, includeCitations);
|
const decorated = decorateCitations(rawResults, includeCitations);
|
||||||
|
const resolved = resolveMemoryBackendConfig({ cfg, agentId });
|
||||||
|
const results =
|
||||||
|
status.backend === "qmd"
|
||||||
|
? clampResultsByInjectedChars(decorated, resolved.qmd?.limits.maxInjectedChars)
|
||||||
|
: decorated;
|
||||||
return jsonResult({
|
return jsonResult({
|
||||||
results,
|
results,
|
||||||
provider: status.provider,
|
provider: status.provider,
|
||||||
@@ -145,6 +151,28 @@ function formatCitation(entry: MemorySearchResult): string {
|
|||||||
return `${entry.path}${lineRange}`;
|
return `${entry.path}${lineRange}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function clampResultsByInjectedChars(
|
||||||
|
results: MemorySearchResult[],
|
||||||
|
budget?: number,
|
||||||
|
): MemorySearchResult[] {
|
||||||
|
if (!budget || budget <= 0) return results;
|
||||||
|
let remaining = budget;
|
||||||
|
const clamped: MemorySearchResult[] = [];
|
||||||
|
for (const entry of results) {
|
||||||
|
if (remaining <= 0) break;
|
||||||
|
const snippet = entry.snippet ?? "";
|
||||||
|
if (snippet.length <= remaining) {
|
||||||
|
clamped.push(entry);
|
||||||
|
remaining -= snippet.length;
|
||||||
|
} else {
|
||||||
|
const trimmed = snippet.slice(0, Math.max(0, remaining));
|
||||||
|
clamped.push({ ...entry, snippet: trimmed });
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return clamped;
|
||||||
|
}
|
||||||
|
|
||||||
function shouldIncludeCitations(params: {
|
function shouldIncludeCitations(params: {
|
||||||
mode: MemoryCitationsMode;
|
mode: MemoryCitationsMode;
|
||||||
sessionKey?: string;
|
sessionKey?: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user