Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ import { newContentReferencesStoreMock } from '@kbn/elastic-assistant-common/imp
import { KnowledgeBaseResource } from '@kbn/elastic-assistant-common';
import { createTrainedModelsProviderMock } from '@kbn/ml-plugin/server/shared_services/providers/__mocks__/trained_models';
import { ASSISTANT_ELSER_INFERENCE_ID } from './field_maps_configuration';
import { ensureIntegrationKnowledgeIndexEntry } from '../../ai_assistant_service/integration_knowledge_helper';

jest.mock('@kbn/ml-plugin/server/lib/node_utils');
jest.mock('../../lib/langchain/content_loaders/security_labs_loader');
jest.mock('../../ai_assistant_service/integration_knowledge_helper');
jest.mock('p-retry');
const date = '2023-03-28T22:27:28.159Z';
let logger: ReturnType<(typeof loggingSystemMock)['createLogger']>;
Expand All @@ -42,6 +44,11 @@ const mockedPRetry = pRetry as jest.MockedFunction<typeof pRetry>;
mockedPRetry.mockResolvedValue({});
const telemetry = coreMock.createSetup().analytics;

const mockEnsureIntegrationKnowledgeIndexEntry =
ensureIntegrationKnowledgeIndexEntry as jest.MockedFunction<
typeof ensureIntegrationKnowledgeIndexEntry
>;

describe('AIAssistantKnowledgeBaseDataClient', () => {
let mockOptions: KnowledgeBaseDataClientParams;
let ml: MlPluginSetup;
Expand All @@ -54,6 +61,7 @@ describe('AIAssistantKnowledgeBaseDataClient', () => {
jest.clearAllMocks();
logger = loggingSystemMock.createLogger();
mockLoadSecurityLabs.mockClear();
mockEnsureIntegrationKnowledgeIndexEntry.mockResolvedValue(true);
ml = mlPluginMock.createSetupContract() as unknown as MlPluginSetup; // Missing SharedServices mock, so manually mocking trainedModelsProvider
mockOptions = {
logger,
Expand All @@ -71,6 +79,7 @@ describe('AIAssistantKnowledgeBaseDataClient', () => {
setIsKBSetupInProgress: jest.fn().mockImplementation(() => {}),
manageGlobalKnowledgeBaseAIAssistant: true,
getTrainedModelsProvider: () => trainedModelsProviderMock,
telemetry,
};
esClientMock.search.mockReturnValue(
// @ts-expect-error not full response interface
Expand Down Expand Up @@ -301,6 +310,7 @@ describe('AIAssistantKnowledgeBaseDataClient', () => {
const client = new AIAssistantKnowledgeBaseDataClient(mockOptions);
await client.setupKnowledgeBase({});

expect(mockEnsureIntegrationKnowledgeIndexEntry).toHaveBeenCalled();
expect(loadSecurityLabs).toHaveBeenCalled();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import { ASSISTANT_ELSER_INFERENCE_ID } from './field_maps_configuration';
import type { BulkOperationError } from '../../lib/data_stream/documents_data_writer';
import { AUDIT_OUTCOME, KnowledgeBaseAuditAction, knowledgeBaseAuditEvent } from './audit_events';
import { findDocuments } from '../find';
import { ensureIntegrationKnowledgeIndexEntry } from '../../ai_assistant_service/integration_knowledge_helper';

/**
* Params for when creating KbDataClient in Request Context Factory. Useful if needing to modify
Expand All @@ -94,6 +95,7 @@ export interface KnowledgeBaseDataClientParams extends AIAssistantDataClientPara
manageGlobalKnowledgeBaseAIAssistant: boolean;
getTrainedModelsProvider: () => ReturnType<TrainedModelsProvider['trainedModelsProvider']>;
elserInferenceId?: string;
telemetry: AnalyticsServiceSetup;
Comment thread
jen-huang marked this conversation as resolved.
}
export class AIAssistantKnowledgeBaseDataClient extends AIAssistantDataClient {
constructor(public readonly options: KnowledgeBaseDataClientParams) {
Expand Down Expand Up @@ -208,6 +210,19 @@ export class AIAssistantKnowledgeBaseDataClient extends AIAssistantDataClient {
return;
}

// Ensure integration knowledge index entry exists first
try {
await ensureIntegrationKnowledgeIndexEntry(
this,
this.options.logger.get('integrationKnowledge'),
this.options.telemetry
);
} catch (error) {
this.options.logger.error(
`Failed to ensure integration knowledge index entry: ${error.message}`
);
}

try {
this.options.logger.debug('Checking if ML nodes are available...');
const mlNodesCount = await getMlNodeCount({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
elasticsearchServiceMock,
loggingSystemMock,
savedObjectsClientMock,
analyticsServiceMock,
} from '@kbn/core/server/mocks';
import type {
IndicesDataStream,
Expand Down Expand Up @@ -141,6 +142,7 @@ describe('AI Assistant Service', () => {
update: jest.fn(),
uninstall: jest.fn(),
}),
telemetry: analyticsServiceMock.createAnalyticsServiceSetup(),
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
ElasticsearchClient,
KibanaRequest,
SavedObjectsClientContract,
AnalyticsServiceSetup,
} from '@kbn/core/server';
import type { TaskManagerSetupContract } from '@kbn/task-manager-plugin/server';
import type { MlPluginSetup } from '@kbn/ml-plugin/server';
Expand Down Expand Up @@ -87,6 +88,7 @@ export interface AIAssistantServiceOpts {
taskManager: TaskManagerSetupContract;
pluginStop$: Subject<void>;
productDocManager: Promise<ProductDocBaseStartContract['management']>;
telemetry: AnalyticsServiceSetup;
}

export interface CreateAIAssistantClientParams {
Expand Down Expand Up @@ -608,6 +610,7 @@ export class AIAssistantService {
spaceId: opts.spaceId,
manageGlobalKnowledgeBaseAIAssistant: opts.manageGlobalKnowledgeBaseAIAssistant ?? false,
getTrainedModelsProvider: opts.getTrainedModelsProvider,
telemetry: this.options.telemetry,
});
}

Expand Down
Loading