guidance.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. export * as ReferenceGuidance from "./guidance"
  2. import { Context, Effect, Layer, Schema } from "effect"
  3. import { PluginBoot } from "../plugin/boot"
  4. import { Reference } from "../reference"
  5. import { SystemContext } from "../system-context/index"
  6. const Summary = Schema.Struct({
  7. name: Schema.String,
  8. path: Schema.String,
  9. description: Schema.String.pipe(Schema.optional),
  10. })
  11. const render = (references: ReadonlyArray<typeof Summary.Type>) =>
  12. [
  13. "Project references provide additional directories that can be accessed when relevant.",
  14. "<available_references>",
  15. ...references.flatMap((reference) => [
  16. " <reference>",
  17. ` <name>${reference.name}</name>`,
  18. ` <path>${reference.path}</path>`,
  19. ...(reference.description === undefined ? [] : [` <description>${reference.description}</description>`]),
  20. " </reference>",
  21. ]),
  22. "</available_references>",
  23. ].join("\n")
  24. export interface Interface {
  25. readonly load: () => Effect.Effect<SystemContext.SystemContext>
  26. }
  27. export class Service extends Context.Service<Service, Interface>()("@opencode/v2/ReferenceGuidance") {}
  28. export const layer = Layer.effect(
  29. Service,
  30. Effect.gen(function* () {
  31. const boot = yield* PluginBoot.Service
  32. const references = yield* Reference.Service
  33. return Service.of({
  34. load: Effect.fn("ReferenceGuidance.load")(function* () {
  35. yield* boot.wait()
  36. const available = (yield* references.list())
  37. .filter((reference) => reference.description !== undefined)
  38. .map((reference) => ({
  39. name: reference.name,
  40. path: reference.path,
  41. description: reference.description,
  42. }))
  43. .toSorted((a, b) => a.name.localeCompare(b.name))
  44. if (available.length === 0) return SystemContext.empty
  45. return SystemContext.make({
  46. key: SystemContext.Key.make("core/reference-guidance"),
  47. codec: Schema.toCodecJson(Schema.Array(Summary)),
  48. load: Effect.succeed(available),
  49. baseline: render,
  50. update: (_previous, current) =>
  51. [
  52. "The available project references have changed. This list supersedes the previous reference list.",
  53. render(current),
  54. ].join("\n"),
  55. removed: () => "Project reference guidance is no longer available. Do not use previously listed references.",
  56. })
  57. }),
  58. })
  59. }),
  60. )
  61. export const locationLayer = layer