location.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { Context, Effect, Layer, Schema } from "effect"
  2. import { Ref } from "@opencode-ai/schema/location"
  3. import { Project } from "./project"
  4. import { AbsolutePath, optionalOmitUndefined } from "./schema"
  5. import { WorkspaceV2 } from "./workspace"
  6. export * as Location from "./location"
  7. export { Ref }
  8. export class Info extends Schema.Class<Info>("Location.Info")({
  9. directory: AbsolutePath,
  10. workspaceID: optionalOmitUndefined(WorkspaceV2.ID),
  11. project: Schema.Struct({
  12. id: Project.ID,
  13. directory: AbsolutePath,
  14. }),
  15. }) {}
  16. export interface Interface extends Info {
  17. readonly vcs?: Project.Vcs
  18. }
  19. export function response<S extends Schema.Top>(data: S) {
  20. return Schema.Struct({ location: Info, data })
  21. }
  22. export class Service extends Context.Service<Service, Interface>()("@opencode/Location") {}
  23. export const layer = (ref: Ref) =>
  24. Layer.effect(
  25. Service,
  26. Effect.gen(function* () {
  27. const project = yield* Project.Service
  28. const resolved = yield* project.resolve(ref.directory)
  29. return Service.of({
  30. directory: ref.directory,
  31. workspaceID: ref.workspaceID,
  32. project: { id: resolved.id, directory: resolved.directory },
  33. vcs: resolved.vcs,
  34. })
  35. }),
  36. )