@@ -84,6 +84,7 @@ export namespace SessionPrompt {
8484 const status = yield * SessionStatus . Service
8585 const sessions = yield * Session . Service
8686 const agents = yield * Agent . Service
87+ const provider = yield * Provider . Service
8788 const processor = yield * SessionProcessor . Service
8889 const compaction = yield * SessionCompaction . Service
8990 const plugin = yield * Plugin . Service
@@ -206,14 +207,14 @@ export namespace SessionPrompt {
206207
207208 const ag = yield * agents . get ( "title" )
208209 if ( ! ag ) return
210+ const mdl = ag . model
211+ ? yield * provider . getModel ( ag . model . providerID , ag . model . modelID )
212+ : ( ( yield * provider . getSmallModel ( input . providerID ) ) ??
213+ ( yield * provider . getModel ( input . providerID , input . modelID ) ) )
214+ const msgs = onlySubtasks
215+ ? [ { role : "user" as const , content : subtasks . map ( ( p ) => p . prompt ) . join ( "\n" ) } ]
216+ : yield * Effect . promise ( ( ) => MessageV2 . toModelMessages ( context , mdl ) )
209217 const text = yield * Effect . promise ( async ( signal ) => {
210- const mdl = ag . model
211- ? await Provider . getModel ( ag . model . providerID , ag . model . modelID )
212- : ( ( await Provider . getSmallModel ( input . providerID ) ) ??
213- ( await Provider . getModel ( input . providerID , input . modelID ) ) )
214- const msgs = onlySubtasks
215- ? [ { role : "user" as const , content : subtasks . map ( ( p ) => p . prompt ) . join ( "\n" ) } ]
216- : await MessageV2 . toModelMessages ( context , mdl )
217218 const result = await LLM . stream ( {
218219 agent : ag ,
219220 user : firstInfo ,
@@ -932,21 +933,35 @@ NOTE: At any point in time through this workflow you should feel free to ask the
932933 return { info : msg , parts : [ part ] }
933934 } )
934935
935- const getModel = ( providerID : ProviderID , modelID : ModelID , sessionID : SessionID ) =>
936- Effect . promise ( ( ) =>
937- Provider . getModel ( providerID , modelID ) . catch ( ( e ) => {
938- if ( Provider . ModelNotFoundError . isInstance ( e ) ) {
939- const hint = e . data . suggestions ?. length ? ` Did you mean: ${ e . data . suggestions . join ( ", " ) } ?` : ""
940- Bus . publish ( Session . Event . Error , {
941- sessionID,
942- error : new NamedError . Unknown ( {
943- message : `Model not found: ${ e . data . providerID } /${ e . data . modelID } .${ hint } ` ,
944- } ) . toObject ( ) ,
945- } )
946- }
947- throw e
948- } ) ,
949- )
936+ const getModel = Effect . fn ( "SessionPrompt.getModel" ) ( function * (
937+ providerID : ProviderID ,
938+ modelID : ModelID ,
939+ sessionID : SessionID ,
940+ ) {
941+ const exit = yield * provider . getModel ( providerID , modelID ) . pipe ( Effect . exit )
942+ if ( Exit . isSuccess ( exit ) ) return exit . value
943+ const err = Cause . squash ( exit . cause )
944+ if ( Provider . ModelNotFoundError . isInstance ( err ) ) {
945+ const hint = err . data . suggestions ?. length ? ` Did you mean: ${ err . data . suggestions . join ( ", " ) } ?` : ""
946+ yield * bus . publish ( Session . Event . Error , {
947+ sessionID,
948+ error : new NamedError . Unknown ( {
949+ message : `Model not found: ${ err . data . providerID } /${ err . data . modelID } .${ hint } ` ,
950+ } ) . toObject ( ) ,
951+ } )
952+ }
953+ return yield * Effect . failCause ( exit . cause )
954+ } )
955+
956+ const lastModel = Effect . fnUntraced ( function * ( sessionID : SessionID ) {
957+ const model = yield * Effect . promise ( async ( ) => {
958+ for await ( const item of MessageV2 . stream ( sessionID ) ) {
959+ if ( item . info . role === "user" && item . info . model ) return item . info . model
960+ }
961+ } )
962+ if ( model ) return model
963+ return yield * provider . defaultModel ( )
964+ } )
950965
951966 const createUserMessage = Effect . fn ( "SessionPrompt.createUserMessage" ) ( function * ( input : PromptInput ) {
952967 const agentName = input . agent || ( yield * agents . defaultAgent ( ) )
@@ -960,9 +975,12 @@ NOTE: At any point in time through this workflow you should feel free to ask the
960975 }
961976
962977 const model = input . model ?? ag . model ?? ( yield * lastModel ( input . sessionID ) )
978+ const same = ag . model && model . providerID === ag . model . providerID && model . modelID === ag . model . modelID
963979 const full =
964- ! input . variant && ag . variant
965- ? yield * Effect . promise ( ( ) => Provider . getModel ( model . providerID , model . modelID ) . catch ( ( ) => undefined ) )
980+ ! input . variant && ag . variant && same
981+ ? yield * provider
982+ . getModel ( model . providerID , model . modelID )
983+ . pipe ( Effect . catch ( ( ) => Effect . succeed ( undefined ) ) )
966984 : undefined
967985 const variant = input . variant ?? ( ag . variant && full ?. variants ?. [ ag . variant ] ? ag . variant : undefined )
968986
@@ -1109,7 +1127,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
11091127 ]
11101128 const read = yield * Effect . promise ( ( ) => ReadTool . init ( ) ) . pipe (
11111129 Effect . flatMap ( ( t ) =>
1112- Effect . promise ( ( ) => Provider . getModel ( info . model . providerID , info . model . modelID ) ) . pipe (
1130+ provider . getModel ( info . model . providerID , info . model . modelID ) . pipe (
11131131 Effect . flatMap ( ( mdl ) =>
11141132 Effect . promise ( ( ) =>
11151133 t . execute ( args , {
@@ -1711,6 +1729,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
17111729 Layer . provide ( FileTime . defaultLayer ) ,
17121730 Layer . provide ( ToolRegistry . defaultLayer ) ,
17131731 Layer . provide ( Truncate . layer ) ,
1732+ Layer . provide ( Provider . defaultLayer ) ,
17141733 Layer . provide ( AppFileSystem . defaultLayer ) ,
17151734 Layer . provide ( Plugin . defaultLayer ) ,
17161735 Layer . provide ( Session . defaultLayer ) ,
@@ -1856,15 +1875,6 @@ NOTE: At any point in time through this workflow you should feel free to ask the
18561875 return runPromise ( ( svc ) => svc . command ( CommandInput . parse ( input ) ) )
18571876 }
18581877
1859- const lastModel = Effect . fnUntraced ( function * ( sessionID : SessionID ) {
1860- return yield * Effect . promise ( async ( ) => {
1861- for await ( const item of MessageV2 . stream ( sessionID ) ) {
1862- if ( item . info . role === "user" && item . info . model ) return item . info . model
1863- }
1864- return Provider . defaultModel ( )
1865- } )
1866- } )
1867-
18681878 /** @internal Exported for testing */
18691879 export function createStructuredOutputTool ( input : {
18701880 schema : Record < string , any >
0 commit comments