11import { readFile } from "node:fs/promises" ;
2- import { join , relative } from "node:path" ;
2+ import { join } from "node:path" ;
33import { getRuntimeConfig } from "../config/config.js" ;
44import type { OpenClawConfig } from "../config/types.openclaw.js" ;
55import { createSubsystemLogger } from "../logging/subsystem.js" ;
@@ -17,14 +17,11 @@ import {
1717 normalizeLowercaseStringOrEmpty ,
1818 normalizeOptionalString ,
1919} from "../shared/string-coerce.js" ;
20- import {
21- resolveAgentWorkspaceDir ,
22- resolveDefaultAgentDir ,
23- resolveDefaultAgentId ,
24- } from "./agent-scope.js" ;
20+ import { resolveDefaultAgentDir } from "./agent-scope.js" ;
2521import { ensureAuthProfileStoreWithoutExternalProfiles } from "./auth-profiles.js" ;
2622import { modelSupportsInput as modelCatalogEntrySupportsInput } from "./model-catalog-lookup.js" ;
2723import type { ModelCatalogEntry , ModelInputType } from "./model-catalog.types.js" ;
24+ import { resolveModelWorkspaceDir } from "./model-discovery-context.js" ;
2825import {
2926 modelKey ,
3027 normalizeConfiguredProviderCatalogModelId ,
@@ -36,10 +33,9 @@ import {
3633} from "./model-selection-shared.js" ;
3734import { ensureOpenClawModelsJson } from "./models-config.js" ;
3835import {
39- decodePluginModelCatalogRelativePathPluginId ,
40- isGeneratedPluginModelCatalog ,
41- listPluginModelCatalogPaths ,
42- resolvePluginModelCatalogOwnerPluginId ,
36+ filterGeneratedPluginModelCatalogProviders ,
37+ listPluginModelCatalogFiles ,
38+ type PluginModelCatalogMetadataSnapshot ,
4339} from "./plugin-model-catalog.js" ;
4440import { normalizeProviderId } from "./provider-id.js" ;
4541
@@ -328,18 +324,12 @@ function readProviderCatalogRows(parsed: unknown): Record<string, Record<string,
328324
329325async function loadReadOnlyPersistedProviderRows (
330326 agentDir : string ,
331- getPluginMetadataSnapshot : ( ) => Pick < PluginMetadataSnapshot , "owners" > ,
327+ getPluginMetadataSnapshot : ( ) => PluginModelCatalogMetadataSnapshot ,
332328) : Promise < Record < string , Record < string , unknown > > > {
333329 const raw = await readFile ( join ( agentDir , "models.json" ) , "utf8" ) ;
334330 const providers = { ...readProviderCatalogRows ( JSON . parse ( raw ) as unknown ) } ;
335- for ( const catalogPath of listPluginModelCatalogPaths ( agentDir ) ) {
336- const catalogPluginId = decodePluginModelCatalogRelativePathPluginId (
337- relative ( agentDir , catalogPath ) ,
338- ) ;
339- if ( ! catalogPluginId ) {
340- continue ;
341- }
342- const catalogRaw = await readFile ( catalogPath , "utf8" ) . catch ( ( ) => undefined ) ;
331+ for ( const catalogFile of listPluginModelCatalogFiles ( agentDir ) ) {
332+ const catalogRaw = await readFile ( catalogFile . path , "utf8" ) . catch ( ( ) => undefined ) ;
343333 if ( ! catalogRaw ) {
344334 continue ;
345335 }
@@ -349,17 +339,15 @@ async function loadReadOnlyPersistedProviderRows(
349339 } catch {
350340 continue ;
351341 }
352- if ( isGeneratedPluginModelCatalog ( parsed ) ) {
353- for ( const [ providerId , provider ] of Object . entries ( readProviderCatalogRows ( parsed ) ) ) {
354- const ownerPluginId = resolvePluginModelCatalogOwnerPluginId ( {
355- providerId,
356- pluginMetadataSnapshot : getPluginMetadataSnapshot ( ) ,
357- } ) ;
358- if ( ownerPluginId === catalogPluginId ) {
359- providers [ providerId ] = provider ;
360- }
361- }
362- }
342+ Object . assign (
343+ providers ,
344+ filterGeneratedPluginModelCatalogProviders ( {
345+ catalogPluginId : catalogFile . pluginId ,
346+ parsedCatalog : parsed ,
347+ pluginMetadataSnapshot : getPluginMetadataSnapshot ( ) ,
348+ providers : readProviderCatalogRows ( parsed ) ,
349+ } ) ,
350+ ) ;
363351 }
364352 return providers ;
365353}
@@ -370,7 +358,7 @@ async function loadReadOnlyPersistedModelCatalog(params?: {
370358} ) : Promise < ModelCatalogEntry [ ] > {
371359 const cfg = params ?. config ?? getRuntimeConfig ( ) ;
372360 const agentDir = resolveDefaultAgentDir ( cfg ) ;
373- const workspaceDir = resolveAgentWorkspaceDir ( cfg , resolveDefaultAgentId ( cfg ) ) ;
361+ const workspaceDir = resolveModelWorkspaceDir ( cfg , undefined ) ;
374362 const models : ModelCatalogEntry [ ] = [ ] ;
375363 const { buildShouldSuppressBuiltInModel } = await loadModelSuppression ( ) ;
376364 const shouldSuppressBuiltInModel = buildShouldSuppressBuiltInModel ( { config : cfg } ) ;
@@ -519,7 +507,7 @@ export async function loadModelCatalog(params?: {
519507 const sortModels = sortModelCatalogEntries ;
520508 try {
521509 const cfg = params ?. config ?? getRuntimeConfig ( ) ;
522- const workspaceDir = resolveAgentWorkspaceDir ( cfg , resolveDefaultAgentId ( cfg ) ) ;
510+ const workspaceDir = resolveModelWorkspaceDir ( cfg , undefined ) ;
523511 let manifestMetadataSnapshot : PluginMetadataSnapshot | undefined ;
524512 let manifestPlugins : ProviderModelIdNormalizationOptions [ "manifestPlugins" ] ;
525513 const getManifestMetadataSnapshot = ( ) => {
0 commit comments