From b746cb470ee0aa0340417920330d811359283990 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Tue, 10 Dec 2024 02:49:22 +1100 Subject: [PATCH] [8.17] [Obs AI Assistant] Fix null pointer in function definition (#203344) (#203435) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Backport This will backport the following commits from `main` to `8.17`: - [[Obs AI Assistant] Fix null pointer in function definition (#203344)](https://github.com/elastic/kibana/pull/203344) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Søren Louv-Jansen --- .../kbn-ai-assistant/src/hooks/use_json_editor_model.ts | 2 +- .../src/utils/create_initialized_object.ts | 2 +- .../observability_ai_assistant/common/functions/types.ts | 4 +++- .../server/functions/query/index.ts | 9 +++++---- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/x-pack/packages/kbn-ai-assistant/src/hooks/use_json_editor_model.ts b/x-pack/packages/kbn-ai-assistant/src/hooks/use_json_editor_model.ts index 6412aeb6a870e..5cba3fbb78d75 100644 --- a/x-pack/packages/kbn-ai-assistant/src/hooks/use_json_editor_model.ts +++ b/x-pack/packages/kbn-ai-assistant/src/hooks/use_json_editor_model.ts @@ -43,7 +43,7 @@ export const useJsonEditorModel = ({ const initialJsonString = initialJsonValue ? JSON.stringify(safeJsonParse(initialJsonValue), null, 4) // prettify the json - : functionDefinition.parameters.properties + : functionDefinition.parameters?.properties ? JSON.stringify(createInitializedObject(functionDefinition.parameters), null, 4) : ''; diff --git a/x-pack/packages/kbn-ai-assistant/src/utils/create_initialized_object.ts b/x-pack/packages/kbn-ai-assistant/src/utils/create_initialized_object.ts index e06800aca07a0..22d173f7da67c 100644 --- a/x-pack/packages/kbn-ai-assistant/src/utils/create_initialized_object.ts +++ b/x-pack/packages/kbn-ai-assistant/src/utils/create_initialized_object.ts @@ -7,7 +7,7 @@ import type { FunctionDefinition } from '@kbn/observability-ai-assistant-plugin/common'; -type Params = FunctionDefinition['parameters']; +type Params = NonNullable; export function createInitializedObject(parameters: Params) { const emptyObject: Record = {}; diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant/common/functions/types.ts b/x-pack/plugins/observability_solution/observability_ai_assistant/common/functions/types.ts index bd786e9ba3c75..093aed3aa7120 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant/common/functions/types.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant/common/functions/types.ts @@ -35,7 +35,9 @@ export type FunctionResponse = } | Observable; -export interface FunctionDefinition { +export interface FunctionDefinition< + TParameters extends CompatibleJSONSchema = CompatibleJSONSchema +> { name: string; description: string; visibility?: FunctionVisibility; diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant_app/server/functions/query/index.ts b/x-pack/plugins/observability_solution/observability_ai_assistant_app/server/functions/query/index.ts index 210dee20339af..6339dd1b0e01b 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant_app/server/functions/query/index.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant_app/server/functions/query/index.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { isChatCompletionChunkEvent, isOutputEvent } from '@kbn/inference-common'; +import { ToolDefinition, isChatCompletionChunkEvent, isOutputEvent } from '@kbn/inference-common'; import { correctCommonEsqlMistakes } from '@kbn/inference-plugin/common'; import { naturalLanguageToEsql } from '@kbn/inference-plugin/server'; import { @@ -132,9 +132,10 @@ export function registerQueryFunction({ ), logger: resources.logger, tools: Object.fromEntries( - actions - .concat(esqlFunctions) - .map((fn) => [fn.name, { description: fn.description, schema: fn.parameters }]) + [...actions, ...esqlFunctions].map((fn) => [ + fn.name, + { description: fn.description, schema: fn.parameters } as ToolDefinition, + ]) ), functionCalling: useSimulatedFunctionCalling ? 'simulated' : 'native', });