location-layer.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { Effect, Layer, LayerMap } from "effect"
  2. import { Location } from "./location"
  3. import { Policy } from "./policy"
  4. import { Config } from "./config"
  5. import { PluginV2 } from "./plugin"
  6. import { Catalog } from "./catalog"
  7. import { Integration } from "./integration"
  8. import { CommandV2 } from "./command"
  9. import { AgentV2 } from "./agent"
  10. import { PluginInternal } from "./plugin/internal"
  11. import { Project } from "./project"
  12. import { ProjectCopy } from "./project/copy"
  13. import { ProjectDirectories } from "./project/directories"
  14. import { EventV2 } from "./event"
  15. import { Credential } from "./credential"
  16. import { Npm } from "./npm"
  17. import { ModelsDev } from "./models-dev"
  18. import { FSUtil } from "./fs-util"
  19. import { Git } from "./git"
  20. import { Global } from "./global"
  21. import { Database } from "./database/database"
  22. import { PermissionV2 } from "./permission"
  23. import { PermissionSaved } from "./permission/saved"
  24. import { FileSystem } from "./filesystem"
  25. import { Ripgrep } from "./ripgrep"
  26. import { Watcher } from "./filesystem/watcher"
  27. import { LocationMutation } from "./location-mutation"
  28. import { FileMutation } from "./file-mutation"
  29. import { Reference } from "./reference"
  30. import { ReferenceGuidance } from "./reference/guidance"
  31. import { RepositoryCache } from "./repository-cache"
  32. import { Pty } from "./pty"
  33. import { SkillV2 } from "./skill"
  34. import { SkillGuidance } from "./skill/guidance"
  35. import { BuiltInTools } from "./tool/builtins"
  36. import { Image } from "./image"
  37. import { ToolRegistry } from "./tool/registry"
  38. import { ApplicationTools } from "./tool/application-tools"
  39. import { ToolOutputStore } from "./tool-output-store"
  40. import { AppProcess } from "./process"
  41. import { SessionStore } from "./session/store"
  42. import { SessionTodo } from "./session/todo"
  43. import { QuestionV2 } from "./question"
  44. import { LLMClient } from "@opencode-ai/llm"
  45. import { RequestExecutor } from "@opencode-ai/llm/route"
  46. import * as SessionRunnerLLM from "./session/runner/llm"
  47. import { SessionRunnerModel } from "./session/runner/model"
  48. import { SystemContextBuiltIns } from "./system-context/builtins"
  49. import { FetchHttpClient } from "effect/unstable/http"
  50. import { Snapshot } from "./snapshot"
  51. export class LocationServiceMap extends LayerMap.Service<LocationServiceMap>()("@opencode/example/LocationServiceMap", {
  52. lookup: (ref: Location.Ref) => {
  53. const boot = Layer.effectDiscard(
  54. Effect.logInfo("booting location services", { directory: ref.directory, workspaceID: ref.workspaceID }),
  55. )
  56. const location = Location.layer(ref)
  57. const systemContext = SystemContextBuiltIns.locationLayer
  58. const base = Layer.mergeAll(
  59. location,
  60. Policy.locationLayer,
  61. Config.locationLayer,
  62. Reference.locationLayer,
  63. PluginV2.locationLayer,
  64. Catalog.locationLayer,
  65. Integration.locationLayer,
  66. CommandV2.locationLayer,
  67. AgentV2.locationLayer,
  68. PluginInternal.locationLayer,
  69. ProjectCopy.locationLayer,
  70. FileSystem.locationLayer,
  71. Watcher.locationLayer,
  72. Pty.locationLayer,
  73. SkillV2.locationLayer,
  74. systemContext,
  75. LocationMutation.locationLayer.pipe(Layer.orDie),
  76. ).pipe(Layer.provideMerge(location))
  77. const resources = ToolOutputStore.layer.pipe(Layer.provide(base))
  78. const permissionsAndTools = ToolRegistry.layer.pipe(
  79. Layer.provideMerge(PermissionV2.locationLayer),
  80. Layer.provide(resources),
  81. Layer.provide(base),
  82. )
  83. const services = Layer.mergeAll(base, resources, permissionsAndTools)
  84. const image = Image.layer.pipe(Layer.provide(services))
  85. const mutation = FileMutation.locationLayer.pipe(Layer.provide(services))
  86. const skillGuidance = SkillGuidance.locationLayer.pipe(Layer.provide(services))
  87. const referenceGuidance = ReferenceGuidance.locationLayer.pipe(Layer.provide(services))
  88. const todos = SessionTodo.layer.pipe(Layer.provide(services))
  89. const questions = QuestionV2.locationLayer.pipe(Layer.provide(services))
  90. const builtInTools = BuiltInTools.locationLayer.pipe(
  91. Layer.provide(services),
  92. Layer.provide(mutation),
  93. Layer.provide(resources),
  94. Layer.provide(todos),
  95. Layer.provide(questions),
  96. Layer.provide(image),
  97. )
  98. const model = SessionRunnerModel.locationLayer.pipe(Layer.provide(services))
  99. const snapshot = Snapshot.locationLayer.pipe(Layer.provide(services))
  100. const runner = SessionRunnerLLM.defaultLayer.pipe(
  101. Layer.provide(services),
  102. Layer.provide(model),
  103. Layer.provide(skillGuidance),
  104. Layer.provide(referenceGuidance),
  105. Layer.provide(snapshot),
  106. )
  107. // Kick off a background project copy refresh to update locations now that we
  108. // have a location
  109. const projectCopyRefresh = Layer.effectDiscard(ProjectCopy.refreshAfterBoot).pipe(Layer.provide(services))
  110. return Layer.mergeAll(
  111. boot,
  112. services,
  113. image,
  114. mutation,
  115. resources,
  116. todos,
  117. questions,
  118. model,
  119. snapshot,
  120. runner,
  121. builtInTools,
  122. referenceGuidance,
  123. projectCopyRefresh,
  124. ).pipe(Layer.fresh)
  125. },
  126. idleTimeToLive: "60 minutes",
  127. dependencies: [
  128. Project.defaultLayer,
  129. EventV2.defaultLayer,
  130. Credential.defaultLayer,
  131. Npm.defaultLayer,
  132. ModelsDev.defaultLayer,
  133. FSUtil.defaultLayer,
  134. Git.defaultLayer,
  135. AppProcess.defaultLayer,
  136. Global.defaultLayer,
  137. Ripgrep.defaultLayer,
  138. Database.defaultLayer,
  139. ProjectDirectories.defaultLayer,
  140. SessionStore.layer.pipe(Layer.provide(Database.defaultLayer)),
  141. PermissionSaved.defaultLayer,
  142. RepositoryCache.defaultLayer,
  143. LLMClient.layer.pipe(Layer.provide(RequestExecutor.defaultLayer)),
  144. FetchHttpClient.layer,
  145. ToolOutputStore.defaultCleanupLayer,
  146. ApplicationTools.layer,
  147. ],
  148. }) {}