11// Provider catalog helpers normalize, hash, and expose model catalogs for provider plugins.
22import { createHash } from "node:crypto" ;
3- import { normalizeModelCatalog } from "@openclaw/model-catalog-core/model-catalog-normalize" ;
4- import { buildModelCatalogRef } from "@openclaw/model-catalog-core/model-catalog-refs" ;
5- import type {
6- ModelCatalogCost ,
7- ModelCatalogMediaInputConfig ,
8- ModelCatalogModel ,
9- ModelCatalogTieredCost ,
10- } from "@openclaw/model-catalog-core/model-catalog-types" ;
113import { findNormalizedProviderKey } from "@openclaw/model-catalog-core/provider-id" ;
124import {
135 isFutureDateTimestampMs ,
146 resolveExpiresAtMsFromDurationMs ,
157} from "../../packages/normalization-core/src/number-coercion.js" ;
16- import { normalizeOptionalString } from "../../packages/normalization-core/src/string-coerce.js" ;
178import { normalizeConfiguredProviderCatalogModelId } from "../agents/model-ref-shared.js" ;
189import { resolveProviderRequestCapabilities } from "../agents/provider-attribution.js" ;
1910import type { ModelDefinitionConfig } from "../config/types.models.js" ;
2011import type { OpenClawConfig } from "../config/types.openclaw.js" ;
2112import { pruneMapToMaxSize } from "../infra/map-size.js" ;
22- import type { ProviderPlugin } from "../plugins/types.js" ;
2313import type { ModelProviderConfig } from "./provider-model-shared.js" ;
2414
2515export type {
@@ -29,9 +19,14 @@ export type {
2919} from "../plugins/types.js" ;
3020
3121export {
22+ buildManifestModelProviderConfig ,
23+ buildManifestProviderCatalogFamily ,
3224 buildPairedProviderApiKeyCatalog ,
3325 buildSingleProviderApiKeyCatalog ,
3426 findCatalogTemplate ,
27+ readManifestProviderDefaultModelRef ,
28+ type ManifestProviderCatalogEntry ,
29+ type ManifestProviderCatalogSurface ,
3530} from "../plugins/provider-catalog.js" ;
3631
3732/**
@@ -119,198 +114,6 @@ export function clearLiveCatalogCacheForTests(): void {
119114 liveCatalogCache . clear ( ) ;
120115}
121116
122- function countRawManifestCatalogModels ( catalog : unknown ) : number | undefined {
123- if ( ! catalog || typeof catalog !== "object" ) {
124- return undefined ;
125- }
126- const models = ( catalog as { models ?: unknown } ) . models ;
127- return Array . isArray ( models ) ? models . length : undefined ;
128- }
129-
130- /** Reads a provider's normalized manifest default as a fully qualified model ref. */
131- export function readManifestProviderDefaultModelRef (
132- manifest : unknown ,
133- providerId : string ,
134- ) : string | undefined {
135- const catalog = ( manifest as { modelCatalog ?: { providers ?: Record < string , unknown > } } )
136- ?. modelCatalog ?. providers ?. [ providerId ] ;
137- const defaultModel = normalizeOptionalString (
138- ( catalog as { defaultModel ?: unknown } ) ?. defaultModel ,
139- ) ;
140- return defaultModel ? buildModelCatalogRef ( providerId , defaultModel ) : undefined ;
141- }
142-
143- function cloneManifestCatalogTieredCost (
144- tier : ModelCatalogTieredCost ,
145- ) : NonNullable < ModelDefinitionConfig [ "cost" ] [ "tieredPricing" ] > [ number ] {
146- return {
147- input : tier . input ,
148- output : tier . output ,
149- cacheRead : tier . cacheRead ,
150- cacheWrite : tier . cacheWrite ,
151- range : tier . range . length === 1 ? [ tier . range [ 0 ] ] : [ tier . range [ 0 ] , tier . range [ 1 ] ] ,
152- } ;
153- }
154-
155- function cloneManifestCatalogCost ( cost : ModelCatalogCost ) : ModelDefinitionConfig [ "cost" ] {
156- return {
157- input : cost . input ?? 0 ,
158- output : cost . output ?? 0 ,
159- cacheRead : cost . cacheRead ?? 0 ,
160- cacheWrite : cost . cacheWrite ?? 0 ,
161- ...( cost . tieredPricing
162- ? { tieredPricing : cost . tieredPricing . map ( cloneManifestCatalogTieredCost ) }
163- : { } ) ,
164- } ;
165- }
166-
167- function buildManifestCatalogModelInput ( model : ModelCatalogModel ) : ModelDefinitionConfig [ "input" ] {
168- if ( model . input ?. includes ( "document" ) ) {
169- throw new Error (
170- `Manifest modelCatalog row ${ model . id } uses unsupported runtime input document` ,
171- ) ;
172- }
173- return model . input ?. filter ( ( item ) : item is "text" | "image" => item !== "document" ) ?? [ "text" ] ;
174- }
175-
176- function cloneManifestCatalogMediaInput (
177- mediaInput ?: ModelCatalogMediaInputConfig ,
178- ) : ModelDefinitionConfig [ "mediaInput" ] | undefined {
179- if ( ! mediaInput ?. image ) {
180- return undefined ;
181- }
182- return {
183- image : { ...mediaInput . image } ,
184- } ;
185- }
186-
187- function buildManifestCatalogModel (
188- providerId : string ,
189- model : ModelCatalogModel ,
190- ) : ModelDefinitionConfig {
191- if ( model . contextWindow === undefined ) {
192- throw new Error ( `Manifest modelCatalog row ${ model . id } is missing contextWindow` ) ;
193- }
194- if ( model . maxTokens === undefined ) {
195- throw new Error ( `Manifest modelCatalog row ${ model . id } is missing maxTokens` ) ;
196- }
197- const id = normalizeConfiguredProviderCatalogModelId ( providerId , model . id , {
198- allowManifestNormalization : false ,
199- } ) ;
200- return {
201- id,
202- name : model . name ?? id ,
203- ...( model . api ? { api : model . api } : { } ) ,
204- ...( model . baseUrl ? { baseUrl : model . baseUrl } : { } ) ,
205- reasoning : model . reasoning ?? false ,
206- input : buildManifestCatalogModelInput ( model ) ,
207- cost : cloneManifestCatalogCost ( model . cost ?? { } ) ,
208- contextWindow : model . contextWindow ,
209- ...( model . contextTokens !== undefined ? { contextTokens : model . contextTokens } : { } ) ,
210- maxTokens : model . maxTokens ,
211- ...( model . thinkingLevelMap ? { thinkingLevelMap : { ...model . thinkingLevelMap } } : { } ) ,
212- ...( model . headers ? { headers : { ...model . headers } } : { } ) ,
213- ...( model . compat ? { compat : { ...model . compat } } : { } ) ,
214- ...( model . mediaInput ? { mediaInput : cloneManifestCatalogMediaInput ( model . mediaInput ) } : { } ) ,
215- } ;
216- }
217-
218- /**
219- * Converts a plugin manifest modelCatalog provider into runtime provider config.
220- */
221- export function buildManifestModelProviderConfig ( params : {
222- /** Provider id that owns the manifest catalog rows. */
223- providerId : string ;
224- /** Raw manifest modelCatalog provider block to normalize into runtime config. */
225- catalog : unknown ;
226- } ) : ModelProviderConfig {
227- const catalog = normalizeModelCatalog (
228- { providers : { [ params . providerId ] : params . catalog } } ,
229- { ownedProviders : new Set ( [ params . providerId ] ) } ,
230- ) ?. providers ?. [ params . providerId ] ;
231- if ( ! catalog ) {
232- throw new Error ( `Missing modelCatalog.providers.${ params . providerId } ` ) ;
233- }
234- if ( ! catalog . baseUrl ) {
235- throw new Error ( `Missing modelCatalog.providers.${ params . providerId } .baseUrl` ) ;
236- }
237- const rawModelCount = countRawManifestCatalogModels ( params . catalog ) ;
238- if ( rawModelCount !== undefined && rawModelCount !== catalog . models . length ) {
239- throw new Error ( `Invalid modelCatalog.providers.${ params . providerId } .models` ) ;
240- }
241- return {
242- baseUrl : catalog . baseUrl ,
243- ...( catalog . api ? { api : catalog . api } : { } ) ,
244- ...( catalog . headers ? { headers : { ...catalog . headers } } : { } ) ,
245- models : catalog . models . map ( ( model ) => buildManifestCatalogModel ( params . providerId , model ) ) ,
246- } ;
247- }
248-
249- export type ManifestProviderCatalogSurface = {
250- id : string ;
251- label : string ;
252- catalog : unknown ;
253- } ;
254-
255- export type ManifestProviderCatalogEntry = {
256- id : string ;
257- label : string ;
258- baseUrl : string ;
259- models : ModelProviderConfig [ "models" ] ;
260- buildProvider : ( ) => ModelProviderConfig ;
261- } ;
262-
263- /** Projects an ordered family of manifest catalogs into static provider and model surfaces. */
264- export function buildManifestProviderCatalogFamily ( params : {
265- surfaces : readonly ManifestProviderCatalogSurface [ ] ;
266- docsPath ?: string ;
267- } ) {
268- const entries : ManifestProviderCatalogEntry [ ] = params . surfaces . map ( ( surface ) => {
269- const buildProvider = ( ) =>
270- buildManifestModelProviderConfig ( {
271- providerId : surface . id ,
272- catalog : surface . catalog ,
273- } ) ;
274- const provider = buildProvider ( ) ;
275- return {
276- id : surface . id ,
277- label : surface . label ,
278- baseUrl : provider . baseUrl ,
279- models : provider . models ,
280- buildProvider,
281- } ;
282- } ) ;
283- const staticDiscovery : ProviderPlugin [ ] = entries . map ( ( { id, label, buildProvider } ) => ( {
284- id,
285- label,
286- docsPath : params . docsPath ?? "/providers/models" ,
287- auth : [ ] ,
288- staticCatalog : {
289- order : "simple" ,
290- run : async ( ) => ( { provider : buildProvider ( ) } ) ,
291- } ,
292- } ) ) ;
293-
294- return {
295- entries,
296- staticDiscovery,
297- staticCatalog : async ( ) => ( {
298- providers : Object . fromEntries ( entries . map ( ( { id, buildProvider } ) => [ id , buildProvider ( ) ] ) ) ,
299- } ) ,
300- augmentModelCatalog : ( ) =>
301- entries . flatMap ( ( { id : provider , models } ) =>
302- models . map ( ( entry ) => ( {
303- provider,
304- id : entry . id ,
305- name : entry . name ,
306- reasoning : entry . reasoning ,
307- input : [ ...entry . input ] ,
308- contextWindow : entry . contextWindow ,
309- } ) ) ,
310- ) ,
311- } ;
312- }
313-
314117function normalizeConfiguredCatalogModelInput (
315118 input : unknown ,
316119) : ConfiguredProviderCatalogEntry [ "input" ] | undefined {
0 commit comments