@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
180 lines • 10.2 kB
JavaScript
import { selectedReadFolder } from "@earendil-works/pi-agent-core/read-folders";
import { Type } from "typebox";
import { detectSupportedImageMimeType, IMAGE_TYPE_SNIFF_BYTES } from "../../utils/image-mime.js";
import { decodeUtf8, encodeBase64 } from "./encoding.js";
import { lexicalCanonicalPath, resolvePosixToolPath, resolveReadPathVariants, } from "./file-tool-host.js";
import { wrapToolDefinition } from "./tool-definition-wrapper.js";
import { shapeTextRead } from "./read-text.js";
import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES } from "./truncate.js";
const readSchema = Type.Object({
path: Type.String({ description: "Path to the file to read (relative or absolute)" }),
offset: Type.Optional(Type.Number({ description: "Line number to start reading from (1-indexed)" })),
limit: Type.Optional(Type.Number({ description: "Maximum number of lines to read" })),
});
export const readToolSystemPromptContribution = {
snippet: "Read file contents",
guidelines: ["Files: read, not cat/sed."],
fullGuidelines: ["Use read to examine files instead of cat or sed."],
};
const LOCAL_URI_SCHEME = /^local:\/\//i;
const LOCAL_URI_GUIDANCE = "local:// URIs resolve only inside eval cells via the kernel read()/write() helpers; the read tool takes filesystem paths. Re-read this with the eval read() helper, or retry with the plain absolute file path.";
const INLINE_IMAGE_TYPES = new Set(["image/png", "image/jpeg", "image/gif", "image/webp"]);
const inlineImageAsIs = async (bytes, mimeType) => INLINE_IMAGE_TYPES.has(mimeType)
? { ok: true, data: encodeBase64(bytes), mimeType, hints: [] }
: { ok: false, message: "[Image omitted: could not be converted to a supported inline image format.]" };
function machineReadPath(machine) {
const exists = (path) => machine.ops.files.stat({ path }, machine.context(undefined)).then(() => true, () => false);
return (path, cwd) => resolveReadPathVariants(path, cwd, resolvePosixToolPath, exists);
}
/** The default read operations: the shared operation layer, called with the context of one admitted call. */
function machineReadOperations(machine, signal, detectImageMimeType) {
const ctx = machine.context(signal);
return {
readFile: async (path) => (await machine.ops.files.read({ path }, ctx)).bytes,
access: async (path) => {
await machine.ops.files.stat({ path, access: "read" }, ctx);
},
detectImageMimeType: detectImageMimeType ??
(async (path) => {
const head = await machine.ops.files.read({ path, offset: 0, limitBytes: IMAGE_TYPE_SNIFF_BYTES }, ctx);
return detectSupportedImageMimeType(head.bytes);
}),
};
}
function getNonVisionImageNote(model) {
if (!model || model.input.includes("image")) {
return undefined;
}
return "[Current model does not support images. The image will be omitted from this request.]";
}
export function createCoreReadToolDefinition(cwd, options) {
const autoResizeImages = options.autoResizeImages ?? true;
const fallbackResizeOptions = options.resizeOptions;
const contextModel = options.contextModel ?? true;
const customOps = options.operations;
const machine = options.machine;
const filesystemPolicy = options.filesystemPolicy;
const resolveReadPath = options.resolveReadPath ?? machineReadPath(machine);
const canonicalizePath = options.canonicalizePath ?? lexicalCanonicalPath;
const processImage = options.processImage ?? inlineImageAsIs;
return {
name: "read",
workspaceEffect: "read",
label: "read",
description: `Read the contents of a file. Supports text files and images (jpg, png, gif, webp, bmp). Images are sent as attachments. For text files, output is truncated to ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). Use offset/limit for large files. When you need the full file, continue with offset until complete. A .js or .json file of 100+ lines read without offset/limit may come back folded: bodies are replaced by markers and a footer lists the offset/limit ranges to reread; markers are not source, so reread those ranges before quoting or editing them.`,
promptSnippet: readToolSystemPromptContribution.snippet,
promptGuidelines: [...readToolSystemPromptContribution.guidelines],
fullPromptGuidelines: [...readToolSystemPromptContribution.fullGuidelines],
parameters: readSchema,
constrainedSampling: { type: "json_schema", strict: "prefer" },
async execute(_toolCallId, { path, offset, limit }, signal, _onUpdate, ctx) {
if (LOCAL_URI_SCHEME.test(path)) {
throw new Error(LOCAL_URI_GUIDANCE);
}
return new Promise((resolve, reject) => {
if (signal?.aborted) {
reject(new Error("Operation aborted"));
return;
}
let aborted = false;
const onAbort = () => {
aborted = true;
reject(new Error("Operation aborted"));
};
signal?.addEventListener("abort", onAbort, { once: true });
(async () => {
try {
const admitted = machine.begin("files.read", { path, offset, limit }, signal);
if (admitted !== undefined)
await admitted;
if (aborted)
return;
const ops = customOps ?? machineReadOperations(machine, signal, options.detectImageMimeType);
const absolutePath = await resolveReadPath(path, ctx?.cwd || cwd);
if (aborted)
return;
if (filesystemPolicy) {
const decision = await filesystemPolicy({
operation: "read",
canonicalPath: await canonicalizePath(absolutePath),
toolName: "read",
});
if (!decision.allow)
throw new Error(decision.reason);
if (aborted)
return;
}
// Check if file exists and is readable.
await ops.access(absolutePath);
if (aborted)
return;
const mimeType = ops.detectImageMimeType ? await ops.detectImageMimeType(absolutePath) : undefined;
let content;
let details;
const model = contextModel ? ctx?.model : undefined;
const nonVisionImageNote = getNonVisionImageNote(model);
if (mimeType) {
// Read image as binary.
const buffer = await ops.readFile(absolutePath);
const processed = await processImage(buffer, mimeType, {
autoResizeImages,
resizeOptions: model?.inputLimits?.images?.resize ?? fallbackResizeOptions,
});
if (!processed.ok) {
let textNote = `Read image file [${mimeType}]\n${processed.message}`;
if (nonVisionImageNote)
textNote += `\n${nonVisionImageNote}`;
content = [{ type: "text", text: textNote }];
}
else {
let textNote = `Read image file [${processed.mimeType}]`;
if (processed.hints.length > 0)
textNote += `\n${processed.hints.join("\n")}`;
if (nonVisionImageNote)
textNote += `\n${nonVisionImageNote}`;
content = [
{ type: "text", text: textNote },
{ type: "image", data: processed.data, mimeType: processed.mimeType },
];
}
}
else {
// Read text content.
const buffer = await ops.readFile(absolutePath);
if (aborted)
return;
const shaped = await shapeTextRead({
path,
absolutePath,
textContent: decodeUtf8(buffer),
offset,
limit,
folder: options.folder,
isAborted: () => aborted,
});
if (shaped === undefined)
return;
content = shaped.content;
details = shaped.details;
}
if (aborted)
return;
signal?.removeEventListener("abort", onAbort);
resolve({ content, details });
}
catch (error) {
signal?.removeEventListener("abort", onAbort);
if (!aborted)
reject(error);
}
})();
});
},
};
}
/** The read tool for a host whose only effect channel is `machine`; reads fold like the local tool's by default. */
export function createCoreReadTool(cwd, options) {
return wrapToolDefinition(createCoreReadToolDefinition(cwd, { folder: selectedReadFolder, ...options }));
}
export { readSchema };
//# sourceMappingURL=read-core.js.map