11export * as Flag from "./flag.js"
22
3- import { Context , Effect , Option , Record } from "effect"
3+ import { Context , Effect , Layer , Record } from "effect"
4+ import { makeLocationNode } from "@opencode/util/effect/app-node"
45import { ConfigFlag } from "@opencode/schema/config/flag"
56import type { Document , Entry } from "@opencode/schema/config"
67import { App } from "./app.js"
@@ -14,6 +15,14 @@ export interface State {
1415 readonly source : "env" | "config" | "default"
1516}
1617
18+ export interface Interface {
19+ readonly resolve : ( entries : readonly Entry [ ] ) => Record < Key , State >
20+ readonly all : Effect . Effect < Record < Key , State > >
21+ readonly enabled : ( key : Key ) => Effect . Effect < boolean >
22+ }
23+
24+ export class Service extends Context . Service < Service , Interface > ( ) ( "@opencode/Flag" ) { }
25+
1726/**
1827 * `OPENCODE_FLAGS`, a comma-separated list such as `schedules,-media_blob`; a leading `-` turns a flag off, and
1928 * `all` turns every flag on unless the same list opts one out, as in `all,-media_blob`.
@@ -63,19 +72,45 @@ export function resolve(input: {
6372 } )
6473}
6574
66- /** {@link resolve } for this process's channel and `OPENCODE_FLAGS`, for synchronous readers of config entries. */
67- export const resolver = Effect . gen ( function * ( ) {
68- const app = yield * App . Metadata
69- const overrides = yield * Overrides
70- return ( entries : readonly Entry [ ] ) => resolve ( { entries, channel : app . channel , overrides } )
71- } )
75+ export const layer = Layer . effect (
76+ Service ,
77+ Effect . gen ( function * ( ) {
78+ const app = yield * App . Service
79+ const config = yield * Config . Service
80+ const overrides = yield * Overrides
81+ const resolveEntries = ( entries : readonly Entry [ ] ) => resolve ( { entries, channel : app . channel , overrides } )
82+ const all = Effect . gen ( function * ( ) {
83+ return resolveEntries ( yield * config . entries ( ) )
84+ } )
85+ return Service . of ( {
86+ resolve : resolveEntries ,
87+ all,
88+ enabled : ( key ) => Effect . map ( all , ( flags ) => flags [ key ] . enabled ) ,
89+ } )
90+ } ) ,
91+ )
7292
73- /** Every flag with its value and where it came from, read from the Config in scope on each call. */
74- export const all = Effect . gen ( function * ( ) {
75- const config = yield * Effect . serviceOption ( Config . Service )
76- const entries = Option . isSome ( config ) ? yield * config . value . entries ( ) : [ ]
77- const read = yield * resolver
78- return read ( entries )
93+ export const testLayer = (
94+ entries : Entry [ ] = [ ] ,
95+ options ?: { readonly app ?: Partial < App . Info > ; readonly overrides ?: string } ,
96+ ) =>
97+ Layer . fresh ( layer ) . pipe (
98+ Layer . provideMerge (
99+ Layer . mergeAll (
100+ App . layer ( options ?. app ) ,
101+ Config . testLayer ( entries ) ,
102+ Layer . succeed ( Overrides , options ?. overrides ) ,
103+ ) ,
104+ ) ,
105+ )
106+
107+ export const node = makeLocationNode ( {
108+ service : Service ,
109+ layer,
110+ deps : [ App . node , Config . node ] ,
79111} )
80112
81- export const enabled = ( key : Key ) => all . pipe ( Effect . map ( ( flags ) => flags [ key ] . enabled ) )
113+ /** Every flag with its value and where it came from, read from the location's Config on each call. */
114+ export const all = Service . use ( ( flags ) => flags . all )
115+
116+ export const enabled = ( key : Key ) => Service . use ( ( flags ) => flags . enabled ( key ) )
0 commit comments