+21

![opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



![opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



James Long
Brendan Allan
Kit Langton
opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Affan Ali
affanali2k3
Frank
opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴
Aiden Cline
Jay V
Dax Raad
Aarav Sareen
OpeOginni
Luke Parker
Ben Guthrie
Dax
Filip
Max Anderson
Brendan Allan
Jack
Shoubhit Dash
Dustin Deus
starptech
Aiden Cline
usrnk1
Jay
runvip
opencode
Julian Coy
Vladimir Glafirov
8c94e9005f
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
119 lines
4.5 KiB
TypeScript
119 lines
4.5 KiB
TypeScript
export * as FileSystem from "./filesystem"
|
|
|
|
import { makeLocationNode } from "./effect/app-node"
|
|
import path from "path"
|
|
import { Context, Effect, Layer, Schema } from "effect"
|
|
import { FSUtil } from "./fs-util"
|
|
import { Location } from "./location"
|
|
import { PositiveInt, RelativePath } from "./schema"
|
|
import { FileSystemSearch } from "./filesystem/search"
|
|
import { Entry, FileSystem, FindInput, Match } from "@opencode-ai/schema/filesystem"
|
|
export { Entry, Match, Submatch } from "@opencode-ai/schema/filesystem"
|
|
|
|
export const ReadInput = Schema.Struct({
|
|
path: RelativePath,
|
|
})
|
|
export type ReadInput = typeof ReadInput.Type
|
|
|
|
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 const ListInput = Schema.Struct({
|
|
path: RelativePath.pipe(Schema.optional),
|
|
})
|
|
export type ListInput = typeof ListInput.Type
|
|
|
|
export { FindInput }
|
|
|
|
export class GlobInput extends Schema.Class<GlobInput>("FileSystem.GlobInput")({
|
|
pattern: Schema.String,
|
|
path: RelativePath.pipe(Schema.optional),
|
|
limit: PositiveInt.pipe(Schema.optional),
|
|
}) {}
|
|
|
|
export class GrepInput extends Schema.Class<GrepInput>("FileSystem.GrepInput")({
|
|
pattern: Schema.String,
|
|
path: RelativePath.pipe(Schema.optional),
|
|
include: Schema.String.pipe(Schema.optional),
|
|
limit: PositiveInt.pipe(Schema.optional),
|
|
}) {}
|
|
|
|
export const Event = FileSystem.Event
|
|
|
|
export interface Interface {
|
|
readonly read: (input: ReadInput) => Effect.Effect<{ readonly content: Uint8Array; readonly mime: string }>
|
|
readonly list: (input?: ListInput) => Effect.Effect<Entry[]>
|
|
readonly find: (input: FindInput) => Effect.Effect<Entry[]>
|
|
readonly glob: (input: GlobInput) => Effect.Effect<readonly Entry[]>
|
|
readonly grep: (input: GrepInput) => Effect.Effect<readonly Match[]>
|
|
}
|
|
|
|
export class Service extends Context.Service<Service, Interface>()("@opencode/v2/FileSystem") {}
|
|
|
|
const baseLayer = Layer.effect(
|
|
Service,
|
|
Effect.gen(function* () {
|
|
const fs = yield* FSUtil.Service
|
|
const location = yield* Location.Service
|
|
const search = yield* FileSystemSearch.Service
|
|
const root = yield* fs.realPath(location.directory).pipe(Effect.orDie)
|
|
const resolve = Effect.fnUntraced(function* (input?: RelativePath) {
|
|
const absolute = path.resolve(location.directory, input ?? ".")
|
|
if (!FSUtil.contains(location.directory, absolute))
|
|
return yield* Effect.die(new Error("Path escapes the location"))
|
|
const real = yield* fs.realPath(absolute).pipe(Effect.orDie)
|
|
if (!FSUtil.contains(root, real)) return yield* Effect.die(new Error("Path escapes the location"))
|
|
return { absolute, real, directory: location.directory, root }
|
|
})
|
|
return Service.of({
|
|
find: search.find,
|
|
glob: search.glob,
|
|
grep: search.grep,
|
|
read: Effect.fn("FileSystem.read")(function* (input) {
|
|
const target = yield* resolve(input.path)
|
|
const info = yield* fs.stat(target.real).pipe(Effect.orDie)
|
|
if (info.type !== "File") return yield* Effect.die(new Error("Path is not a file"))
|
|
return {
|
|
content: yield* fs.readFile(target.real).pipe(Effect.orDie),
|
|
mime: FSUtil.mimeType(target.real),
|
|
}
|
|
}),
|
|
list: Effect.fn("FileSystem.list")(function* (input = {}) {
|
|
const target = yield* resolve(input.path)
|
|
const info = yield* fs.stat(target.real).pipe(Effect.orDie)
|
|
if (info.type !== "Directory") return yield* Effect.die(new Error("Path is not a directory"))
|
|
return yield* fs.readDirectoryEntries(target.real).pipe(
|
|
Effect.orDie,
|
|
Effect.map((items) =>
|
|
items
|
|
.flatMap((item) => {
|
|
if (item.type !== "file" && item.type !== "directory") return []
|
|
const absolute = path.join(target.absolute, item.name)
|
|
const relative = path.relative(target.directory, absolute)
|
|
return [
|
|
Entry.make({
|
|
path: RelativePath.make(relative + (item.type === "directory" ? path.sep : "")),
|
|
type: item.type,
|
|
}),
|
|
]
|
|
})
|
|
.sort((a, b) => (a.type === b.type ? a.path.localeCompare(b.path) : a.type === "directory" ? -1 : 1)),
|
|
),
|
|
)
|
|
}),
|
|
})
|
|
}),
|
|
)
|
|
|
|
export const node = makeLocationNode({
|
|
service: Service,
|
|
layer: baseLayer,
|
|
deps: [FSUtil.node, Location.node, FileSystemSearch.node],
|
|
})
|