@@ -10,6 +10,8 @@ import { Location } from "../../location.js"
1010import { Model } from "../../model.js"
1111import type { PluginInternal } from "../internal.js"
1212import { foldSettings } from "./configured.js"
13+ import { activeCredential } from "./credential-settings.js"
14+ import { ProviderRefresh } from "./refresh.js"
1315
1416export type LocalModel = {
1517 readonly id : string
@@ -53,10 +55,11 @@ export function createLocalProviderPlugin<Discovered>(input: {
5355 ? undefined
5456 : input . origin )
5557 const source = { current : configured ( yield * config . entries ( ) , input . providerID , defaultOrigin , suffix ) }
56- const loaded = { models : [ ] as readonly Discovered [ ] , hash : "[]" }
58+ const loaded = { models : [ ] as readonly Discovered [ ] , connected : false , hash : "[]" }
5759
5860 yield * ctx . integration . transform ( ( integrations ) => {
59- if ( loaded . models . length === 0 ) return
61+ // A key connection lives on the integration (env keys only exist while it does), so keep it then.
62+ if ( loaded . models . length === 0 || loaded . connected ) return
6063 integrations . remove ( input . providerID )
6164 } )
6265
@@ -74,7 +77,7 @@ export function createLocalProviderPlugin<Discovered>(input: {
7477 provider : input . providerID ,
7578 apiKey : source . current . apiKey ?? "" ,
7679 }
77- provider . integrationID = undefined
80+ if ( ! loaded . connected ) provider . integrationID = undefined
7881 } )
7982 for ( const item of loaded . models ) {
8083 const spec = input . model ( item )
@@ -101,38 +104,40 @@ export function createLocalProviderPlugin<Discovered>(input: {
101104 if ( ! current . endpoint ) return undefined
102105 return yield * discoveryLock . withPermit (
103106 Effect . gen ( function * ( ) {
107+ // A key saved with /connect or set in the integration's env var wins over config, as it does for requests.
108+ const active = yield * activeCredential ( ctx , input . providerID )
109+ const connected = active . connection !== undefined
110+ const apiKey = active . credential ?. type === "key" ? active . credential . key : current . apiKey
104111 const cached = discovery . get ( current . root )
105- if (
106- cached &&
107- cached . apiKey === current . apiKey &&
108- Date . now ( ) - cached . checked < Duration . toMillis ( interval )
109- )
110- return { source : current , models : cached . models }
112+ if ( cached && cached . apiKey === apiKey && Date . now ( ) - cached . checked < Duration . toMillis ( interval ) )
113+ return { source : current , connected, models : cached . models }
111114 discovery . set ( current . root , {
112115 checked : Date . now ( ) ,
113- apiKey : current . apiKey ,
114- models : cached && cached . apiKey === current . apiKey ? cached . models : undefined ,
116+ apiKey,
117+ models : cached && cached . apiKey === apiKey ? cached . models : undefined ,
115118 } )
116- const models = yield * input . discover ( createClient ( http , current . root , current . apiKey , current . endpoint ) )
117- discovery . set ( current . root , { checked : Date . now ( ) , apiKey : current . apiKey , models } )
118- return { source : current , models }
119+ const models = yield * input . discover ( createClient ( http , current . root , apiKey , current . endpoint ) )
120+ discovery . set ( current . root , { checked : Date . now ( ) , apiKey, models } )
121+ return { source : current , connected , models }
119122 } ) ,
120123 )
121124 } )
122125
123126 const refresh = Effect . fn ( "LocalProviderPlugin.refresh" ) ( function * ( ) {
124127 const result = yield * discover ( )
125128 if ( ! result ?. models || result . source !== source . current ) return
126- const hash = JSON . stringify ( result . models )
129+ const hash = JSON . stringify ( [ result . connected , result . models ] )
127130 if ( hash === loaded . hash ) return
128131 loaded . models = result . models
132+ loaded . connected = result . connected
129133 loaded . hash = hash
130134 yield * ctx . integration . reload ( )
131135 yield * ctx . provider . reload ( )
132136 } )
133137
134138 // Keep the last successful inventory through transient outages instead of flickering model availability.
135139 yield * refresh ( ) . pipe ( Effect . ignore , Effect . repeat ( Schedule . spaced ( interval ) ) , Effect . forkScoped )
140+ yield * ProviderRefresh . onSwitch ( input . providerID , refresh ( ) . pipe ( Effect . ignore ) )
136141 const reload = Effect . fn ( "LocalProviderPlugin.reload" ) ( function * ( ) {
137142 const next = configured ( yield * config . entries ( ) , input . providerID , defaultOrigin , suffix )
138143 if (
@@ -143,6 +148,7 @@ export function createLocalProviderPlugin<Discovered>(input: {
143148 return
144149 source . current = next
145150 loaded . models = [ ]
151+ loaded . connected = false
146152 loaded . hash = "[]"
147153 yield * ctx . integration . reload ( )
148154 yield * ctx . provider . reload ( )
0 commit comments