feat(files): open markdown, HTML, and PDF files outside the workspace (#9140)
Co-authored-by: Claude Code <[email protected]>
This commit is contained in:
1 parent
f2a914b858
commit
f46a709ee4
26 files changed
+394
-89
No files matched your search
@@ -3,10 +3,13 @@
|
||||
* WorkspaceFileSystem - Effect service contract for workspace file mutations.
|
||||
*
|
||||
* Owns workspace-root-relative file read/write operations and their associated
|
||||
* safety checks and cache invalidation hooks.
|
||||
* safety checks and cache invalidation hooks. Reads also accept absolute host
|
||||
* paths so clients can show files an agent left outside the workspace; writes
|
||||
* never leave the root.
|
||||
*
|
||||
* @module WorkspaceFileSystem
|
||||
*/
|
||||
import * as NodeFS from "node:fs";
|
||||
import * as NodeFSP from "node:fs/promises";
|
||||
|
||||
import type {
|
||||
@@ -104,7 +107,10 @@ export type WorkspaceFileSystemError = typeof WorkspaceFileSystemError.Type;
|
||||
export class WorkspaceFileSystem extends Context.Service<
|
||||
WorkspaceFileSystem,
|
||||
{
|
||||
/** Read a UTF-8 text file relative to the workspace root. */
|
||||
/**
|
||||
* Read a UTF-8 text file relative to the workspace root, or any host file by
|
||||
* absolute path.
|
||||
*/
|
||||
readonly readFile: (
|
||||
input: ProjectReadFileInput,
|
||||
) => Effect.Effect<
|
||||
@@ -132,9 +138,31 @@ export const make = Effect.gen(function* () {
|
||||
const workspacePaths = yield* WorkspacePaths.WorkspacePaths;
|
||||
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
|
||||
|
||||
const readFile: WorkspaceFileSystem["Service"]["readFile"] = Effect.fn(
|
||||
"WorkspaceFileSystem.readFile",
|
||||
)(function* (input) {
|
||||
/**
|
||||
* Resolves the file a read targets. Workspace-relative paths must stay inside the
|
||||
* root, symlinks included. An absolute path reads a host file in place, such as a
|
||||
* report an agent wrote to a temp directory; it gets no root check.
|
||||
*/
|
||||
const resolveReadTarget = Effect.fn("WorkspaceFileSystem.resolveReadTarget")(function* (
|
||||
input: ProjectReadFileInput,
|
||||
) {
|
||||
const requestedPath = input.relativePath.trim();
|
||||
if (path.isAbsolute(requestedPath)) {
|
||||
const realTargetPath = yield* Effect.tryPromise({
|
||||
try: () => NodeFSP.realpath(requestedPath),
|
||||
catch: (cause) =>
|
||||
new WorkspaceFileSystemOperationError({
|
||||
workspaceRoot: input.cwd,
|
||||
relativePath: input.relativePath,
|
||||
resolvedPath: requestedPath,
|
||||
operationPath: requestedPath,
|
||||
operation: "realpath-target",
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
return { relativePath: requestedPath, realTargetPath };
|
||||
}
|
||||
|
||||
const target = yield* workspacePaths.resolveRelativePathWithinRoot({
|
||||
workspaceRoot: input.cwd,
|
||||
relativePath: input.relativePath,
|
||||
@@ -177,10 +205,24 @@ export const make = Effect.gen(function* () {
|
||||
resolvedPath: realTargetPath,
|
||||
});
|
||||
}
|
||||
return { relativePath: target.relativePath, realTargetPath };
|
||||
});
|
||||
|
||||
const readFile: WorkspaceFileSystem["Service"]["readFile"] = Effect.fn(
|
||||
"WorkspaceFileSystem.readFile",
|
||||
)(function* (input) {
|
||||
const target = yield* resolveReadTarget(input);
|
||||
const realTargetPath = target.realTargetPath;
|
||||
|
||||
return yield* Effect.acquireUseRelease(
|
||||
Effect.tryPromise({
|
||||
try: () => NodeFSP.open(realTargetPath, "r"),
|
||||
// Non-blocking so a FIFO cannot hang the open; the stat below rejects
|
||||
// it. Regular files ignore the flag. Windows lacks it.
|
||||
try: () =>
|
||||
NodeFSP.open(
|
||||
realTargetPath,
|
||||
NodeFS.constants.O_RDONLY | (NodeFS.constants.O_NONBLOCK ?? 0),
|
||||
),
|
||||
catch: (cause) =>
|
||||
new WorkspaceFileSystemOperationError({
|
||||
workspaceRoot: input.cwd,
|
||||
|
||||
Reference in new issue
Block a user