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

AntigravityClientFiles.ts

113 lines4.7 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/** The agent requests permission for every write; check only path containment here. */
8const CLIENT_FILE_MAX_BYTES = 8 * 1024 * 1024;
9​
10function isInsideRoot(path: Path.Path, root: string, candidate: string): boolean {
11 const relative = path.relative(root, candidate);
12 return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
14​
15/** Resolve all symlinks, including the final component, before checking workspace containment. */
16const resolveClientFilePath = Effect.fn("AntigravityClientFiles.resolveClientFilePath")(
17 function* (input: {
18 readonly fileSystem: FileSystem.FileSystem;
19 readonly path: Path.Path;
20 readonly allowedRoots: ReadonlyArray<string>;
21 readonly requestPath: string;
22 }) {
23 const { path } = input;
24 const resolved = path.resolve(input.requestPath);
25 const outside = EffectAcpErrors.AcpRequestError.invalidParams(
26 `Path '${input.requestPath}' is outside the session workspace.`,
27 );
28 const real = yield* input.fileSystem.realPath(resolved).pipe(
29 Effect.catch(() =>
30 Effect.gen(function* () {
31 // Fall back to the parent only for missing files, never dangling or unreadable links.
32 const entryExists = yield* input.fileSystem.readLink(resolved).pipe(
33 Effect.as(true),
34 Effect.catch(() => input.fileSystem.exists(resolved)),
35 Effect.orElseSucceed(() => true),
36 );
37 if (entryExists) return yield* outside;
38 const parent = yield* input.fileSystem
39 .realPath(path.dirname(resolved))
40 .pipe(Effect.orElseSucceed(() => path.dirname(resolved)));
41 return path.join(parent, path.basename(resolved));
42 }),
43 ),
44 );
45 const roots = yield* Effect.forEach(input.allowedRoots, (root) =>
46 input.fileSystem.realPath(root).pipe(Effect.orElseSucceed(() => root)),
47 );
48 if (!roots.some((root) => isInsideRoot(path, root, real))) {
49 return yield* outside;
50 }
51 return real;
52 },
53);
54​
55export const readAntigravityClientTextFile = Effect.fn("AntigravityClientFiles.readTextFile")(
56 function* (input: {
57 readonly fileSystem: FileSystem.FileSystem;
58 readonly path: Path.Path;
59 readonly allowedRoots: ReadonlyArray<string>;
60 readonly request: EffectAcpSchema.ReadTextFileRequest;
61 }): Effect.fn.Return<EffectAcpSchema.ReadTextFileResponse, EffectAcpErrors.AcpError> {
62 const filePath = yield* resolveClientFilePath({ ...input, requestPath: input.request.path });
63 const info = yield* input.fileSystem
64 .stat(filePath)
65 .pipe(
66 Effect.mapError(() =>
67 EffectAcpErrors.AcpRequestError.resourceNotFound(
68 `File '${input.request.path}' not found.`,
69 ),
70 ),
71 );
72 if (info.type !== "File" || Number(info.size) > CLIENT_FILE_MAX_BYTES) {
73 return yield* EffectAcpErrors.AcpRequestError.invalidParams(
74 `File '${input.request.path}' is not a readable text file under ${CLIENT_FILE_MAX_BYTES} bytes.`,
75 );
76 }
77 const text = yield* input.fileSystem
78 .readFileString(filePath)
79 .pipe(
80 Effect.mapError(() =>
81 EffectAcpErrors.AcpRequestError.internalError(`Could not read '${input.request.path}'.`),
82 ),
83 );
84 const line = input.request.line ?? undefined;
85 const limit = input.request.limit ?? undefined;
86 if (line === undefined && limit === undefined) {
87 return { content: text };
88 }
89 // ACP lines are 1-indexed. `limit` is a line count.
90 const lines = text.split("\n");
91 const start = Math.max(0, (line ?? 1) - 1);
92 const end = limit === undefined ? lines.length : Math.min(lines.length, start + limit);
93 return { content: lines.slice(start, end).join("\n") };
94 },
95);
96​
97export const writeAntigravityClientTextFile = Effect.fn("AntigravityClientFiles.writeTextFile")(
98 function* (input: {
99 readonly fileSystem: FileSystem.FileSystem;
100 readonly path: Path.Path;
101 readonly allowedRoots: ReadonlyArray<string>;
102 readonly request: EffectAcpSchema.WriteTextFileRequest;
103 }): Effect.fn.Return<EffectAcpSchema.WriteTextFileResponse, EffectAcpErrors.AcpError> {
104 const filePath = yield* resolveClientFilePath({ ...input, requestPath: input.request.path });
105 yield* input.fileSystem.makeDirectory(input.path.dirname(filePath), { recursive: true }).pipe(
106 Effect.andThen(input.fileSystem.writeFileString(filePath, input.request.content)),
107 Effect.mapError(() =>
108 EffectAcpErrors.AcpRequestError.internalError(`Could not write '${input.request.path}'.`),
109 ),
110 );
111 return {};
112 },
113);