@@ -16,6 +16,42 @@ export type ToolParameterSchemaOptions = {
1616 modelCompat ?: ModelCompatConfig ;
1717} ;
1818
19+ const MAX_TOOL_PARAMETER_SCHEMA_CACHE_ENTRIES_PER_SCHEMA = 8 ;
20+ const toolParameterSchemaCache = new WeakMap < object , Array < { key : string ; value : TSchema } > > ( ) ;
21+
22+ function resolveToolParameterSchemaCacheKey (
23+ options : ToolParameterSchemaOptions | undefined ,
24+ ) : string {
25+ const normalizedProvider = normalizeLowercaseStringOrEmpty ( options ?. modelProvider ) ;
26+ const normalizedModelId = normalizeLowercaseStringOrEmpty ( options ?. modelId ) ;
27+ const unsupportedKeywords = [
28+ ...resolveUnsupportedToolSchemaKeywords ( options ?. modelCompat ) ,
29+ ] . sort ( ) ;
30+ const omitEmptyArrayItems = shouldOmitEmptyArrayItems ( options ?. modelCompat ) ;
31+ return JSON . stringify ( [
32+ normalizedProvider ,
33+ normalizedModelId ,
34+ unsupportedKeywords ,
35+ omitEmptyArrayItems ,
36+ ] ) ;
37+ }
38+
39+ function getCachedToolParameterSchema ( schema : object , key : string ) : TSchema | undefined {
40+ return toolParameterSchemaCache . get ( schema ) ?. find ( ( entry ) => entry . key === key ) ?. value ;
41+ }
42+
43+ function rememberCachedToolParameterSchema ( schema : object , key : string , value : TSchema ) : TSchema {
44+ const entries = toolParameterSchemaCache . get ( schema ) ?? [ ] ;
45+ toolParameterSchemaCache . set (
46+ schema ,
47+ [ { key, value } , ...entries . filter ( ( entry ) => entry . key !== key ) ] . slice (
48+ 0 ,
49+ MAX_TOOL_PARAMETER_SCHEMA_CACHE_ENTRIES_PER_SCHEMA ,
50+ ) ,
51+ ) ;
52+ return value ;
53+ }
54+
1955function extractEnumValues ( schema : unknown ) : unknown [ ] | undefined {
2056 if ( ! schema || typeof schema !== "object" ) {
2157 return undefined ;
@@ -705,9 +741,9 @@ function normalizeOpenApiSchemaKeywords(schema: unknown): unknown {
705741 return changed || nullable ? normalized : schema ;
706742}
707743
708- export function normalizeToolParameterSchema (
744+ function normalizeToolParameterSchemaUncached (
709745 schema : unknown ,
710- options ?: { modelProvider ?: string ; modelId ?: string ; modelCompat ?: ModelCompatConfig } ,
746+ options ?: ToolParameterSchemaOptions ,
711747) : TSchema {
712748 const inlinedSchema = normalizeOpenApiSchemaKeywords ( inlineLocalToolSchemaRefs ( schema ) ) ;
713749 const schemaRecord =
@@ -844,3 +880,22 @@ export function normalizeToolParameterSchema(
844880 // Merging properties preserves useful enums like `action` while keeping schemas portable.
845881 return applyProviderCleaning ( flattenedSchema ) ;
846882}
883+
884+ export function normalizeToolParameterSchema (
885+ schema : unknown ,
886+ options ?: ToolParameterSchemaOptions ,
887+ ) : TSchema {
888+ if ( ! schema || typeof schema !== "object" ) {
889+ return normalizeToolParameterSchemaUncached ( schema , options ) ;
890+ }
891+ const cacheKey = resolveToolParameterSchemaCacheKey ( options ) ;
892+ const cached = getCachedToolParameterSchema ( schema , cacheKey ) ;
893+ if ( cached ) {
894+ return cached ;
895+ }
896+ return rememberCachedToolParameterSchema (
897+ schema ,
898+ cacheKey ,
899+ normalizeToolParameterSchemaUncached ( schema , options ) ,
900+ ) ;
901+ }
0 commit comments