mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-26 16:06:16 +00:00
fix(memory): use explicit qmd snippet line metadata (#58181)
* fix(memory): preserve qmd snippet line metadata * Memory/QMD: preserve snippet span with partial line metadata
This commit is contained in:
@@ -24,6 +24,22 @@ complete`,
|
||||
expect(results).toEqual([{ docid: "abc", score: 0.5 }]);
|
||||
});
|
||||
|
||||
it("preserves explicit qmd line metadata when present", () => {
|
||||
const results = parseQmdQueryJson(
|
||||
'[{"docid":"abc","score":0.5,"start_line":4,"end_line":6,"snippet":"@@ -10,1\\nignored"}]',
|
||||
"",
|
||||
);
|
||||
expect(results).toEqual([
|
||||
{
|
||||
docid: "abc",
|
||||
score: 0.5,
|
||||
snippet: "@@ -10,1\nignored",
|
||||
startLine: 4,
|
||||
endLine: 6,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("treats plain-text no-results from stderr as an empty result set", () => {
|
||||
const results = parseQmdQueryJson("", "No results found\n");
|
||||
expect(results).toEqual([]);
|
||||
|
||||
@@ -9,6 +9,8 @@ export type QmdQueryResult = {
|
||||
file?: string;
|
||||
snippet?: string;
|
||||
body?: string;
|
||||
startLine?: number;
|
||||
endLine?: number;
|
||||
};
|
||||
|
||||
export function parseQmdQueryJson(stdout: string, stderr: string): QmdQueryResult[] {
|
||||
@@ -73,12 +75,40 @@ function parseQmdQueryResultArray(raw: string): QmdQueryResult[] | null {
|
||||
if (!Array.isArray(parsed)) {
|
||||
return null;
|
||||
}
|
||||
return parsed as QmdQueryResult[];
|
||||
return parsed.map((item) => {
|
||||
if (typeof item !== "object" || item === null) {
|
||||
return item as QmdQueryResult;
|
||||
}
|
||||
const record = item as Record<string, unknown>;
|
||||
const docid = typeof record.docid === "string" ? record.docid : undefined;
|
||||
const score =
|
||||
typeof record.score === "number" && Number.isFinite(record.score)
|
||||
? record.score
|
||||
: undefined;
|
||||
const collection = typeof record.collection === "string" ? record.collection : undefined;
|
||||
const file = typeof record.file === "string" ? record.file : undefined;
|
||||
const snippet = typeof record.snippet === "string" ? record.snippet : undefined;
|
||||
const body = typeof record.body === "string" ? record.body : undefined;
|
||||
return {
|
||||
docid,
|
||||
score,
|
||||
collection,
|
||||
file,
|
||||
snippet,
|
||||
body,
|
||||
startLine: parseQmdLineNumber(record.start_line ?? record.startLine),
|
||||
endLine: parseQmdLineNumber(record.end_line ?? record.endLine),
|
||||
} as QmdQueryResult;
|
||||
});
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function parseQmdLineNumber(value: unknown): number | undefined {
|
||||
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined;
|
||||
}
|
||||
|
||||
function extractFirstJsonArray(raw: string): string | null {
|
||||
const start = raw.indexOf("[");
|
||||
if (start < 0) {
|
||||
|
||||
Reference in New Issue
Block a user