filesystem.ts 4.9 KB

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