Skip to content

Commit

Permalink
add global key to KB Entry API schema
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykkopycinski committed Feb 24, 2025
1 parent 00dcf79 commit 6836fc2
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,9 @@ components:
- name
- type: object
properties:
global:
description: Whether this Knowledge Base Entry is global, defaults to false
type: boolean
namespace:
description: Kibana Space, defaults to 'default' space
type: string
Expand All @@ -1321,6 +1324,7 @@ components:
type: array
required:
- namespace
- global
- users
- type: object
properties:
Expand All @@ -1339,6 +1343,9 @@ components:
allOf:
- type: object
properties:
global:
description: Whether this Knowledge Base Entry is global, defaults to false
type: boolean
name:
description: Name of the Knowledge Base Entry
type: string
Expand Down Expand Up @@ -1394,6 +1401,9 @@ components:
allOf:
- type: object
properties:
global:
description: Whether this Knowledge Base Entry is global, defaults to false
type: boolean
id:
$ref: '#/components/schemas/NonEmptyString'
name:
Expand Down Expand Up @@ -1484,6 +1494,9 @@ components:
- name
- type: object
properties:
global:
description: Whether this Knowledge Base Entry is global, defaults to false
type: boolean
namespace:
description: Kibana Space, defaults to 'default' space
type: string
Expand All @@ -1497,6 +1510,7 @@ components:
type: array
required:
- namespace
- global
- users
- type: object
properties:
Expand All @@ -1515,6 +1529,9 @@ components:
allOf:
- type: object
properties:
global:
description: Whether this Knowledge Base Entry is global, defaults to false
type: boolean
name:
description: Name of the Knowledge Base Entry
type: string
Expand Down Expand Up @@ -1583,6 +1600,9 @@ components:
allOf:
- type: object
properties:
global:
description: Whether this Knowledge Base Entry is global, defaults to false
type: boolean
id:
$ref: '#/components/schemas/NonEmptyString'
name:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,9 @@ components:
- name
- type: object
properties:
global:
description: Whether this Knowledge Base Entry is global, defaults to false
type: boolean
namespace:
description: Kibana Space, defaults to 'default' space
type: string
Expand All @@ -1321,6 +1324,7 @@ components:
type: array
required:
- namespace
- global
- users
- type: object
properties:
Expand All @@ -1339,6 +1343,9 @@ components:
allOf:
- type: object
properties:
global:
description: Whether this Knowledge Base Entry is global, defaults to false
type: boolean
name:
description: Name of the Knowledge Base Entry
type: string
Expand Down Expand Up @@ -1394,6 +1401,9 @@ components:
allOf:
- type: object
properties:
global:
description: Whether this Knowledge Base Entry is global, defaults to false
type: boolean
id:
$ref: '#/components/schemas/NonEmptyString'
name:
Expand Down Expand Up @@ -1484,6 +1494,9 @@ components:
- name
- type: object
properties:
global:
description: Whether this Knowledge Base Entry is global, defaults to false
type: boolean
namespace:
description: Kibana Space, defaults to 'default' space
type: string
Expand All @@ -1497,6 +1510,7 @@ components:
type: array
required:
- namespace
- global
- users
- type: object
properties:
Expand All @@ -1515,6 +1529,9 @@ components:
allOf:
- type: object
properties:
global:
description: Whether this Knowledge Base Entry is global, defaults to false
type: boolean
name:
description: Name of the Knowledge Base Entry
type: string
Expand Down Expand Up @@ -1583,6 +1600,9 @@ components:
allOf:
- type: object
properties:
global:
description: Whether this Knowledge Base Entry is global, defaults to false
type: boolean
id:
$ref: '#/components/schemas/NonEmptyString'
name:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ export const BaseDefaultableFields = z.object({
* Kibana Space, defaults to 'default' space
*/
namespace: z.string().optional(),
/**
* Whether this Knowledge Base Entry is global, defaults to false
*/
global: z.boolean().optional(),
/**
* Users who have access to the Knowledge Base Entry, defaults to current user. Empty array provides access to all users.
*/
users: z.array(User).nullable().optional(),
users: z.array(User).optional(),
});

