export * as ApplicationTools from "./application-tools" import { Context, Effect, Layer, Scope } from "effect" import { State } from "../state" import { Tool } from "./tool" import { makeGlobalNode } from "../effect/app-node" type Data = { readonly entries: Map } type Draft = { readonly set: (name: string, entry: Entry) => void } export interface Entry { readonly identity: object readonly tool: Tool.AnyTool } export interface Interface { readonly register: ( tools: Readonly>, ) => Effect.Effect readonly entries: () => ReadonlyMap } export class Service extends Context.Service()("@opencode/ApplicationTools") {} export const layer = Layer.effect( Service, Effect.gen(function* () { const state = State.create({ initial: () => ({ entries: new Map() }), draft: (draft) => ({ set: (name, tool) => { draft.entries.set(name, tool) }, }), }) return Service.of({ register: Effect.fn("ApplicationTools.register")(function* (tools) { const entries = Tool.registrationEntries(tools) if (entries.length === 0) return const registrations = entries.map(([name, tool]) => [name, { identity: {}, tool }] as const) yield* state.transform((draft) => { for (const [name, entry] of registrations) draft.set(name, entry) }) }), entries: () => state.get().entries, }) }), ) export const node = makeGlobalNode({ service: Service, layer, deps: [] })