filesystem.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. export * as FileSystem from "./filesystem"
  2. import path from "path"
  3. import { Context, Effect, Layer, Schema } from "effect"
  4. import { FSUtil } from "./fs-util"
  5. import { Location } from "./location"
  6. import { PositiveInt, RelativePath } from "./schema"
  7. import { FileSystemSearch } from "./filesystem/search"
  8. import { Entry, FileSystem, Match } from "@opencode-ai/schema/filesystem"
  9. export { Entry, Match, Submatch } from "@opencode-ai/schema/filesystem"
  10. export const ReadInput = Schema.Struct({
  11. path: RelativePath,
  12. })
  13. export type ReadInput = typeof ReadInput.Type
  14. export const Content = Schema.Struct({
  15. uri: Schema.String,
  16. name: Schema.String.pipe(Schema.optional),
  17. content: Schema.String,
  18. encoding: Schema.Literals(["utf8", "base64"]),
  19. mime: Schema.String,
  20. }).annotate({ identifier: "FileSystem.Content" })
  21. export type Content = typeof Content.Type
  22. export const ListInput = Schema.Struct({
  23. path: RelativePath.pipe(Schema.optional),
  24. })
  25. export type ListInput = typeof ListInput.Type
  26. export class FindInput extends Schema.Class<FindInput>("FileSystem.FindInput")({
  27. query: Schema.String,
  28. type: Schema.Literals(["file", "directory"]).pipe(Schema.optional),
  29. limit: PositiveInt.pipe(Schema.optional),
  30. }) {}
  31. export class GlobInput extends Schema.Class<GlobInput>("FileSystem.GlobInput")({
  32. pattern: Schema.String,
  33. path: RelativePath.pipe(Schema.optional),
  34. limit: PositiveInt.pipe(Schema.optional),
  35. }) {}
  36. export class GrepInput extends Schema.Class<GrepInput>("FileSystem.GrepInput")({
  37. pattern: Schema.String,
  38. path: RelativePath.pipe(Schema.optional),
  39. include: Schema.String.pipe(Schema.optional),
  40. limit: PositiveInt.pipe(Schema.optional),
  41. }) {}
  42. export const Event = FileSystem.Event
  43. export interface Interface {
  44. readonly read: (input: ReadInput) => Effect.Effect<{ readonly content: Uint8Array; readonly mime: string }>
  45. readonly list: (input?: ListInput) => Effect.Effect<Entry[]>
  46. readonly find: (input: FindInput) => Effect.Effect<Entry[]>
  47. readonly glob: (input: GlobInput) => Effect.Effect<readonly Entry[]>
  48. readonly grep: (input: GrepInput) => Effect.Effect<readonly Match[]>
  49. }
  50. export class Service extends Context.Service<Service, Interface>()("@opencode/v2/FileSystem") {}
  51. const baseLayer = Layer.effect(
  52. Service,
  53. Effect.gen(function* () {
  54. const fs = yield* FSUtil.Service
  55. const location = yield* Location.Service
  56. const search = yield* FileSystemSearch.Service
  57. const root = yield* fs.realPath(location.directory).pipe(Effect.orDie)
  58. const resolve = Effect.fnUntraced(function* (input?: RelativePath) {
  59. const absolute = path.resolve(location.directory, input ?? ".")
  60. if (!FSUtil.contains(location.directory, absolute))
  61. return yield* Effect.die(new Error("Path escapes the location"))
  62. const real = yield* fs.realPath(absolute).pipe(Effect.orDie)
  63. if (!FSUtil.contains(root, real)) return yield* Effect.die(new Error("Path escapes the location"))
  64. return { absolute, real, directory: location.directory, root }
  65. })
  66. return Service.of({
  67. find: search.find,
  68. glob: search.glob,
  69. grep: search.grep,
  70. read: Effect.fn("FileSystem.read")(function* (input) {
  71. const target = yield* resolve(input.path)
  72. const info = yield* fs.stat(target.real).pipe(Effect.orDie)
  73. if (info.type !== "File") return yield* Effect.die(new Error("Path is not a file"))
  74. return {
  75. content: yield* fs.readFile(target.real).pipe(Effect.orDie),
  76. mime: FSUtil.mimeType(target.real),
  77. }
  78. }),
  79. list: Effect.fn("FileSystem.list")(function* (input = {}) {
  80. const target = yield* resolve(input.path)
  81. const info = yield* fs.stat(target.real).pipe(Effect.orDie)
  82. if (info.type !== "Directory") return yield* Effect.die(new Error("Path is not a directory"))
  83. return yield* fs.readDirectoryEntries(target.real).pipe(
  84. Effect.orDie,
  85. Effect.map((items) =>
  86. items
  87. .flatMap((item) => {
  88. if (item.type !== "file" && item.type !== "directory") return []
  89. const absolute = path.join(target.absolute, item.name)
  90. const relative = path.relative(target.directory, absolute)
  91. return [
  92. Entry.make({
  93. path: RelativePath.make(relative + (item.type === "directory" ? path.sep : "")),
  94. type: item.type,
  95. }),
  96. ]
  97. })
  98. .sort((a, b) => (a.type === b.type ? a.path.localeCompare(b.path) : a.type === "directory" ? -1 : 1)),
  99. ),
  100. )
  101. }),
  102. })
  103. }),
  104. )
  105. export const layer = baseLayer.pipe(Layer.provide(FileSystemSearch.locationLayer), Layer.provide(FSUtil.defaultLayer))
  106. export const locationLayer = layer