@@ -38,8 +38,7 @@ import { resolveLlamaServerEndpoint } from "./endpoint.js";
3838import { buildLlamaServerProviderConfig } from "./models.js" ;
3939
4040function selectSetupModelId ( discovery : Extract < LlamaServerDiscoveryResult , { kind : "success" } > ) {
41- const healthy = discovery . models . filter ( ( model ) => ! model . failed ) ;
42- const candidates = healthy . length > 0 ? healthy : discovery . models ;
41+ const candidates = discovery . models . filter ( ( model ) => ! model . failed ) ;
4342 const ready = candidates . filter (
4443 ( model ) => model . status === "loaded" || model . status === "sleeping" ,
4544 ) ;
@@ -224,28 +223,33 @@ async function removeDefaultAuthProfile(agentDir?: string): Promise<void> {
224223 }
225224}
226225
227- async function discoverForSetup ( params : {
228- config : OpenClawConfig ;
229- baseUrl : string ;
230- env ?: NodeJS . ProcessEnv ;
231- signal ?: AbortSignal ;
232- } ) : Promise < LlamaServerDiscoveryResult > {
233- const providerConfig = params . config . models ?. providers ?. [ LLAMA_CPP_PROVIDER_ID ] ;
234- const headers = await resolveLlamaServerProviderHeaders ( {
235- config : params . config ,
236- env : params . env ,
237- headers : providerConfig ?. headers ,
238- } ) ;
239- const resolvedApiKey = ! hasLlamaServerAuthorizationHeader ( headers )
240- ? await resolveLlamaServerRuntimeApiKey ( { config : params . config } )
241- : undefined ;
242- return await discoverLlamaServer ( {
243- baseUrl : params . baseUrl ,
244- apiKey : resolvedApiKey ,
245- headers,
246- signal : params . signal ,
247- cacheTtlMs : 0 ,
248- } ) ;
226+ async function discoverForSetup (
227+ ctx : ProviderAppGuidedSetupContext ,
228+ ) : Promise < Extract < LlamaServerDiscoveryResult , { kind : "success" } > | null > {
229+ const provider = ctx . config . models ?. providers ?. [ LLAMA_CPP_PROVIDER_ID ] ;
230+ if ( provider ?. localService ) {
231+ return null ;
232+ }
233+ try {
234+ const headers = await resolveLlamaServerProviderHeaders ( {
235+ config : ctx . config ,
236+ env : ctx . env ,
237+ headers : provider ?. headers ,
238+ } ) ;
239+ const apiKey = ! hasLlamaServerAuthorizationHeader ( headers )
240+ ? await resolveLlamaServerRuntimeApiKey ( { config : ctx . config } )
241+ : undefined ;
242+ const discovery = await discoverLlamaServer ( {
243+ baseUrl : provider ?. baseUrl ?? LLAMA_SERVER_DEFAULT_ORIGIN ,
244+ apiKey,
245+ headers,
246+ signal : ctx . signal ,
247+ cacheTtlMs : 0 ,
248+ } ) ;
249+ return discovery . kind === "success" ? discovery : null ;
250+ } catch {
251+ return null ;
252+ }
249253}
250254
251255async function discoverWithAccess ( params : {
@@ -267,23 +271,8 @@ async function discoverWithAccess(params: {
267271export async function detectLlamaServerSetup (
268272 ctx : ProviderAppGuidedSetupContext ,
269273) : Promise < { modelRef : string ; detail ?: string } | null > {
270- const provider = ctx . config . models ?. providers ?. [ LLAMA_CPP_PROVIDER_ID ] ;
271- if ( provider ?. localService ) {
272- return null ;
273- }
274- const baseUrl = provider ?. baseUrl ?? LLAMA_SERVER_DEFAULT_ORIGIN ;
275- let discovery : LlamaServerDiscoveryResult ;
276- try {
277- discovery = await discoverForSetup ( {
278- config : ctx . config ,
279- baseUrl,
280- env : ctx . env ,
281- signal : ctx . signal ,
282- } ) ;
283- } catch {
284- return null ;
285- }
286- if ( discovery . kind !== "success" ) {
274+ const discovery = await discoverForSetup ( ctx ) ;
275+ if ( ! discovery ) {
287276 return null ;
288277 }
289278 const modelId = selectSetupModelId ( discovery ) ;
@@ -300,22 +289,8 @@ export async function detectLlamaServerSetup(
300289export async function prepareLlamaServerSetup (
301290 ctx : ProviderAppGuidedSetupContext & { modelRef : string } ,
302291) : Promise < ProviderAuthResult | null > {
303- const provider = ctx . config . models ?. providers ?. [ LLAMA_CPP_PROVIDER_ID ] ;
304- if ( provider ?. localService ) {
305- return null ;
306- }
307- let discovery : LlamaServerDiscoveryResult ;
308- try {
309- discovery = await discoverForSetup ( {
310- config : ctx . config ,
311- baseUrl : provider ?. baseUrl ?? LLAMA_SERVER_DEFAULT_ORIGIN ,
312- env : ctx . env ,
313- signal : ctx . signal ,
314- } ) ;
315- } catch {
316- return null ;
317- }
318- if ( discovery . kind !== "success" ) {
292+ const discovery = await discoverForSetup ( ctx ) ;
293+ if ( ! discovery ) {
319294 return null ;
320295 }
321296 const prefix = `${ LLAMA_CPP_PROVIDER_ID } /` ;
0 commit comments