Memory: share ENOENT helpers

This commit is contained in:
Vignesh Natarajan
2026-02-19 23:04:26 -08:00
committed by Vignesh
parent 14a3af212d
commit 5542a43623
11 changed files with 245 additions and 43 deletions

View File

@@ -7,6 +7,7 @@ import { resolveAgentWorkspaceDir } from "../agents/agent-scope.js";
import type { OpenClawConfig } from "../config/config.js";
import { resolveStateDir } from "../config/paths.js";
import { createSubsystemLogger } from "../logging/subsystem.js";
import { isFileMissingError, statRegularFile } from "./fs-utils.js";
import { deriveQmdScopeChannel, deriveQmdScopeChatType, isQmdScopeAllowed } from "./qmd-scope.js";
import {
listSessionFilesForAgent,
@@ -493,19 +494,25 @@ export class QmdMemoryManager implements MemorySearchManager {
if (!absPath.endsWith(".md")) {
throw new Error("path required");
}
const stat = await fs.lstat(absPath);
if (stat.isSymbolicLink() || !stat.isFile()) {
throw new Error("path required");
const statResult = await statRegularFile(absPath);
if (statResult.missing) {
return { text: "", path: relPath };
}
if (params.from !== undefined || params.lines !== undefined) {
const text = await this.readPartialText(absPath, params.from, params.lines);
return { text, path: relPath };
const partial = await this.readPartialText(absPath, params.from, params.lines);
if (partial.missing) {
return { text: "", path: relPath };
}
return { text: partial.text, path: relPath };
}
const full = await this.readFullText(absPath);
if (full.missing) {
return { text: "", path: relPath };
}
const content = await fs.readFile(absPath, "utf-8");
if (!params.from && !params.lines) {
return { text: content, path: relPath };
return { text: full.text, path: relPath };
}
const lines = content.split("\n");
const lines = full.text.split("\n");
const start = Math.max(1, params.from ?? 1);
const count = Math.max(1, params.lines ?? lines.length);
const slice = lines.slice(start - 1, start - 1 + count);
@@ -764,10 +771,22 @@ export class QmdMemoryManager implements MemorySearchManager {
});
}
private async readPartialText(absPath: string, from?: number, lines?: number): Promise<string> {
private async readPartialText(
absPath: string,
from?: number,
lines?: number,
): Promise<{ missing: true } | { missing: false; text: string }> {
const start = Math.max(1, from ?? 1);
const count = Math.max(1, lines ?? Number.POSITIVE_INFINITY);
const handle = await fs.open(absPath);
let handle;
try {
handle = await fs.open(absPath);
} catch (err) {
if (isFileMissingError(err)) {
return { missing: true };
}
throw err;
}
const stream = handle.createReadStream({ encoding: "utf-8" });
const rl = readline.createInterface({
input: stream,
@@ -790,7 +809,21 @@ export class QmdMemoryManager implements MemorySearchManager {
rl.close();
await handle.close();
}
return selected.slice(0, count).join("\n");
return { missing: false, text: selected.slice(0, count).join("\n") };
}
private async readFullText(
absPath: string,
): Promise<{ missing: true } | { missing: false; text: string }> {
try {
const text = await fs.readFile(absPath, "utf-8");
return { missing: false, text };
} catch (err) {
if (isFileMissingError(err)) {
return { missing: true };
}
throw err;
}
}
private ensureDb(): SqliteDatabase {