location.ts 1.3 KB

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