Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AntigravityClientFiles.ts

104 lines4.3 KB

1import * as Effect from "effect/Effect";
2import type * as FileSystem from "effect/FileSystem";
3import type * as Path from "effect/Path";
4import * as EffectAcpErrors from "effect-acp/errors";
5import type * as EffectAcpSchema from "effect-acp/compat";
6​
7/**
8 * Workspace file access requested through the ACP client `fs` capability.
9 * The agent gates each write behind `session/request_permission`, so only
10 * path containment is checked here.
11 */
12const CLIENT_FILE_MAX_BYTES = 8 * 1024 * 1024;
13​
14function isInsideRoot(path: Path.Path, root: string, candidate: string): boolean {
15 const relative = path.relative(root, candidate);
16 return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
18​
19/** Resolves an agent-supplied path and rejects anything outside the session roots. */
20const resolveClientFilePath = Effect.fn("AntigravityClientFiles.resolveClientFilePath")(
21 function* (input: {
22 readonly fileSystem: FileSystem.FileSystem;
23 readonly path: Path.Path;
24 readonly allowedRoots: ReadonlyArray<string>;
25 readonly requestPath: string;
26 }) {
27 const { path } = input;
28 const resolved = path.resolve(input.requestPath);
29 // Follow symlinks on the parent so a link out of the workspace cannot escape it.
30 const parent = yield* input.fileSystem
31 .realPath(path.dirname(resolved))
32 .pipe(Effect.orElseSucceed(() => path.dirname(resolved)));
33 const real = path.join(parent, path.basename(resolved));
34 const roots = yield* Effect.forEach(input.allowedRoots, (root) =>
35 input.fileSystem.realPath(root).pipe(Effect.orElseSucceed(() => root)),
36 );
37 if (!roots.some((root) => isInsideRoot(path, root, real))) {
38 return yield* EffectAcpErrors.AcpRequestError.invalidParams(
39 `Path '${input.requestPath}' is outside the session workspace.`,
40 );
41 }
42 return real;
43 },
44);
45​
46export const readAntigravityClientTextFile = Effect.fn("AntigravityClientFiles.readTextFile")(
47 function* (input: {
48 readonly fileSystem: FileSystem.FileSystem;
49 readonly path: Path.Path;
50 readonly allowedRoots: ReadonlyArray<string>;
51 readonly request: EffectAcpSchema.ReadTextFileRequest;
52 }): Effect.fn.Return<EffectAcpSchema.ReadTextFileResponse, EffectAcpErrors.AcpError> {
53 const filePath = yield* resolveClientFilePath({ ...input, requestPath: input.request.path });
54 const info = yield* input.fileSystem
55 .stat(filePath)
56 .pipe(
57 Effect.mapError(() =>
58 EffectAcpErrors.AcpRequestError.resourceNotFound(
59 `File '${input.request.path}' not found.`,
60 ),
61 ),
62 );
63 if (info.type !== "File" || Number(info.size) > CLIENT_FILE_MAX_BYTES) {
64 return yield* EffectAcpErrors.AcpRequestError.invalidParams(
65 `File '${input.request.path}' is not a readable text file under ${CLIENT_FILE_MAX_BYTES} bytes.`,
66 );
67 }
68 const text = yield* input.fileSystem
69 .readFileString(filePath)
70 .pipe(
71 Effect.mapError(() =>
72 EffectAcpErrors.AcpRequestError.internalError(`Could not read '${input.request.path}'.`),
73 ),
74 );
75 const line = input.request.line ?? undefined;
76 const limit = input.request.limit ?? undefined;
77 if (line === undefined && limit === undefined) {
78 return { content: text };
79 }
80 // ACP lines are 1-indexed. `limit` is a line count.
81 const lines = text.split("\n");
82 const start = Math.max(0, (line ?? 1) - 1);
83 const end = limit === undefined ? lines.length : Math.min(lines.length, start + limit);
84 return { content: lines.slice(start, end).join("\n") };
85 },
86);
87​
88export const writeAntigravityClientTextFile = Effect.fn("AntigravityClientFiles.writeTextFile")(
89 function* (input: {
90 readonly fileSystem: FileSystem.FileSystem;
91 readonly path: Path.Path;
92 readonly allowedRoots: ReadonlyArray<string>;
93 readonly request: EffectAcpSchema.WriteTextFileRequest;
94 }): Effect.fn.Return<EffectAcpSchema.WriteTextFileResponse, EffectAcpErrors.AcpError> {
95 const filePath = yield* resolveClientFilePath({ ...input, requestPath: input.request.path });
96 yield* input.fileSystem.makeDirectory(input.path.dirname(filePath), { recursive: true }).pipe(
97 Effect.andThen(input.fileSystem.writeFileString(filePath, input.request.content)),
98 Effect.mapError(() =>
99 EffectAcpErrors.AcpRequestError.internalError(`Could not write '${input.request.path}'.`),
100 ),
101 );
102 return {};
103 },
104);