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 { MemoryCitationsMode } from "../../config/types.memory.js";
|
||||
import { resolveMemoryBackendConfig } from "../../memory/backend-config.js";
|
||||
import { getMemorySearchManager } from "../../memory/index.js";
|
||||
import type { MemorySearchResult } from "../../memory/types.js";
|
||||
import { resolveSessionAgentId } from "../agent-scope.js";
|
||||
@@ -61,7 +62,12 @@ export function createMemorySearchTool(options: {
|
||||
sessionKey: options.agentSessionKey,
|
||||
});
|
||||
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({
|
||||
results,
|
||||
provider: status.provider,
|
||||
@@ -145,6 +151,28 @@ function formatCitation(entry: MemorySearchResult): string {
|
||||
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: {
|
||||
mode: MemoryCitationsMode;
|
||||
sessionKey?: string;
|
||||
|
||||
Reference in New Issue
Block a user