forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security GenAI][BUG] Knowledge Base: Show only indices with `semanti…
…c_text` fields (elastic#198707) ## Summary This is a fix the next issue: > Index input should only list indices with semantic_text fields, not all indices. ### Current behaviour We show all available indices <img width="1311" alt="Screenshot 2024-11-01 at 18 14 36" src="https://github.com/user-attachments/assets/cf9d08fd-a809-4530-b653-d12b8e643e45"> ### Behaviour after the fix We show only indices with `semantic_text` fields <img width="1311" alt="Screenshot 2024-11-01 at 18 08 29" src="https://github.com/user-attachments/assets/864b5552-aece-4cc6-848a-8f73f88f55dc"> ### Testing notes Create some indices with `semantic_text` fields. For example, you can do that via uploading and indexing a PDF file: 1. Navigate to Integrations page 2. Select "Upload a file" 3. Select and upload a PDF file 4. Press Import button 5. Switch to Advanced tab 6. Fill in "Index name" 7. Add additional field > Add semantic text field > Fill in form * Field: `attachment.content` * Copy to field: `content` * Inference endpoint: `elser_model_2` 8. Press Add button 9. Press Import button ### Checklist Delete any items that are not applicable to this PR. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios (cherry picked from commit b122722)
- Loading branch information
Showing
23 changed files
with
738 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...stic-assistant-common/impl/schemas/knowledge_base/get_knowledge_base_indices_route.gen.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
/* | ||
* NOTICE: Do not edit this file manually. | ||
* This file is automatically generated by the OpenAPI Generator, @kbn/openapi-generator. | ||
* | ||
* info: | ||
* title: Get Knowledge Base Indices API endpoints | ||
* version: 1 | ||
*/ | ||
|
||
import { z } from '@kbn/zod'; | ||
|
||
export type GetKnowledgeBaseIndicesResponse = z.infer<typeof GetKnowledgeBaseIndicesResponse>; | ||
export const GetKnowledgeBaseIndicesResponse = z.object({ | ||
/** | ||
* List of indices with at least one field of a `sematic_text` type. | ||
*/ | ||
indices: z.array(z.string()), | ||
}); |
42 changes: 42 additions & 0 deletions
42
...assistant-common/impl/schemas/knowledge_base/get_knowledge_base_indices_route.schema.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
openapi: 3.0.0 | ||
info: | ||
title: Get Knowledge Base Indices API endpoints | ||
version: '1' | ||
paths: | ||
/internal/elastic_assistant/knowledge_base/_indices: | ||
get: | ||
x-codegen-enabled: true | ||
x-labels: [ess, serverless] | ||
operationId: GetKnowledgeBaseIndices | ||
description: Gets Knowledge Base indices that have fields of a `sematic_text` type. | ||
summary: Gets Knowledge Base indices that have fields of a `sematic_text` type. | ||
tags: | ||
- KnowledgeBase API | ||
responses: | ||
200: | ||
description: Indicates a successful call. | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
indices: | ||
type: array | ||
description: List of indices with at least one field of a `sematic_text` type. | ||
items: | ||
type: string | ||
required: | ||
- indices | ||
400: | ||
description: Generic Error | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
statusCode: | ||
type: number | ||
error: | ||
type: string | ||
message: | ||
type: string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...n-elastic-assistant/impl/assistant/api/knowledge_base/use_knowledge_base_indices.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import { | ||
useKnowledgeBaseIndices, | ||
UseKnowledgeBaseIndicesParams, | ||
} from './use_knowledge_base_indices'; | ||
import { getKnowledgeBaseIndices as _getKnowledgeBaseIndices } from './api'; | ||
|
||
const getKnowledgeBaseIndicesMock = _getKnowledgeBaseIndices as jest.Mock; | ||
|
||
jest.mock('./api', () => { | ||
const actual = jest.requireActual('./api'); | ||
return { | ||
...actual, | ||
getKnowledgeBaseIndices: jest.fn((...args) => actual.getKnowledgeBaseIndices(...args)), | ||
}; | ||
}); | ||
|
||
jest.mock('@tanstack/react-query', () => ({ | ||
useQuery: jest.fn().mockImplementation(async (queryKey, fn, opts) => { | ||
try { | ||
const res = await fn({}); | ||
return Promise.resolve(res); | ||
} catch (e) { | ||
opts.onError(e); | ||
} | ||
}), | ||
})); | ||
|
||
const indicesResponse = ['index-1', 'index-2', 'index-3']; | ||
|
||
const http = { | ||
fetch: jest.fn().mockResolvedValue(indicesResponse), | ||
}; | ||
const toasts = { | ||
addError: jest.fn(), | ||
}; | ||
const defaultProps = { http, toasts } as unknown as UseKnowledgeBaseIndicesParams; | ||
describe('useKnowledgeBaseIndices', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call api to get knowledge base indices', async () => { | ||
await act(async () => { | ||
const { waitForNextUpdate } = renderHook(() => useKnowledgeBaseIndices(defaultProps)); | ||
await waitForNextUpdate(); | ||
|
||
expect(defaultProps.http.fetch).toHaveBeenCalledWith( | ||
'/internal/elastic_assistant/knowledge_base/_indices', | ||
{ | ||
method: 'GET', | ||
signal: undefined, | ||
version: '1', | ||
} | ||
); | ||
expect(toasts.addError).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('should return indices response', async () => { | ||
await act(async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => useKnowledgeBaseIndices(defaultProps)); | ||
await waitForNextUpdate(); | ||
|
||
await expect(result.current).resolves.toStrictEqual(indicesResponse); | ||
}); | ||
}); | ||
|
||
it('should display error toast when api throws error', async () => { | ||
getKnowledgeBaseIndicesMock.mockRejectedValue(new Error('this is an error')); | ||
await act(async () => { | ||
const { waitForNextUpdate } = renderHook(() => useKnowledgeBaseIndices(defaultProps)); | ||
await waitForNextUpdate(); | ||
|
||
expect(toasts.addError).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
59 changes: 59 additions & 0 deletions
59
...es/kbn-elastic-assistant/impl/assistant/api/knowledge_base/use_knowledge_base_indices.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { UseQueryResult } from '@tanstack/react-query'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import type { HttpSetup, IHttpFetchError, ResponseErrorBody } from '@kbn/core-http-browser'; | ||
import type { IToasts } from '@kbn/core-notifications-browser'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { GetKnowledgeBaseIndicesResponse } from '@kbn/elastic-assistant-common'; | ||
import { getKnowledgeBaseIndices } from './api'; | ||
|
||
const KNOWLEDGE_BASE_INDICES_QUERY_KEY = ['elastic-assistant', 'knowledge-base-indices']; | ||
|
||
export interface UseKnowledgeBaseIndicesParams { | ||
http: HttpSetup; | ||
toasts?: IToasts; | ||
} | ||
|
||
/** | ||
* Hook for getting indices that have fields of `semantic_text` type. | ||
* | ||
* @param {Object} options - The options object. | ||
* @param {HttpSetup} options.http - HttpSetup | ||
* @param {IToasts} [options.toasts] - IToasts | ||
* | ||
* @returns {useQuery} hook for getting indices that have fields of `semantic_text` type | ||
*/ | ||
export const useKnowledgeBaseIndices = ({ | ||
http, | ||
toasts, | ||
}: UseKnowledgeBaseIndicesParams): UseQueryResult< | ||
GetKnowledgeBaseIndicesResponse, | ||
IHttpFetchError | ||
> => { | ||
return useQuery( | ||
KNOWLEDGE_BASE_INDICES_QUERY_KEY, | ||
async ({ signal }) => { | ||
return getKnowledgeBaseIndices({ http, signal }); | ||
}, | ||
{ | ||
onError: (error: IHttpFetchError<ResponseErrorBody>) => { | ||
if (error.name !== 'AbortError') { | ||
toasts?.addError( | ||
error.body && error.body.message ? new Error(error.body.message) : error, | ||
{ | ||
title: i18n.translate('xpack.elasticAssistant.knowledgeBase.indicesError', { | ||
defaultMessage: 'Error fetching Knowledge Base Indices', | ||
}), | ||
} | ||
); | ||
} | ||
}, | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.