fs.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { FileSystem } from "@opencode-ai/schema/filesystem"
  2. import { Location } from "@opencode-ai/schema/location"
  3. import { PositiveInt, RelativePath } from "@opencode-ai/schema/schema"
  4. import { Schema } from "effect"
  5. import { HttpApiEndpoint, HttpApiGroup, HttpApiSchema, OpenApi } from "effect/unstable/httpapi"
  6. import { LocationQuery, locationQueryOpenApi } from "./location"
  7. const ListQuery = Schema.Struct({
  8. ...LocationQuery.fields,
  9. path: RelativePath.pipe(Schema.optional),
  10. })
  11. const FindQuery = Schema.Struct({
  12. ...LocationQuery.fields,
  13. query: FileSystem.FindInput.fields.query,
  14. type: FileSystem.FindInput.fields.type,
  15. limit: Schema.NumberFromString.pipe(Schema.decodeTo(PositiveInt), Schema.optional),
  16. })
  17. export const FileSystemGroup = HttpApiGroup.make("server.fs")
  18. .add(
  19. HttpApiEndpoint.get("fs.read", "/api/fs/read/*", {
  20. query: LocationQuery,
  21. success: Schema.Uint8Array.pipe(HttpApiSchema.asUint8Array()),
  22. })
  23. .annotateMerge(locationQueryOpenApi)
  24. .annotateMerge(
  25. OpenApi.annotations({
  26. identifier: "v2.fs.read",
  27. summary: "Read file",
  28. description: "Serve one file relative to the requested location.",
  29. }),
  30. ),
  31. )
  32. .add(
  33. HttpApiEndpoint.get("fs.list", "/api/fs/list", {
  34. query: ListQuery,
  35. success: Location.response(Schema.Array(FileSystem.Entry)),
  36. })
  37. .annotateMerge(locationQueryOpenApi)
  38. .annotateMerge(
  39. OpenApi.annotations({
  40. identifier: "v2.fs.list",
  41. summary: "List directory",
  42. description: "List direct children of one directory relative to the requested location.",
  43. }),
  44. ),
  45. )
  46. .add(
  47. HttpApiEndpoint.get("fs.find", "/api/fs/find", {
  48. query: FindQuery,
  49. success: Location.response(Schema.Array(FileSystem.Entry)),
  50. })
  51. .annotateMerge(locationQueryOpenApi)
  52. .annotateMerge(
  53. OpenApi.annotations({
  54. identifier: "v2.fs.find",
  55. summary: "Find files",
  56. description: "Find recursively ranked filesystem entries relative to the requested location.",
  57. }),
  58. ),
  59. )
  60. .annotateMerge(
  61. OpenApi.annotations({
  62. title: "filesystem",
  63. description: "Experimental location-scoped filesystem routes.",
  64. }),
  65. )