11export * as OpenCode from "./opencode"
22
33import { Context , Effect , Layer } from "effect"
4- import { Catalog } from "../catalog"
54import { Database } from "../database/database"
65import { EventV2 } from "../event"
76import { LocationServiceMap } from "../location-layer"
8- import { PluginBoot } from "../plugin/boot"
97import { ProjectV2 } from "../project"
108import { SessionV2 } from "../session"
119import * as SessionExecutionLocal from "../session/execution/local"
@@ -23,69 +21,22 @@ export interface Interface {
2321/** Intentional public native API for Effect applications embedding OpenCode. */
2422export class Service extends Context . Service < Service , Interface > ( ) ( "@opencode/public/OpenCode" ) { }
2523
26- class SessionModelValidation extends Context . Service <
27- SessionModelValidation ,
28- {
29- readonly validate : (
30- input : Session . SwitchModelInput & { readonly location : Session . Info [ "location" ] } ,
31- ) => Effect . Effect < void , Session . ModelUnavailableError | Session . VariantUnavailableError >
32- }
33- > ( ) ( "@opencode/public/OpenCode/SessionModelValidation" ) { }
34-
35- const ApplicationToolsLayer = ApplicationTools . layer
36- const LocationServicesLayer = LocationServiceMap . layer . pipe ( Layer . provide ( ApplicationToolsLayer ) )
37- const SessionModelValidationLayer = Layer . effect (
38- SessionModelValidation ,
39- Effect . gen ( function * ( ) {
40- const locations = yield * LocationServiceMap
41- return SessionModelValidation . of ( {
42- validate : Effect . fn ( "OpenCode.sessions.validateModel" ) ( function * ( input ) {
43- yield * Effect . gen ( function * ( ) {
44- yield * ( yield * PluginBoot . Service ) . wait ( )
45- const catalog = yield * Catalog . Service
46- const model = ( yield * catalog . model . available ( ) ) . find (
47- ( model ) => model . providerID === input . model . providerID && model . id === input . model . id ,
48- )
49- if ( ! model )
50- return yield * new Session . ModelUnavailableError ( {
51- providerID : input . model . providerID ,
52- modelID : input . model . id ,
53- } )
54- if (
55- input . model . variant !== undefined &&
56- input . model . variant !== "default" &&
57- ! model . variants . some ( ( variant ) => variant . id === input . model . variant )
58- )
59- return yield * new Session . VariantUnavailableError ( {
60- providerID : input . model . providerID ,
61- modelID : input . model . id ,
62- variant : input . model . variant ,
63- } )
64- } ) . pipe ( Effect . provide ( locations . get ( input . location ) ) )
65- } ) ,
66- } )
67- } ) ,
24+ const SessionsLayer = SessionV2 . layer . pipe (
25+ Layer . provide ( SessionProjector . layer ) ,
26+ Layer . provide ( SessionExecutionLocal . layer ) ,
27+ Layer . provide ( SessionStore . layer ) ,
28+ Layer . provide ( EventV2 . layer ) ,
29+ Layer . provide ( Database . defaultLayer ) ,
30+ Layer . provide ( ProjectV2 . defaultLayer ) ,
31+ Layer . provide ( LocationServiceMap . layer . pipe ( Layer . provide ( ApplicationTools . layer ) ) ) ,
32+ Layer . orDie ,
6833)
69-
70- const SessionsLayer = Layer . merge (
71- SessionV2 . layer . pipe (
72- Layer . provide ( SessionProjector . layer ) ,
73- Layer . provide ( SessionExecutionLocal . layer ) ,
74- Layer . provide ( SessionStore . layer ) ,
75- Layer . provide ( EventV2 . layer ) ,
76- Layer . provide ( Database . defaultLayer ) ,
77- Layer . provide ( ProjectV2 . defaultLayer ) ,
78- Layer . orDie ,
79- ) ,
80- SessionModelValidationLayer ,
81- ) . pipe ( Layer . provide ( LocationServicesLayer ) )
8234// TODO: Accept explicit storage so tests and embeddings can select disposable or application-owned persistence.
8335export const layer = Layer . effect (
8436 Service ,
8537 Effect . gen ( function * ( ) {
8638 const sessions = yield * SessionV2 . Service
8739 const tools = yield * ApplicationTools . Service
88- const validation = yield * SessionModelValidation
8940 return Service . of ( {
9041 tools : { register : tools . register } ,
9142 sessions : {
@@ -98,11 +49,7 @@ export const layer = Layer.effect(
9849 } ) ,
9950 get : sessions . get ,
10051 list : sessions . list ,
101- switchModel : Effect . fn ( "OpenCode.sessions.switchModel" ) ( function * ( input ) {
102- const session = yield * sessions . get ( input . sessionID )
103- yield * validation . validate ( { ...input , location : session . location } )
104- yield * sessions . switchModel ( input )
105- } ) ,
52+ switchModel : sessions . switchModel ,
10653 interrupt : sessions . interrupt ,
10754 prompt : ( input ) =>
10855 sessions . prompt ( {
@@ -124,6 +71,6 @@ export const layer = Layer.effect(
12471 } ,
12572 } )
12673 } ) ,
127- ) . pipe ( Layer . provide ( Layer . merge ( ApplicationToolsLayer , SessionsLayer ) ) )
74+ ) . pipe ( Layer . provide ( Layer . merge ( ApplicationTools . layer , SessionsLayer ) ) )
12875
12976// TODO: Add OpenCode.create(...) as the Promise facade over the same native API semantics.
0 commit comments