MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / execution

Method execution

packages/agent-core/src/tools/builtin/file/read.ts:264–323  ·  view source on GitHub ↗
(args: ReadInput, safePath: string)

Source from the content-addressed store, hash-verified

262 }
263
264 private async execution(args: ReadInput, safePath: string): Promise<ExecutableToolResult> {
265 try {
266 let stat: StatResult;
267 try {
268 stat = await this.kaos.stat(safePath);
269 } catch (error) {
270 if (isFileNotFoundError(error)) {
271 return { isError: true, output: `"${args.path}" does not exist.` };
272 }
273 throw error;
274 }
275 if (!isRegularFileMode(stat.stMode)) {
276 return { isError: true, output: `"${args.path}" is not a file.` };
277 }
278
279 const header = await readTextHeader(this.kaos, safePath, MEDIA_SNIFF_BYTES);
280 const fileType = detectFileType(safePath, header);
281 if (fileType.kind === 'image' || fileType.kind === 'video') {
282 return {
283 isError: true,
284 output: `"${args.path}" is a ${fileType.kind} file. Use ReadMediaFile to read image or video files.`,
285 };
286 }
287 if (fileType.kind === 'unknown') {
288 return {
289 isError: true,
290 output: notReadableFileOutput(args.path),
291 };
292 }
293
294 const lineOffset = args.line_offset ?? 1;
295 const requestedLines = args.n_lines ?? MAX_LINES;
296 const effectiveLimit = Math.min(requestedLines, MAX_LINES);
297
298 if (lineOffset < 0) {
299 return await this.readTail(
300 safePath,
301 args.path,
302 lineOffset,
303 effectiveLimit,
304 requestedLines,
305 );
306 }
307 return await this.readForward(
308 safePath,
309 args.path,
310 lineOffset,
311 effectiveLimit,
312 requestedLines,
313 );
314 } catch (error) {
315 if (isTextDecodeError(error)) {
316 return { isError: true, output: notReadableFileOutput(args.path) };
317 }
318 return {
319 isError: true,
320 output: error instanceof Error ? error.message : String(error),
321 };

Callers 1

resolveExecutionMethod · 0.95

Calls 9

readTailMethod · 0.95
readForwardMethod · 0.95
detectFileTypeFunction · 0.90
isFileNotFoundErrorFunction · 0.85
isRegularFileModeFunction · 0.85
readTextHeaderFunction · 0.85
notReadableFileOutputFunction · 0.85
isTextDecodeErrorFunction · 0.85
statMethod · 0.65

Tested by

no test coverage detected