session-system-context.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. export * as SessionSystemContext from "./session-system-context"
  2. import { Context, DateTime, Effect, Layer } from "effect"
  3. import { Location } from "./location"
  4. import { SystemContext } from "./system-context"
  5. export interface Interface {
  6. readonly load: () => Effect.Effect<SystemContext.Snapshot>
  7. }
  8. export class Service extends Context.Service<Service, Interface>()("@opencode/v2/SessionSystemContext") {}
  9. export const layer = Layer.effect(
  10. Service,
  11. Effect.gen(function* () {
  12. const location = yield* Location.Service
  13. const environment = [
  14. "<env>",
  15. ` Working directory: ${location.directory}`,
  16. ` Workspace root folder: ${location.project.directory}`,
  17. ` Is directory a git repo: ${location.vcs?.type === "git" ? "yes" : "no"}`,
  18. ` Platform: ${process.platform}`,
  19. "</env>",
  20. ].join("\n")
  21. const context = SystemContext.struct({
  22. environment: SystemContext.value({
  23. key: SystemContext.Key.make("core/environment"),
  24. load: Effect.succeed({
  25. baseline: ["Here is some useful information about the environment you are running in:", environment].join(
  26. "\n",
  27. ),
  28. update: ["The environment you are running in is now:", environment].join("\n"),
  29. }),
  30. }),
  31. date: SystemContext.value({
  32. key: SystemContext.Key.make("core/date"),
  33. load: DateTime.nowAsDate.pipe(
  34. Effect.map((date) => ({
  35. baseline: `Today's date: ${date.toDateString()}`,
  36. update: `Today's date is now: ${date.toDateString()}`,
  37. })),
  38. ),
  39. }),
  40. })
  41. return Service.of({
  42. load: Effect.fn("SessionSystemContext.load")(function* () {
  43. return yield* SystemContext.load(context)
  44. }),
  45. })
  46. }),
  47. )
  48. export const locationLayer = layer