Download src/memory/root-memory-files.ts from SaylorTwift/openclaw: direct link, hf CLI and curl.
- Browser
- Download file 2.8 kB
-
https://huggingface.co/SaylorTwift/openclaw/resolve/main/src/memory/root-memory-files.ts
- Command line
-
hf download hf://SaylorTwift/openclaw/src/memory/root-memory-files.ts
-
curl -L -o root-memory-files.ts https://huggingface.co/SaylorTwift/openclaw/resolve/main/src/memory/root-memory-files.ts
2.8 kB
| // Locates root memory files that seed agent context. | |
| import fs from "node:fs/promises"; | |
| import path from "node:path"; | |
| import { isMissingPathError } from "../infra/errno.js"; | |
| /** Canonical root memory file name used by current workspaces. */ | |
| export const CANONICAL_ROOT_MEMORY_FILENAME = "MEMORY.md"; | |
| /** Legacy root memory file name kept out of auxiliary scans. */ | |
| export const LEGACY_ROOT_MEMORY_FILENAME = "memory.md"; | |
| const ROOT_MEMORY_REPAIR_RELATIVE_DIR = ".openclaw-repair/root-memory"; | |
| /** Resolves the canonical root memory file path for a workspace. */ | |
| export function resolveCanonicalRootMemoryPath(workspaceDir: string): string { | |
| return path.join(workspaceDir, CANONICAL_ROOT_MEMORY_FILENAME); | |
| } | |
| /** Resolves the legacy root memory file path for a workspace. */ | |
| export function resolveLegacyRootMemoryPath(workspaceDir: string): string { | |
| return path.join(workspaceDir, LEGACY_ROOT_MEMORY_FILENAME); | |
| } | |
| /** Resolves the repair directory used while migrating root memory files. */ | |
| export function resolveRootMemoryRepairDir(workspaceDir: string): string { | |
| return path.join(workspaceDir, ".openclaw-repair", "root-memory"); | |
| } | |
| function normalizeWorkspaceRelativePath(value: string): string { | |
| return value.trim().replace(/\\/g, "/").replace(/^\.\//, ""); | |
| } | |
| /** Checks for an exact directory entry without case-folded path lookup. */ | |
| export async function exactWorkspaceEntryExists(dir: string, name: string): Promise<boolean> { | |
| try { | |
| const entries = await fs.readdir(dir); | |
| return entries.includes(name); | |
| } catch { | |
| return false; | |
| } | |
| } | |
| /** Resolves the canonical root memory file only when it is a real file, not a symlink. */ | |
| export async function resolveCanonicalRootMemoryFile(workspaceDir: string): Promise<string | null> { | |
| try { | |
| const entries = await fs.readdir(workspaceDir, { withFileTypes: true }); | |
| for (const entry of entries) { | |
| if ( | |
| entry.name === CANONICAL_ROOT_MEMORY_FILENAME && | |
| entry.isFile() && | |
| !entry.isSymbolicLink() | |
| ) { | |
| return path.join(workspaceDir, entry.name); | |
| } | |
| } | |
| } catch (error) { | |
| if (!isMissingPathError(error)) { | |
| throw error; | |
| } | |
| } | |
| return null; | |
| } | |
| /** Skips legacy/repair root memory paths when scanning workspace memory files. */ | |
| export function shouldSkipRootMemoryAuxiliaryPath(params: { | |
| workspaceDir: string; | |
| absPath: string; | |
| }): boolean { | |
| const relative = path.relative(params.workspaceDir, params.absPath); | |
| if (relative.startsWith("..") || path.isAbsolute(relative)) { | |
| return false; | |
| } | |
| const normalized = normalizeWorkspaceRelativePath(relative); | |
| return ( | |
| normalized === LEGACY_ROOT_MEMORY_FILENAME || | |
| normalized === ROOT_MEMORY_REPAIR_RELATIVE_DIR || | |
| normalized.startsWith(`${ROOT_MEMORY_REPAIR_RELATIVE_DIR}/`) | |
| ); | |
| } | |