@@ -58,8 +58,9 @@ import {
5858} from "@t3tools/provider-core/server/failure" ;
5959import { turnScopedSelectionTransition } from "@t3tools/provider-core/server/selectionTransition" ;
6060import {
61+ findReportedPiModel ,
6162 makePiRpcConnection ,
62- parsePiModelSlug ,
63+ resolvePiModelSlug ,
6364 piRecordField as recordField ,
6465 piRecordNumber as recordNumber ,
6566 piRecordString as recordString ,
@@ -589,7 +590,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
589590 let baselineThinking : string | null = null ;
590591 let contextWindow : number | null = null ;
591592 const modelContextWindows = new Map < string , number > ( ) ;
592- let modelsDiscovered = false ;
593+ let discoveredModels : ReadonlyArray < unknown > | null = null ;
593594 // Prompt ACKs lack IDs; retain session send order and ownership so late ACKs cannot affect new turns.
594595 const pendingPromptResponses : Array < PiPromptSend > = [ ] ;
595596 const pendingCompactResponses : Array < {
@@ -2420,6 +2421,17 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
24202421 Effect . forkIn ( scope ) ,
24212422 ) ;
24222423
2424+ const discoverModels = Effect . fnUntraced ( function * ( ) {
2425+ const modelsData = yield * request ( { type : "get_available_models" } ) . pipe (
2426+ Effect . orElseSucceed ( ( ) => undefined ) ,
2427+ ) ;
2428+ const models = recordField ( modelsData , "models" ) ;
2429+ if ( Array . isArray ( models ) ) {
2430+ for ( const model of models ) rememberModelContextWindow ( model ) ;
2431+ discoveredModels = models ;
2432+ }
2433+ } ) ;
2434+
24232435 const registerThread = Effect . fnUntraced ( function * (
24242436 threadInput : ProviderAdapter . ProviderAdapterV2EnsureThreadInput ,
24252437 publish = true ,
@@ -2468,16 +2480,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
24682480 }
24692481 }
24702482 const stateData = yield * request ( { type : "get_state" } ) ;
2471- if ( ! modelsDiscovered ) {
2472- const modelsData = yield * request ( { type : "get_available_models" } ) . pipe (
2473- Effect . orElseSucceed ( ( ) => undefined ) ,
2474- ) ;
2475- const models = recordField ( modelsData , "models" ) ;
2476- if ( Array . isArray ( models ) ) {
2477- for ( const model of models ) rememberModelContextWindow ( model ) ;
2478- modelsDiscovered = true ;
2479- }
2480- }
2483+ if ( discoveredModels === null ) yield * discoverModels ( ) ;
24812484 contextWindow = rememberModelContextWindow ( recordField ( stateData , "model" ) ) ;
24822485 // Capture each baseline before applying selections so our own choices cannot become Pi defaults.
24832486 if ( baselineModel === null && appliedModel === null ) {
@@ -2573,7 +2576,11 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
25732576 appliedThinking = baselineThinking ;
25742577 }
25752578 } else if ( modelSelection . model !== appliedModel ) {
2576- const parsed = parsePiModelSlug ( modelSelection . model ) ;
2579+ // Extensions and models.json edits can add providers after the first discovery.
2580+ if ( findReportedPiModel ( modelSelection . model , discoveredModels ) === undefined ) {
2581+ yield * discoverModels ( ) ;
2582+ }
2583+ const parsed = resolvePiModelSlug ( modelSelection . model , discoveredModels ) ;
25772584 if ( parsed === null ) {
25782585 return yield * protocolError (
25792586 `Pi model '${ modelSelection . model } ' must use provider/model format` ,
0 commit comments