@@ -15,12 +15,21 @@ import type { OpenClawConfig } from "../config/config.js";
1515import { extractModelCompat } from "../plugins/provider-model-compat.js" ;
1616import type { ProviderRuntimeModel } from "../plugins/provider-runtime-model.types.js" ;
1717import { normalizeProviderTransportWithPlugin } from "../plugins/provider-runtime.js" ;
18- import { resolveAgentDir , resolveAgentWorkspaceDir , resolveSessionAgentId } from "./agent-scope.js" ;
18+ import {
19+ resolveAgentDir ,
20+ resolveAgentWorkspaceDir ,
21+ resolveDefaultAgentDir ,
22+ resolveSessionAgentId ,
23+ } from "./agent-scope.js" ;
1924import { createOpenClawCodingTools } from "./agent-tools.js" ;
2025import { resolveEffectiveToolPolicy } from "./agent-tools.policy.js" ;
21- import { resolveModel } from "./embedded-agent-runner/model.js" ;
26+ import { resolveModel , resolveModelAsync } from "./embedded-agent-runner/model.js" ;
2227import { resolveBundledStaticCatalogModel } from "./embedded-agent-runner/model.static-catalog.js" ;
2328import { normalizeStaticProviderModelId } from "./model-ref-shared.js" ;
29+ import {
30+ PreparedModelRuntimeOwnerNotPublishedError ,
31+ acquireReadOnlyPreparedModelRuntime ,
32+ } from "./prepared-model-runtime.js" ;
2433import { normalizeToolName } from "./tool-policy.js" ;
2534import { buildRuntimeCompatibleToolInventory } from "./tools-effective-inventory-build.js" ;
2635import { buildEffectiveToolInventoryGroups } from "./tools-effective-inventory-groups.js" ;
@@ -172,7 +181,7 @@ function resolveDynamicRuntimeModelContext(params: {
172181}
173182
174183/** Resolves the runtime model metadata needed to filter model-compatible tools. */
175- export function resolveEffectiveToolInventoryRuntimeModelContext ( params : {
184+ function resolveEffectiveToolInventoryRuntimeModelContext ( params : {
176185 cfg : OpenClawConfig ;
177186 agentId ?: string ;
178187 agentDir ?: string ;
@@ -260,6 +269,49 @@ export function resolveEffectiveToolInventoryRuntimeModelContext(params: {
260269 } ;
261270}
262271
272+ /** Resolves dynamic model metadata after publishing a request-owned read snapshot when needed. */
273+ export async function resolveEffectiveToolInventoryRuntimeModelContextAsync (
274+ params : Parameters < typeof resolveEffectiveToolInventoryRuntimeModelContext > [ 0 ] ,
275+ ) : Promise < ReturnType < typeof resolveEffectiveToolInventoryRuntimeModelContext > > {
276+ try {
277+ return resolveEffectiveToolInventoryRuntimeModelContext ( params ) ;
278+ } catch ( error ) {
279+ if ( ! ( error instanceof PreparedModelRuntimeOwnerNotPublishedError ) ) {
280+ throw error ;
281+ }
282+ }
283+
284+ const provider = normalizeProviderId ( params . modelProvider ?? "" ) ;
285+ const modelId = params . modelId ?. trim ( ) ?? "" ;
286+ if ( ! provider || ! modelId ) {
287+ return { } ;
288+ }
289+ const agentId = params . agentId ?. trim ( ) || resolveSessionAgentId ( { config : params . cfg } ) ;
290+ const agentDir = params . agentDir ?? resolveAgentDir ( params . cfg , agentId ) ;
291+ const workspaceDir = params . workspaceDir ?? resolveAgentWorkspaceDir ( params . cfg , agentId ) ;
292+ const lease = await acquireReadOnlyPreparedModelRuntime ( {
293+ agentId,
294+ agentDir,
295+ config : params . cfg ,
296+ inheritedAuthDir : resolveDefaultAgentDir ( params . cfg ) ,
297+ workspaceDir,
298+ } ) ;
299+ try {
300+ const stores = lease . snapshot . createStores ( ) ;
301+ const resolved = await resolveModelAsync ( provider , modelId , agentDir , params . cfg , {
302+ agentId,
303+ workspaceDir,
304+ authStorage : stores . authStorage ,
305+ modelRegistry : stores . modelRegistry ,
306+ preparedModelRuntime : lease . snapshot ,
307+ } ) ;
308+ const runtimeModel = resolved . model as ProviderRuntimeModel | undefined ;
309+ return runtimeModel ? { modelApi : runtimeModel . api , runtimeModel } : { } ;
310+ } finally {
311+ lease . release ( ) ;
312+ }
313+ }
314+
263315/** Resolves compatibility metadata explicitly configured for a provider/model pair. */
264316export function resolveConfiguredModelCompat ( params : {
265317 cfg : OpenClawConfig ;
@@ -299,7 +351,7 @@ export function resolveEffectiveToolInventory(
299351 const workspaceDir = params . workspaceDir ?? resolveAgentWorkspaceDir ( params . cfg , agentId ) ;
300352 const agentDir = params . agentDir ?? resolveAgentDir ( params . cfg , agentId ) ;
301353 const runtimeModelContext =
302- params . modelApi || params . runtimeModel
354+ Object . hasOwn ( params , " modelApi" ) || Object . hasOwn ( params , " runtimeModel" )
303355 ? {
304356 modelApi : params . modelApi ?? params . runtimeModel ?. api ,
305357 runtimeModel : params . runtimeModel ,
0 commit comments