From 9e2ae1c004103aa7537f06073f9b1d97c48ff7b1 Mon Sep 17 00:00:00 2001 From: Daniel Salazar Date: Thu, 11 Dec 2025 17:13:25 -0800 Subject: [PATCH] fix: bad ai calls (#2144) --- .../src/services/ai/utils/FunctionCalling.js | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/backend/src/services/ai/utils/FunctionCalling.js b/src/backend/src/services/ai/utils/FunctionCalling.js index ad8df58b0..d9046617a 100644 --- a/src/backend/src/services/ai/utils/FunctionCalling.js +++ b/src/backend/src/services/ai/utils/FunctionCalling.js @@ -1,3 +1,29 @@ + +export const normalize_json_schema = (schema) => { + if ( ! schema ) return schema; + + if ( schema.type === 'object' ) { + if ( ! schema.properties ) { + return schema; + } + + const keys = Object.keys(schema.properties); + for ( const key of keys ) { + schema.properties[key] = normalize_json_schema(schema.properties[key]); + } + } + + if ( schema.type === 'array' ) { + if ( ! schema.items ) { + schema.items = {}; + } else { + schema.items = normalize_json_schema(schema.items); + } + } + + return schema; +}; + /** * Normalizes the 'tools' object in-place. * @@ -28,7 +54,7 @@ export const normalize_tools_object = (tools) => { }; if ( parameters.properties ) { - parameters = this.normalize_json_schema(parameters); + parameters = normalize_json_schema(parameters); } if ( fn.name ) { @@ -64,31 +90,6 @@ export const normalize_tools_object = (tools) => { return tools; }; -export const normalize_json_schema = (schema) => { - if ( ! schema ) return schema; - - if ( schema.type === 'object' ) { - if ( ! schema.properties ) { - return schema; - } - - const keys = Object.keys(schema.properties); - for ( const key of keys ) { - schema.properties[key] = this.normalize_json_schema(schema.properties[key]); - } - } - - if ( schema.type === 'array' ) { - if ( ! schema.items ) { - schema.items = {}; - } else { - schema.items = this.normalize_json_schema(schema.items); - } - } - - return schema; -}; - /** * This function will convert a normalized tools object to the * format expected by OpenAI.