[8.17] [Obs AI Assistant] Make KB retrieval namespace specific (#213505) (#214004)

This commit is contained in:
Viduni Wickramarachchi 2025-03-11 19:43:09 -04:00 committed by GitHub
parent 5e62ac5aa9
commit 13cb2ff1c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 176 additions and 21 deletions

View file

@ -829,7 +829,12 @@ export class ObservabilityAIAssistantClient {
sortBy: string;
sortDirection: 'asc' | 'desc';
}) => {
return this.dependencies.knowledgeBaseService.getEntries({ query, sortBy, sortDirection });
return this.dependencies.knowledgeBaseService.getEntries({
query,
sortBy,
sortDirection,
namespace: this.dependencies.namespace,
});
};
deleteKnowledgeBaseEntry = async (id: string) => {

View file

@ -19,6 +19,7 @@ import {
} from '../../../common/types';
import { getAccessQuery } from '../util/get_access_query';
import { getCategoryQuery } from '../util/get_category_query';
import { getSpaceQuery } from '../util/get_space_query';
import {
createInferenceEndpoint,
deleteInferenceEndpoint,
@ -252,14 +253,17 @@ export class KnowledgeBaseService {
query,
sortBy,
sortDirection,
namespace,
}: {
query?: string;
sortBy?: string;
sortDirection?: 'asc' | 'desc';
namespace: string;
}): Promise<{ entries: KnowledgeBaseEntry[] }> => {
if (!this.dependencies.config.enableKnowledgeBase) {
return { entries: [] };
}
try {
const response = await this.dependencies.esClient.asInternalUser.search<
KnowledgeBaseEntry & { doc_id?: string }
@ -274,8 +278,12 @@ export class KnowledgeBaseService {
: []),
{
// exclude user instructions
bool: { must_not: { term: { type: KnowledgeBaseType.UserInstruction } } },
bool: {
must_not: { term: { type: KnowledgeBaseType.UserInstruction } },
},
},
// filter by space
...getSpaceQuery({ namespace }),
],
},
},

View file

@ -0,0 +1,19 @@
/*
* 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.
*/
export function getSpaceQuery({ namespace }: { namespace?: string }) {
return [
{
bool: {
should: [
{ term: { namespace } },
{ bool: { must_not: { exists: { field: 'namespace' } } } },
],
},
},
];
}

View file

