Repository navigation
Expand file tree
/
Copy pathfilesystem.ts
More file actions
216 lines (198 loc) · 8.34 KB
/
Copy pathfilesystem.ts
File metadata and controls
216 lines (198 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
export * as FileSystem from "./filesystem.js"
import { makeLocationNode } from "@opencode/util/effect/app-node"
import path from "path"
import { Context, Effect, Layer, type Option, type PlatformError, Schema, type Stream } from "effect"
import { FSUtil } from "@opencode/util/fs-util"
import { Location } from "./location.js"
import { AbsolutePath, PositiveInt, RelativePath } from "./schema.js"
import { FileSystemSearch } from "./filesystem/search.js"
import { Entry, FileSystem, FindInput, Write } from "@opencode/schema/filesystem"
export { Entry, Match, Submatch } from "@opencode/schema/filesystem"
export interface ReadInput {
readonly path: RelativePath
}
export interface WriteInput {
/** Absolute, or relative to the location directory. */
readonly path: string
readonly data: Uint8Array
}
export class NotFoundError extends Schema.TaggedError<NotFoundError>()("FileSystem.NotFoundError", {
path: RelativePath,
}) {}
export class DirectoryNotFoundError extends Schema.TaggedError<DirectoryNotFoundError>()(
"FileSystem.DirectoryNotFoundError",
{
directory: AbsolutePath,
cause: Schema.Defect(),
},
) {
override get message() {
return `Directory not found: ${this.directory}`
}
}
export class DirectoryAccessDeniedError extends Schema.TaggedError<DirectoryAccessDeniedError>()(
"FileSystem.DirectoryAccessDeniedError",
{
directory: AbsolutePath,
cause: Schema.Defect(),
},
) {
override get message() {
return `Access denied to directory: ${this.directory}`
}
}
export const Content = Schema.Struct({
uri: Schema.String,
name: Schema.String.pipe(Schema.optional),
content: Schema.String,
encoding: Schema.Literals(["utf8", "base64"]),
mime: Schema.String,
}).annotate({ identifier: "FileSystem.Content" })
export type Content = typeof Content.Type
export interface ListInput {
readonly path?: string
}
export { FindInput, Write }
export const DEFAULT_SEARCH_LIMIT = 100
export const DEFAULT_SEARCH_TIMEOUT_MS = 30_000
export class GlobInput extends Schema.Class<GlobInput>("FileSystem.GlobInput")({
pattern: Schema.String,
path: Schema.optionalKey(RelativePath),
hidden: Schema.optionalKey(Schema.Boolean),
limit: Schema.optionalKey(PositiveInt),
}) {}
export class GrepInput extends Schema.Class<GrepInput>("FileSystem.GrepInput")({
pattern: Schema.String,
path: Schema.optionalKey(RelativePath),
include: Schema.optionalKey(Schema.String),
literal: Schema.optionalKey(Schema.Boolean),
caseSensitive: Schema.optionalKey(Schema.Boolean),
limit: Schema.optionalKey(PositiveInt),
}) {}
export const Event = FileSystem.Event
export interface File {
readonly mime: string
readonly size: number
readonly mtime: Option.Option<Date>
readonly stream: (options?: {
readonly offset?: number
readonly bytesToRead?: number
}) => Stream.Stream<Uint8Array, PlatformError.PlatformError>
}
export interface Interface {
readonly read: (input: ReadInput) => Effect.Effect<File, NotFoundError>
/** Fails with `NotFoundError` when the path is missing or is not a directory. */
readonly list: (input?: ListInput) => Effect.Effect<Entry[], NotFoundError>
readonly find: (input: FindInput) => Effect.Effect<Entry[]>
/** Writes a file at an absolute path or one relative to the location; not confined to it. */
readonly write: (input: WriteInput) => Effect.Effect<Write>
}
export class Service extends Context.Service<Service, Interface>()("@opencode/FileSystem") {}
const baseLayer = Layer.effect(
Service,
Effect.gen(function* () {
const fs = yield* FSUtil.Service
const location = yield* Location.Service
const search = yield* FileSystemSearch.Service
// Workspace-placed directories exist only inside the workspace, so a host
// realpath probe at boot consults the wrong filesystem and would block
// construction on servers without a matching local directory. Treat the
// configured directory as canonical; local placements keep symlink
// canonicalization. This skip is boot-only: resolve/read/list below still
// access the host filesystem per operation (tracked in #44568).
const root = location.workspaceID
? location.directory
: yield* fs.realPath(location.directory).pipe(
// Linux realpath succeeds on an unreadable directory, so probe readability explicitly.
Effect.tap((real) => fs.access(real, { readable: true })),
Effect.catch((cause): Effect.Effect<never, DirectoryNotFoundError | DirectoryAccessDeniedError> => {
if (cause.reason._tag === "NotFound")
return Effect.fail(new DirectoryNotFoundError({ directory: location.directory, cause }))
if (FSUtil.isAccessDenied(cause))
return Effect.fail(new DirectoryAccessDeniedError({ directory: location.directory, cause }))
return Effect.die(cause)
}),
)
const resolve = Effect.fnUntraced(function* (input: RelativePath) {
if (input.includes("\u0000")) return yield* Effect.fail(new NotFoundError({ path: input }))
const absolute = path.resolve(location.directory, input)
if (!FSUtil.contains(location.directory, absolute)) return yield* Effect.fail(new NotFoundError({ path: input }))
const real = yield* fs.realPath(absolute).pipe(
Effect.catchReason(
"PlatformError",
"NotFound",
() => Effect.fail(new NotFoundError({ path: input })),
(_, error) => Effect.die(error),
),
)
if (!FSUtil.contains(root, real)) return yield* Effect.fail(new NotFoundError({ path: input }))
return { absolute, real, directory: location.directory }
})
return Service.of({
find: search.find,
read: Effect.fn("FileSystem.read")(function* (input) {
const target = yield* resolve(input.path)
const info = yield* fs.stat(target.real).pipe(
Effect.catchReason(
"PlatformError",
"NotFound",
() => Effect.fail(new NotFoundError({ path: input.path })),
(_, error) => Effect.die(error),
),
)
if (info.type !== "File") return yield* Effect.fail(new NotFoundError({ path: input.path }))
return {
mime: FSUtil.mimeType(target.real),
size: Number(info.size),
mtime: info.mtime,
stream: (options) => fs.stream(target.real, options),
}
}),
list: Effect.fn("FileSystem.list")(function* (input = {}) {
// Navigation can leave the cwd without activating another Location.
const directory = path.resolve(location.directory, input.path ?? ".")
const missing = new NotFoundError({ path: RelativePath.make(input.path ?? ".") })
const info = yield* fs.stat(directory).pipe(
Effect.catchReason(
"PlatformError",
"NotFound",
() => Effect.fail(missing),
(_, error) => Effect.die(error),
),
)
if (info.type !== "Directory") return yield* missing
return yield* fs.readDirectoryEntries(directory).pipe(
Effect.orDie,
Effect.map((items) =>
items
.flatMap((item) => {
if (item.type !== "File" && item.type !== "Directory") return []
const type = item.type === "Directory" ? "directory" : "file"
const absolute = path.join(directory, item.name)
const relative = path.relative(location.directory, absolute) || "."
return [
Entry.make({
path: RelativePath.make(relative + (type === "directory" ? path.sep : "")),
type,
}),
]
})
.sort((a, b) => (a.type === b.type ? a.path.localeCompare(b.path) : a.type === "directory" ? -1 : 1)),
),
)
}),
// Unlike read, write reaches outside the location so clients can stage files in the
// server tmp directory, which the model is already told to prefer and permitted to access.
write: Effect.fn("FileSystem.write")(function* (input) {
const target = path.resolve(location.directory, input.path)
yield* fs.writeWithDirs(target, input.data).pipe(Effect.orDie)
return Write.make({ path: AbsolutePath.make(target) })
}),
})
}),
)
export const node = makeLocationNode({
service: Service,
layer: baseLayer,
deps: [FSUtil.node, Location.node, FileSystemSearch.node],
})