export type BaseCreateProps = z.infer<typeof BaseCreateProps>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ components:
namespace:
type: string
description: Kibana Space, defaults to 'default' space
global:
type: boolean
description: Whether this Knowledge Base Entry is global, defaults to false
users:
type: array
nullable: true
description: Users who have access to the Knowledge Base Entry, defaults to current user. Empty array provides access to all users.
items:
$ref: "../../common_attributes.schema.yaml#/components/schemas/User"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export const getKnowledgeBaseEntryMock = (
createdAt: '2020-04-20T15:25:31.830Z',
updatedAt: '2020-04-20T15:25:31.830Z',
namespace: 'default',
global: false,
users: [
{
name: 'my_username',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
KnowledgeBaseEntryResponse,
KnowledgeBaseEntryUpdateProps,
} from '@kbn/elastic-assistant-common';
import { isArray } from 'lodash';
import { AUDIT_OUTCOME, KnowledgeBaseAuditAction, knowledgeBaseAuditEvent } from './audit_events';
import {
CREATE_KNOWLEDGE_BASE_ENTRY_ERROR_EVENT,
Expand Down Expand Up @@ -47,7 +48,6 @@ export const createKnowledgeBaseEntry = async ({
user,
knowledgeBaseEntry,
logger,
global = false,
telemetry,
}: CreateKnowledgeBaseEntryParams): Promise<KnowledgeBaseEntryResponse | null> => {
const createdAt = new Date().toISOString();
Expand All @@ -56,7 +56,6 @@ export const createKnowledgeBaseEntry = async ({
spaceId,
user,
entry: knowledgeBaseEntry as unknown as KnowledgeBaseEntryCreateProps,
global,
});
const telemetryPayload = {
entryType: body.type,
Expand Down Expand Up @@ -112,23 +111,24 @@ interface TransformToUpdateSchemaProps {
user: AuthenticatedUser;
updatedAt: string;
entry: KnowledgeBaseEntryUpdateProps;
global?: boolean;
}

export const transformToUpdateSchema = ({
user,
updatedAt,
entry,
global = false,
}: TransformToUpdateSchemaProps): UpdateKnowledgeBaseEntrySchema => {
const base = {
id: entry.id,
updated_at: updatedAt,
updated_by: user.profile_uid ?? 'unknown',
name: entry.name,
type: entry.type,
users: global
global: entry.global,
users: entry.global
? []
: isArray(entry.users) && entry.users.length
? entry.users
: [
{
id: user.profile_uid,
Expand Down Expand Up @@ -178,15 +178,13 @@ interface TransformToCreateSchemaProps {
spaceId: string;
user: AuthenticatedUser;
entry: KnowledgeBaseEntryCreateProps;
global?: boolean;
}

export const transformToCreateSchema = ({
createdAt,
spaceId,
user,
entry,
global = false,
}: TransformToCreateSchemaProps): CreateKnowledgeBaseEntrySchema => {
const base = {
'@timestamp': createdAt,
Expand All @@ -197,8 +195,11 @@ export const transformToCreateSchema = ({
name: entry.name,
namespace: spaceId,
type: entry.type,
users: global
global: entry.global,
users: entry.global
? []
: isArray(entry.users) && entry.users.length
? entry.users
: [
{
id: user.profile_uid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ describe('AIAssistantKnowledgeBaseDataClient', () => {
mockOptions.manageGlobalKnowledgeBaseAIAssistant = false;

await expect(
client.createKnowledgeBaseEntry({ telemetry, knowledgeBaseEntry, global: true })
client.createKnowledgeBaseEntry({ telemetry, knowledgeBaseEntry })
).rejects.toThrow('User lacks privileges to create global knowledge base entries');
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import pRetry from 'p-retry';
import { StructuredTool } from '@langchain/core/tools';
import { AnalyticsServiceSetup, AuditLogger, ElasticsearchClient } from '@kbn/core/server';
import { IndexPatternsFetcher } from '@kbn/data-views-plugin/server';
import { map } from 'lodash';
import { isArray, map } from 'lodash';
import { AIAssistantDataClient, AIAssistantDataClientParams } from '..';
import { GetElser } from '../../types';
import {
Expand Down Expand Up @@ -434,8 +434,8 @@ export class AIAssistantKnowledgeBaseDataClient extends AIAssistantDataClient {
kbResource: doc.metadata.kbResource ?? 'unknown',
required: doc.metadata.required ?? false,
source: doc.metadata.source ?? 'unknown',
global,
},
global,
});
}),
authenticatedUser,
Expand Down Expand Up @@ -658,11 +658,9 @@ export class AIAssistantKnowledgeBaseDataClient extends AIAssistantDataClient {
auditLogger,
knowledgeBaseEntry,
telemetry,
global = false,
}: {
auditLogger?: AuditLogger;
knowledgeBaseEntry: KnowledgeBaseEntryCreateProps;
global?: boolean;
telemetry: AnalyticsServiceSetup;
}): Promise<KnowledgeBaseEntryResponse | null> => {
const authenticatedUser = this.options.currentUser;
Expand All @@ -673,7 +671,12 @@ export class AIAssistantKnowledgeBaseDataClient extends AIAssistantDataClient {
);
}

if (global && !this.options.manageGlobalKnowledgeBaseAIAssistant) {
const globalEntry =
(isArray(knowledgeBaseEntry.users) && knowledgeBaseEntry.users.length === 0) ||
knowledgeBaseEntry.global ||
false;

if (globalEntry && !this.options.manageGlobalKnowledgeBaseAIAssistant) {
throw new Error('User lacks privileges to create global knowledge base entries');
}

Expand All @@ -690,7 +693,6 @@ export class AIAssistantKnowledgeBaseDataClient extends AIAssistantDataClient {
spaceId: this.spaceId,
user: authenticatedUser,
knowledgeBaseEntry,
global,
telemetry,
});
};
Expand Down Expand Up @@ -736,7 +738,6 @@ export class AIAssistantKnowledgeBaseDataClient extends AIAssistantDataClient {
user: authenticatedUser,
updatedAt: changedAt,
entry: knowledgeBaseEntry,
global: knowledgeBaseEntry.users != null && knowledgeBaseEntry.users.length === 0,
}),
],
getUpdateScript: (entry: UpdateKnowledgeBaseEntrySchema) => getUpdateScript({ entry }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const transformEsSchemaToEntry = (
createdBy: esKbEntry.created_by,
updatedAt: esKbEntry.updated_at,
updatedBy: esKbEntry.updated_by,
global: !esKbEntry.users?.length,
users:
esKbEntry.users?.map((user) => ({
id: user.id,
Expand Down Expand Up @@ -80,6 +81,7 @@ const transformEsSchemaToEntry = (
createdBy: esKbEntry.created_by,
updatedAt: esKbEntry.updated_at,
updatedBy: esKbEntry.updated_by,
global: !esKbEntry.users?.length,
users:
esKbEntry.users?.map((user) => ({
id: user.id,
Expand Down Expand Up @@ -117,6 +119,7 @@ const getDocumentEntryFromLegacyKbEntry = (
createdBy: legacyEsKbDoc.created_by,
updatedAt: legacyEsKbDoc.updated_at,
updatedBy: legacyEsKbDoc.updated_by,
global: !legacyEsKbDoc.users?.length,
users:
legacyEsKbDoc.users?.map((user) => ({
id: user.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export const bulkActionKnowledgeBaseEntriesRoute = (router: ElasticAssistantPlug
if (body.create && body.create.length > 0) {
// RBAC validation
body.create.forEach((entry) => {
const isGlobal = entry.users != null && entry.users.length === 0;
const isGlobal = entry.global ?? (entry.users != null && entry.users.length === 0);
if (isGlobal && !manageGlobalKnowledgeBaseAIAssistant) {
throw new Error(`User lacks privileges to create global knowledge base entries`);
}
Expand Down Expand Up @@ -276,7 +276,6 @@ export const bulkActionKnowledgeBaseEntriesRoute = (router: ElasticAssistantPlug
spaceId,
user: authenticatedUser,
entry,
global: entry.users != null && entry.users.length === 0,
})
),
documentsToDelete: body.delete?.ids,
Expand All @@ -285,7 +284,6 @@ export const bulkActionKnowledgeBaseEntriesRoute = (router: ElasticAssistantPlug
user: authenticatedUser,
updatedAt: changedAt,
entry,
global: entry.users != null && entry.users.length === 0,
})
),
getUpdateScript: (entry: UpdateKnowledgeBaseEntrySchema) => getUpdateScript({ entry }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export const createKnowledgeBaseEntryRoute = (router: ElasticAssistantPluginRout
logger.debug(() => `Creating KB Entry:\n${JSON.stringify(request.body)}`);
const createResponse = await kbDataClient?.createKnowledgeBaseEntry({
knowledgeBaseEntry: request.body,
global: request.body.users != null && request.body.users.length === 0,
auditLogger: ctx.elasticAssistant.auditLogger,
telemetry: ctx.elasticAssistant.telemetry,
});
Expand Down
Loading

0 comments on commit 6836fc2

Please sign in to comment.