From ac494d7767a9a1acb53c6f7bd4143b271f15fb49 Mon Sep 17 00:00:00 2001 From: Garrett Spong Date: Tue, 24 Sep 2024 13:00:01 -0600 Subject: [PATCH] Fix alert count not saving, fix entries being queries on stack mgmt page if FF disabled, fixed modelId override only if FF is enabled, and renamed assistant_all user --- .../env/http-client.env.json | 4 +- .../crud_knowledge_base_entries_route.http | 36 ++++++++-- .../impl/utils/spaces_roles_users_data.http | 16 ++--- .../entries/use_knowledge_base_entries.ts | 3 + .../index.tsx | 72 ++++++++++++++++--- .../knowledge_base/post_knowledge_base.ts | 4 +- 6 files changed, 107 insertions(+), 28 deletions(-) diff --git a/x-pack/packages/kbn-elastic-assistant-common/env/http-client.env.json b/x-pack/packages/kbn-elastic-assistant-common/env/http-client.env.json index 221493d016cd5..2c93670ea1f85 100644 --- a/x-pack/packages/kbn-elastic-assistant-common/env/http-client.env.json +++ b/x-pack/packages/kbn-elastic-assistant-common/env/http-client.env.json @@ -13,8 +13,8 @@ "username": "elastic", "password": "changeme" }, - "assistant_system": { - "username": "assistant_system", + "assistant_all": { + "username": "assistant_all", "password": "changeme" } }, diff --git a/x-pack/packages/kbn-elastic-assistant-common/impl/schemas/knowledge_base/entries/crud_knowledge_base_entries_route.http b/x-pack/packages/kbn-elastic-assistant-common/impl/schemas/knowledge_base/entries/crud_knowledge_base_entries_route.http index 9863b974762f3..0f75b22e66378 100644 --- a/x-pack/packages/kbn-elastic-assistant-common/impl/schemas/knowledge_base/entries/crud_knowledge_base_entries_route.http +++ b/x-pack/packages/kbn-elastic-assistant-common/impl/schemas/knowledge_base/entries/crud_knowledge_base_entries_route.http @@ -15,6 +15,27 @@ X-Kbn-Context: {{appContext.security}} "text": "My favorite food is Dan Bing" } +### Create Index Entry (Admin Global) +POST http://{{host}}:{{port}}{{basePath}}/internal/elastic_assistant/knowledge_base/entries +kbn-xsrf: "true" +Content-Type: application/json +Elastic-Api-Version: {{elasticApiVersion}} +Authorization: Basic {{auth.admin.username}} {{auth.admin.password}} +X-Kbn-Context: {{appContext.security}} + +{ + "type": "index", + "name": "SlackConnector (Admin Global)", + "namespace": "default", + "index": "slackbot-test", + "field": "semantic_text", + "description": "Use this index to search for the user's Slack messages.", + "queryDescription": + "The free text search that the user wants to perform over this dataset. So if asking \"what are my slack messages from last week about failed tests\", the query would be \"A test has failed! failing test failed test\"", + "outputFields": ["author", "text", "timestamp"], + "users": [] +} + ### Create Index Entry (Admin) POST http://{{host}}:{{port}}{{basePath}}/internal/elastic_assistant/knowledge_base/entries kbn-xsrf: "true" @@ -35,24 +56,27 @@ X-Kbn-Context: {{appContext.security}} "outputFields": ["author", "text", "timestamp"] } -### Create Index Entry (Assistant System) +### Create Index Entry (Assistant All) POST http://{{host}}:{{port}}{{basePath}}/internal/elastic_assistant/knowledge_base/entries kbn-xsrf: "true" Content-Type: application/json Elastic-Api-Version: {{elasticApiVersion}} -Authorization: Basic {{auth.assistant_system.username}} {{auth.assistant_system.password}} +Authorization: Basic {{auth.assistant_all.username}} {{auth.assistant_all.password}} X-Kbn-Context: {{appContext.security}} { "type": "index", - "name": "SlackConnector (Assistant System)", + "name": "SlackConnector (Assistant All)", "namespace": "default", "index": "slackbot-test", "field": "semantic_text", "description": "Use this index to search for the user's Slack messages.", - "queryDescription": - "The free text search that the user wants to perform over this dataset. So if asking \"what are my slack messages from last week about failed tests\", the query would be \"A test has failed! failing test failed test\"", - "outputFields": ["author", "text", "timestamp"] + "queryDescription": "The free text search that the user wants to perform over this dataset. So if asking \"what are my slack messages from last week about failed tests\", the query would be \"A test has failed! failing test failed test\"", + "outputFields": [ + "author", + "text", + "timestamp" + ] } diff --git a/x-pack/packages/kbn-elastic-assistant-common/impl/utils/spaces_roles_users_data.http b/x-pack/packages/kbn-elastic-assistant-common/impl/utils/spaces_roles_users_data.http index 26bba007b653d..16cdf270565a4 100644 --- a/x-pack/packages/kbn-elastic-assistant-common/impl/utils/spaces_roles_users_data.http +++ b/x-pack/packages/kbn-elastic-assistant-common/impl/utils/spaces_roles_users_data.http @@ -30,8 +30,8 @@ Authorization: Basic {{auth.admin.username}} {{auth.admin.password}} "imageUrl": "" } -### Create assistant_admin Role - All Spaces, All Features -PUT http://{{host}}:{{port}}{{basePath}}/api/security/role/assistant_admin +### Create Assistant All Role - All Spaces, All Features +PUT http://{{host}}:{{port}}{{basePath}}/api/security/role/assistant_all kbn-xsrf: "true" Content-Type: application/json Elastic-Api-Version: {{elasticApiVersion}} @@ -100,20 +100,20 @@ Authorization: Basic {{auth.admin.username}} {{auth.admin.password}} ] } -### Create assistant_system User - All Spaces, All Features -POST http://{{host}}:{{port}}{{basePath}}/internal/security/users/assistant_system +### Create Assistant All User - All Spaces, All Features +POST http://{{host}}:{{port}}{{basePath}}/internal/security/users/assistant_all kbn-xsrf: "true" Content-Type: application/json Elastic-Api-Version: {{elasticApiVersion}} Authorization: Basic {{auth.admin.username}} {{auth.admin.password}} { - "password": "{{auth.assistant_system.password}}", - "username": "{{auth.assistant_system.username}}", - "full_name": "Assistant System", + "password": "{{auth.assistant_all.password}}", + "username": "{{auth.assistant_all.username}}", + "full_name": "Assistant All", "email": "", "roles": [ - "assistant_admin" + "assistant_all" ] } diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_knowledge_base_entries.ts b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_knowledge_base_entries.ts index c5cf41f6e0c1c..b41119779b21d 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_knowledge_base_entries.ts +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_knowledge_base_entries.ts @@ -23,6 +23,7 @@ export interface UseKnowledgeBaseEntriesParams { query?: FindKnowledgeBaseEntriesRequestQuery; signal?: AbortSignal | undefined; toasts?: IToasts; + enabled?: boolean; // For disabling if FF is off } const defaultQuery: FindKnowledgeBaseEntriesRequestQuery = { @@ -54,6 +55,7 @@ export const useKnowledgeBaseEntries = ({ query = defaultQuery, signal, toasts, + enabled = false, }: UseKnowledgeBaseEntriesParams) => useQuery( KNOWLEDGE_BASE_ENTRY_QUERY_KEY, @@ -68,6 +70,7 @@ export const useKnowledgeBaseEntries = ({ } ), { + enabled, keepPreviousData: true, initialData: { page: 1, perPage: 100, total: 0, data: [] }, onError: (error: IHttpFetchError) => { diff --git a/x-pack/packages/kbn-elastic-assistant/impl/knowledge_base/knowledge_base_settings_management/index.tsx b/x-pack/packages/kbn-elastic-assistant/impl/knowledge_base/knowledge_base_settings_management/index.tsx index 91849b1c2d9d7..43c28bf337b2f 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/knowledge_base/knowledge_base_settings_management/index.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/knowledge_base/knowledge_base_settings_management/index.tsx @@ -27,6 +27,7 @@ import { AlertsSettingsManagement } from '../../alerts/settings/alerts_settings_ import { useKnowledgeBaseEntries } from '../../assistant/api/knowledge_base/entries/use_knowledge_base_entries'; import { useAssistantContext } from '../../assistant_context'; import { useKnowledgeBaseTable } from './use_knowledge_base_table'; +import { AssistantSettingsBottomBar } from '../../assistant/settings/assistant_settings_bottom_bar'; import { useSettingsUpdater, DEFAULT_CONVERSATIONS, @@ -48,6 +49,7 @@ import { } from './helpers'; import { useCreateKnowledgeBaseEntry } from '../../assistant/api/knowledge_base/entries/use_create_knowledge_base_entry'; import { useUpdateKnowledgeBaseEntries } from '../../assistant/api/knowledge_base/entries/use_update_knowledge_base_entries'; +import { SETTINGS_UPDATED_TOAST_TITLE } from '../../assistant/settings/translations'; export const KnowledgeBaseSettingsManagement: React.FC = React.memo(() => { const { @@ -55,15 +57,47 @@ export const KnowledgeBaseSettingsManagement: React.FC = React.memo(() => { http, toasts, } = useAssistantContext(); + const [hasPendingChanges, setHasPendingChanges] = useState(false); // Only needed for legacy settings management - const { knowledgeBase, setUpdatedKnowledgeBaseSettings } = useSettingsUpdater( - DEFAULT_CONVERSATIONS, // Knowledge Base settings do not require conversations - DEFAULT_PROMPTS, // Knowledge Base settings do not require prompts - false, // Knowledge Base settings do not require conversations - false // Knowledge Base settings do not require prompts + const { knowledgeBase, setUpdatedKnowledgeBaseSettings, resetSettings, saveSettings } = + useSettingsUpdater( + DEFAULT_CONVERSATIONS, // Knowledge Base settings do not require conversations + DEFAULT_PROMPTS, // Knowledge Base settings do not require prompts + false, // Knowledge Base settings do not require conversations + false // Knowledge Base settings do not require prompts + ); + + const handleUpdateKnowledgeBaseSettings = useCallback( + (updatedKnowledgeBase) => { + setHasPendingChanges(true); + setUpdatedKnowledgeBaseSettings(updatedKnowledgeBase); + }, + [setUpdatedKnowledgeBaseSettings] + ); + + const handleSave = useCallback( + async (param?: { callback?: () => void }) => { + await saveSettings(); + toasts?.addSuccess({ + iconType: 'check', + title: SETTINGS_UPDATED_TOAST_TITLE, + }); + setHasPendingChanges(false); + param?.callback?.(); + }, + [saveSettings, toasts] ); + const onCancelClick = useCallback(() => { + resetSettings(); + setHasPendingChanges(false); + }, [resetSettings]); + + const onSaveButtonClicked = useCallback(() => { + handleSave(); + }, [handleSave]); + const { isFlyoutOpen: isFlyoutVisible, openFlyout, closeFlyout } = useFlyoutModalVisibility(); const [selectedEntry, setSelectedEntry] = @@ -100,7 +134,11 @@ export const KnowledgeBaseSettingsManagement: React.FC = React.memo(() => { closeFlyout(); }, [closeFlyout]); - const { data: entries } = useKnowledgeBaseEntries({ http, toasts }); + const { data: entries } = useKnowledgeBaseEntries({ + http, + toasts, + enabled: enableKnowledgeBaseByDefault, + }); const { getColumns } = useKnowledgeBaseTable(); const columns = useMemo( () => @@ -169,10 +207,17 @@ export const KnowledgeBaseSettingsManagement: React.FC = React.memo(() => { if (!enableKnowledgeBaseByDefault) { return ( - + <> + + + ); } @@ -208,7 +253,12 @@ export const KnowledgeBaseSettingsManagement: React.FC = React.memo(() => { + const core = ctx.core; const soClient = core.savedObjects.getClient(); const kbResource = request.params.resource; - const modelIdOverride = request.query.modelId; // FF Check for V2 KB const v2KnowledgeBaseEnabled = isV2KnowledgeBaseEnabled({ context: ctx, request }); + // Only allow modelId override if FF is enabled as this will re-write the ingest pipeline and break any previous KB entries + // This is only really needed for API integration tests + const modelIdOverride = v2KnowledgeBaseEnabled ? request.query.modelId : undefined; try { const knowledgeBaseDataClient =