@ -34,6 +34,7 @@ export function createObservabilityAIAssistantApiClient(st: supertest.Agent) {
options: {
type?: 'form-data';
endpoint: TEndpoint;
spaceId?: string;
} & ObservabilityAIAssistantAPIClientRequestParamsOf<TEndpoint> & {
params?: { query?: { _inspect?: boolean } };
}
@ -43,7 +44,8 @@ export function createObservabilityAIAssistantApiClient(st: supertest.Agent) {
const params = 'params' in options ? (options.params as Record<string, any>) : {};
const { method, pathname, version } = formatRequest(endpoint, params.path);
const url = format({ pathname, query: params?.query });
const pathnameWithSpaceId = options.spaceId ? `/s/${options.spaceId}${pathname}` : pathname;
const url = format({ pathname: pathnameWithSpaceId, query: params?.query });
const headers: Record<string, string> = { 'kbn-xsrf': 'foo' };

View file

@ -21,7 +21,32 @@ export default function ApiTest({ getService }: FtrProviderContext) {
const es = getService('es');
const observabilityAIAssistantAPIClient = getService('observabilityAIAssistantAPIClient');
describe('Knowledge base', () => {
async function getEntries({
query = '',
sortBy = 'title',
sortDirection = 'asc',
spaceId,
user = 'editor',
}: {
query?: string;
sortBy?: string;
sortDirection?: 'asc' | 'desc';
spaceId?: string;
user?: 'admin' | 'editor' | 'viewer';
} = {}): Promise<KnowledgeBaseEntry[]> {
const res = await observabilityAIAssistantAPIClient[user]({
endpoint: 'GET /internal/observability_ai_assistant/kb/entries',
params: {
query: { query, sortBy, sortDirection },
},
spaceId,
});
expect(res.status).to.be(200);
return omitCategories(res.body.entries);
}
describe('Knowledge base', function () {
before(async () => {
await createKnowledgeBaseModel(ml);
@ -133,23 +158,6 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});
describe('when managing multiple entries', () => {
async function getEntries({
query = '',
sortBy = 'title',
sortDirection = 'asc',
}: { query?: string; sortBy?: string; sortDirection?: 'asc' | 'desc' } = {}) {
const res = await observabilityAIAssistantAPIClient
.editor({
endpoint: 'GET /internal/observability_ai_assistant/kb/entries',
params: {
query: { query, sortBy, sortDirection },
},
})
.expect(200);
return omitCategories(res.body.entries);
}
beforeEach(async () => {
await clearKnowledgeBase(es);
@ -210,6 +218,119 @@ export default function ApiTest({ getService }: FtrProviderContext) {
expect(entries[0].title).to.eql('My title b');
});
});
describe('when managing multiple entries across spaces', () => {
const SPACE_A_ID = 'space_a';
const SPACE_B_ID = 'space_b';
before(async () => {
await clearKnowledgeBase(es);
const { status: importStatusForSpaceA } = await observabilityAIAssistantAPIClient.admin({
endpoint: 'POST /internal/observability_ai_assistant/kb/entries/import',
params: {
body: {
entries: [
{
id: 'my-doc-1',
title: `Entry in Space A by Admin 1`,
text: `This is a public entry in Space A created by Admin`,
public: true,
},
{
id: 'my-doc-2',
title: `Entry in Space A by Admin 2`,
text: `This is a private entry in Space A created by Admin`,
public: false,
},
],
},
},
spaceId: SPACE_A_ID,
});
expect(importStatusForSpaceA).to.be(200);
const { status: importStatusForSpaceB } = await observabilityAIAssistantAPIClient.admin({
endpoint: 'POST /internal/observability_ai_assistant/kb/entries/import',
params: {
body: {
entries: [
{
id: 'my-doc-3',
title: `Entry in Space B by Admin 3`,
text: `This is a public entry in Space B created by Admin`,
public: true,
},
{
id: 'my-doc-4',
title: `Entry in Space B by Admin 4`,
text: `This is a private entry in Space B created by Admin`,
public: false,
},
],
},
},
spaceId: SPACE_B_ID,
});
expect(importStatusForSpaceB).to.be(200);
});
after(async () => {
await clearKnowledgeBase(es);
});
it('ensures users can only access entries relevant to their namespace', async () => {
// User (admin) in space A should only see entries in space A
const spaceAEntries = await getEntries({
user: 'admin',
spaceId: SPACE_A_ID,
});
expect(spaceAEntries.length).to.be(2);
expect(spaceAEntries[0].id).to.equal('my-doc-1');
expect(spaceAEntries[0].public).to.be(true);
expect(spaceAEntries[0].title).to.equal('Entry in Space A by Admin 1');
expect(spaceAEntries[1].id).to.equal('my-doc-2');
expect(spaceAEntries[1].public).to.be(false);
expect(spaceAEntries[1].title).to.equal('Entry in Space A by Admin 2');
// User (admin) in space B should only see entries in space B
const spaceBEntries = await getEntries({
user: 'admin',
spaceId: SPACE_B_ID,
});
expect(spaceBEntries.length).to.be(2);
expect(spaceBEntries[0].id).to.equal('my-doc-3');
expect(spaceBEntries[0].public).to.be(true);
expect(spaceBEntries[0].title).to.equal('Entry in Space B by Admin 3');
expect(spaceBEntries[1].id).to.equal('my-doc-4');
expect(spaceBEntries[1].public).to.be(false);
expect(spaceBEntries[1].title).to.equal('Entry in Space B by Admin 4');
});
it('should allow a user who is not the owner of the entries to access entries relevant to their namespace', async () => {
// User (editor) in space B should only see entries in space B
const spaceBEntries = await getEntries({
user: 'editor',
spaceId: SPACE_B_ID,
});
expect(spaceBEntries.length).to.be(2);
expect(spaceBEntries[0].id).to.equal('my-doc-3');
expect(spaceBEntries[0].public).to.be(true);
expect(spaceBEntries[0].title).to.equal('Entry in Space B by Admin 3');
expect(spaceBEntries[1].id).to.equal('my-doc-4');
expect(spaceBEntries[1].public).to.be(false);
expect(spaceBEntries[1].title).to.equal('Entry in Space B by Admin 4');
});
});
});
}