MCPcopy Create free account
hub / github.com/bearlyai/OpenADE / describePath

Function describePath

projects/runtime-node/src/localFiles.ts:116–163  ·  view source on GitHub ↗
(params)

Source from the content-addressed store, hash-verified

114export function createRuntimeNodeLocalFilesAdapter(): RuntimeNodeFilesAdapter {
115 return {
116 async describePath(params) {
117 const record = asRecord(params)
118 const targetPath = stringValue(record, "path")
119 const maxReadSize = positiveInt(record.maxReadSize, DEFAULT_MAX_READ_SIZE)
120 const readContents = boolValue(record.readContents)
121 const showHidden = boolValue(record.showHidden)
122
123 try {
124 const stat = await fs.lstat(targetPath)
125 if (stat.isDirectory()) {
126 const names = await fs.readdir(targetPath)
127 const entries = (await Promise.all(names.filter((name) => showHidden || !name.startsWith(".")).map((name) => entryFor(targetPath, name))))
128 .filter((entry): entry is PathEntry => entry !== null)
129 .sort((a, b) => Number(b.isDir) - Number(a.isDir) || a.name.localeCompare(b.name))
130 return { type: "dir", path: targetPath, mode: stat.mode, entries }
131 }
132
133 if (stat.isFile()) {
134 const tooLarge = stat.size > maxReadSize
135 let isReadable = true
136 let metadata: FileMetadata | null = null
137 if (readContents) {
138 try {
139 metadata = classifyFileMetadata(targetPath, await readFileSample(targetPath, stat.size))
140 } catch {
141 isReadable = false
142 }
143 }
144 const content = readContents && isReadable && !tooLarge && metadata?.isBinary !== true ? await fs.readFile(targetPath, "utf8").catch(() => null) : null
145 if (readContents && isReadable && !tooLarge && metadata?.isBinary !== true && content === null) isReadable = false
146 return {
147 type: "file",
148 path: targetPath,
149 size: stat.size,
150 mode: stat.mode,
151 content,
152 tooLarge,
153 isReadable,
154 ...(metadata ? { isBinary: metadata.isBinary, mediaType: metadata.mediaType, previewKind: metadata.previewKind } : {}),
155 }
156 }
157
158 return { type: "error", path: targetPath, message: "Path is neither a file nor directory" }
159 } catch (error) {
160 if ((error as NodeJS.ErrnoException).code === "ENOENT") return { type: "not_found", path: targetPath }
161 return { type: "error", path: targetPath, message: error instanceof Error ? error.message : "Unable to read path" }
162 }
163 },
164
165 async readFile(params) {
166 const record = asRecord(params)

Callers

nothing calls this directly

Calls 10

classifyFileMetadataFunction · 0.90
positiveIntFunction · 0.85
boolValueFunction · 0.85
entryForFunction · 0.85
isDirectoryMethod · 0.80
allMethod · 0.80
readFileMethod · 0.80
asRecordFunction · 0.70
stringValueFunction · 0.70
readFileSampleFunction · 0.70

Tested by

no test coverage detected