Support pi-internal:// scheme in read tool

Resolves pi-internal:// paths to the coding-agent package directory,
allowing the model to read internal documentation. Appends a hint
showing the filesystem path and instructing to use it for further reads.
This commit is contained in:
Mario Zechner 2026-01-16 02:31:59 +01:00
parent be26d362fa
commit 012319e15a
3 changed files with 433 additions and 2 deletions

View file

@ -1,9 +1,11 @@
import { accessSync, constants } from "node:fs";
import * as os from "node:os";
import { isAbsolute, resolve as resolvePath } from "node:path";
import { getPackageDir } from "../../config.js";
const UNICODE_SPACES = /[\u00A0\u2000-\u200A\u202F\u205F\u3000]/g;
const NARROW_NO_BREAK_SPACE = "\u202F";
export const PI_INTERNAL_SCHEME = "pi-internal://";
function normalizeUnicodeSpaces(str: string): string {
return str.replace(UNICODE_SPACES, " ");
@ -46,6 +48,12 @@ export function resolveToCwd(filePath: string, cwd: string): string {
}
export function resolveReadPath(filePath: string, cwd: string): string {
// Handle pi-internal:// scheme for Pi package documentation
if (filePath.startsWith(PI_INTERNAL_SCHEME)) {
const relativePath = filePath.slice(PI_INTERNAL_SCHEME.length);
return resolvePath(getPackageDir(), relativePath);
}
const resolved = resolveToCwd(filePath, cwd);
if (fileExists(resolved)) {

View file

@ -5,7 +5,7 @@ import { constants } from "fs";
import { access as fsAccess, readFile as fsReadFile } from "fs/promises";
import { formatDimensionNote, resizeImage } from "../../utils/image-resize.js";
import { detectSupportedImageMimeTypeFromFile } from "../../utils/mime.js";
import { resolveReadPath } from "./path-utils.js";
import { PI_INTERNAL_SCHEME, resolveReadPath } from "./path-utils.js";
import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, type TruncationResult, truncateHead } from "./truncate.js";
const readSchema = Type.Object({
@ -111,14 +111,21 @@ export function createReadTool(cwd: string, options?: ReadToolOptions): AgentToo
if (dimensionNote) {
textNote += `\n${dimensionNote}`;
}
if (path.startsWith(PI_INTERNAL_SCHEME)) {
textNote += `\n[${path} -> ${absolutePath}. Use filesystem paths for further reads.]`;
}
content = [
{ type: "text", text: textNote },
{ type: "image", data: resized.data, mimeType: resized.mimeType },
];
} else {
let textNote = `Read image file [${mimeType}]`;
if (path.startsWith(PI_INTERNAL_SCHEME)) {
textNote += `\n[${path} -> ${absolutePath}. Use filesystem paths for further reads.]`;
}
content = [
{ type: "text", text: `Read image file [${mimeType}]` },
{ type: "text", text: textNote },
{ type: "image", data: base64, mimeType },
];
}
@ -184,6 +191,11 @@ export function createReadTool(cwd: string, options?: ReadToolOptions): AgentToo
outputText = truncation.content;
}
// Add filesystem path hint for pi-internal:// paths
if (path.startsWith(PI_INTERNAL_SCHEME)) {
outputText += `\n\n[${path} -> ${absolutePath}. Use filesystem paths for further reads.]`;
}
content = [{ type: "text", text: outputText }];
}