Skip to content

Commit

Permalink
feature(unified-doc-viewer): register and consume ai assistant
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Antonio Ghiani committed Apr 10, 2024
1 parent a3432e1 commit bfc2d75
Show file tree
Hide file tree
Showing 21 changed files with 137 additions and 28 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@
"@kbn/discover-customization-examples-plugin": "link:examples/discover_customization_examples",
"@kbn/discover-enhanced-plugin": "link:x-pack/plugins/discover_enhanced",
"@kbn/discover-plugin": "link:src/plugins/discover",
"@kbn/discover-shared-plugin": "link:src/plugins/discover_shared",
"@kbn/discover-utils": "link:packages/kbn-discover-utils",
"@kbn/doc-links": "link:packages/kbn-doc-links",
"@kbn/dom-drag-drop": "link:packages/kbn-dom-drag-drop",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { BehaviorSubject, Observable, Subscription } from 'rxjs';
import { BaseFeature, FeaturesMap } from './types';

export class FeaturesRegistry<Feature extends BaseFeature> {
export class FeaturesRegistry<Feature extends BaseFeature = BaseFeature> {
private readonly features = new BehaviorSubject<FeaturesMap<Feature>>(new Map());

/**
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/discover_shared/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface SecurityAIAssistantFeature {

export interface ObservabilityLogsAIAssistantFeature {
id: 'observability-logs-ai-assistant';
render: (deps: {doc: DocumentOverview}) => React.ReactNode;
render: (deps: {doc: DataTableRecord}) => React.ReactNode;
// Add any prop required for the feature
}

Expand All @@ -49,7 +49,7 @@ Once we have an interface for the feature, Discover can now retrieve it and use
function LogsOverviewAIAssistant ({ doc }) {
const { discoverShared } = getUnifiedDocViewerServices();

const discoverShared = discoverShared.features.registry.getById('observability-logs-ai-assistant')
const logsAIAssistantFeature = discoverShared.features.registry.getById('observability-logs-ai-assistant')

if (logsAIAssistantFeature) {
return logsAIAssistantFeature.render({ doc })
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/discover_shared/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function plugin() {
return new DiscoverSharedPlugin();
}

export type { DiscoverSharedClientSetupExports, DiscoverSharedClientStartExports } from './types';
export type { DiscoverSharedPublicSetup, DiscoverSharedPublicStart } from './types';
export type {
ObservabilityLogsAIAssistantFeatureRenderDeps,
ObservabilityLogsAIAssistantFeature,
Expand Down
33 changes: 33 additions & 0 deletions src/plugins/discover_shared/public/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import {
createDiscoverFeaturesServiceSetupMock,
createDiscoverFeaturesServiceStartMock,
} from './services/discover_features/discover_features_service.mock';
import { DiscoverSharedPublicSetup, DiscoverSharedPublicStart } from './types';

export type Setup = jest.Mocked<DiscoverSharedPublicSetup>;
export type Start = jest.Mocked<DiscoverSharedPublicStart>;

const createSetupContract = (): Setup => {
return {
features: createDiscoverFeaturesServiceSetupMock(),
};
};

const createStartContract = (): Start => {
return {
features: createDiscoverFeaturesServiceStartMock(),
};
};

export const discoverSharedPluginMock = {
createSetupContract,
createStartContract,
};
4 changes: 2 additions & 2 deletions src/plugins/discover_shared/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/

import { DiscoverFeaturesService } from './services/discover_features';
import { DiscoverSharedClientPlugin } from './types';
import { DiscoverSharedPublicPlugin } from './types';

export class DiscoverSharedPlugin implements DiscoverSharedClientPlugin {
export class DiscoverSharedPlugin implements DiscoverSharedPublicPlugin {
private discoverFeaturesService: DiscoverFeaturesService = new DiscoverFeaturesService();

public setup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
*/

import { FeaturesRegistry } from '@kbn/discover-utils';
import { DiscoverFeature } from './types';

export const createDiscoverFeaturesServiceStartMock = () => ({
registry: new FeaturesRegistry(),
});
const registry = new FeaturesRegistry<DiscoverFeature>();

export const createDiscoverFeaturesServiceSetupMock = () => ({ registry });
export const createDiscoverFeaturesServiceStartMock = () => ({ registry });
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { DocumentOverview, FeaturesRegistry } from '@kbn/discover-utils';
import { DataTableRecord, FeaturesRegistry } from '@kbn/discover-utils';

/**
* Features types
Expand All @@ -21,11 +21,11 @@ import { DocumentOverview, FeaturesRegistry } from '@kbn/discover-utils';
*/

export interface ObservabilityLogsAIAssistantFeatureRenderDeps {
doc: DocumentOverview;
doc: DataTableRecord;
}
export interface ObservabilityLogsAIAssistantFeature {
id: 'observability-logs-ai-assistant';
render: (deps: ObservabilityLogsAIAssistantFeatureRenderDeps) => React.ReactNode;
render: (deps: ObservabilityLogsAIAssistantFeatureRenderDeps) => JSX.Element;
}

// This should be a union of all the available client features.
Expand Down
18 changes: 9 additions & 9 deletions src/plugins/discover_shared/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ import {
DiscoverFeaturesServiceStart,
} from './services/discover_features';

export interface DiscoverSharedClientSetupExports {
export interface DiscoverSharedPublicSetup {
features: DiscoverFeaturesServiceSetup;
}

export interface DiscoverSharedClientStartExports {
export interface DiscoverSharedPublicStart {
features: DiscoverFeaturesServiceStart;
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface DiscoverSharedClientPluginSetupDeps {}
export interface DiscoverSharedPublicSetupDeps {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface DiscoverSharedClientPluginStartDeps {}
export interface DiscoverSharedPublicStartDeps {}

export type DiscoverSharedClientPlugin = Plugin<
DiscoverSharedClientSetupExports,
DiscoverSharedClientStartExports,
DiscoverSharedClientPluginSetupDeps,
DiscoverSharedClientPluginStartDeps
export type DiscoverSharedPublicPlugin = Plugin<
DiscoverSharedPublicSetup,
DiscoverSharedPublicStart,
DiscoverSharedPublicSetupDeps,
DiscoverSharedPublicStartDeps
>;
2 changes: 1 addition & 1 deletion src/plugins/unified_doc_viewer/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"server": false,
"browser": true,
"requiredBundles": ["kibanaUtils"],
"requiredPlugins": ["data", "fieldFormats"],
"requiredPlugins": ["data", "discoverShared", "fieldFormats"],
}
}
2 changes: 2 additions & 0 deletions src/plugins/unified_doc_viewer/public/__mocks__/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { analyticsServiceMock } from '@kbn/core-analytics-browser-mocks';
import { dataPluginMock } from '@kbn/data-plugin/public/mocks';
import { discoverSharedPluginMock } from '@kbn/discover-shared-plugin/public/mocks';
import { fieldFormatsMock } from '@kbn/field-formats-plugin/common/mocks';
import { uiSettingsServiceMock } from '@kbn/core-ui-settings-browser-mocks';
import type { UnifiedDocViewerServices, UnifiedDocViewerStart } from '../types';
Expand All @@ -21,6 +22,7 @@ export const mockUnifiedDocViewer: jest.Mocked<UnifiedDocViewerStart> = {
export const mockUnifiedDocViewerServices: jest.Mocked<UnifiedDocViewerServices> = {
analytics: analyticsServiceMock.createAnalyticsServiceStart(),
data: dataPluginMock.createStartContract(),
discoverShared: discoverSharedPluginMock.createStartContract(),
fieldFormats: fieldFormatsMock,
storage: new Storage(localStorage),
uiSettings: uiSettingsServiceMock.createStartContract(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { LogsOverviewHeader } from './logs_overview_header';
import { LogsOverviewHighlights } from './logs_overview_highlights';
import { FieldActionsProvider } from '../../hooks/use_field_actions';
import { getUnifiedDocViewerServices } from '../../plugin';
import { LogsOverviewAIAssistant } from './logs_overview_ai_assistant';

export function LogsOverview({
columns,
Expand All @@ -36,6 +37,7 @@ export function LogsOverview({
<EuiSpacer size="m" />
<LogsOverviewHeader doc={parsedDoc} />
<LogsOverviewHighlights formattedDoc={parsedDoc} flattenedDoc={hit.flattened} />
<LogsOverviewAIAssistant doc={hit} />
</FieldActionsProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { DataTableRecord } from '@kbn/discover-utils';
import { getUnifiedDocViewerServices } from '../../plugin';

export function LogsOverviewAIAssistant({ doc }: { doc: DataTableRecord }) {
const { discoverShared } = getUnifiedDocViewerServices();

const logsAIAssistantFeature = discoverShared.features.registry.getById(
'observability-logs-ai-assistant'
);

if (!logsAIAssistantFeature) {
return null;
}

return logsAIAssistantFeature.render({ doc });
}
14 changes: 12 additions & 2 deletions src/plugins/unified_doc_viewer/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { DataPublicPluginStart } from '@kbn/data-plugin/public';
import { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import { CoreStart } from '@kbn/core/public';
import { dynamic } from '@kbn/shared-ux-utility';
import { DiscoverSharedPublicStart } from '@kbn/discover-shared-plugin/public';
import type { UnifiedDocViewerServices } from './types';

export const [getUnifiedDocViewerServices, setUnifiedDocViewerServices] =
Expand Down Expand Up @@ -47,6 +48,7 @@ export interface UnifiedDocViewerStart {

export interface UnifiedDocViewerStartDeps {
data: DataPublicPluginStart;
discoverShared: DiscoverSharedPublicStart;
fieldFormats: FieldFormatsStart;
}

Expand Down Expand Up @@ -111,12 +113,20 @@ export class UnifiedDocViewerPublicPlugin

public start(core: CoreStart, deps: UnifiedDocViewerStartDeps) {
const { analytics, uiSettings } = core;
const { data, fieldFormats } = deps;
const { data, discoverShared, fieldFormats } = deps;
const storage = new Storage(localStorage);
const unifiedDocViewer = {
registry: this.docViewsRegistry,
};
const services = { analytics, data, fieldFormats, storage, uiSettings, unifiedDocViewer };
const services = {
analytics,
data,
discoverShared,
fieldFormats,
storage,
uiSettings,
unifiedDocViewer,
};
setUnifiedDocViewerServices(services);
return unifiedDocViewer;
}
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/unified_doc_viewer/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type { UnifiedDocViewerSetup, UnifiedDocViewerStart } from './plugin';

import type { AnalyticsServiceStart } from '@kbn/core-analytics-browser';
import type { DataPublicPluginStart } from '@kbn/data-plugin/public';
import type { DiscoverSharedPublicStart } from '@kbn/discover-shared-plugin/public';
import type { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import type { Storage } from '@kbn/kibana-utils-plugin/public';
import type { IUiSettingsClient } from '@kbn/core-ui-settings-browser';
Expand All @@ -20,6 +21,7 @@ import type { UnifiedDocViewerStart } from './plugin';
export interface UnifiedDocViewerServices {
analytics: AnalyticsServiceStart;
data: DataPublicPluginStart;
discoverShared: DiscoverSharedPublicStart;
fieldFormats: FieldFormatsStart;
storage: Storage;
uiSettings: IUiSettingsClient;
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,8 @@
"@kbn/discover-enhanced-plugin/*": ["x-pack/plugins/discover_enhanced/*"],
"@kbn/discover-plugin": ["src/plugins/discover"],
"@kbn/discover-plugin/*": ["src/plugins/discover/*"],
"@kbn/discover-shared-plugin": ["src/plugins/discover_shared"],
"@kbn/discover-shared-plugin/*": ["src/plugins/discover_shared/*"],
"@kbn/discover-utils": ["packages/kbn-discover-utils"],
"@kbn/discover-utils/*": ["packages/kbn-discover-utils/*"],
"@kbn/doc-links": ["packages/kbn-doc-links"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"requiredPlugins": [
"data",
"dataViews",
"discoverShared",
"usageCollection",
"observabilityShared",
"share"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React from 'react';
import React, { useMemo } from 'react';
import { dynamic } from '@kbn/shared-ux-utility';
import { LogAIAssistantProps } from './log_ai_assistant';
import { ObservabilityLogsAIAssistantFeatureRenderDeps } from '@kbn/discover-shared-plugin/public';
import { LogAIAssistantDocument, LogAIAssistantProps } from './log_ai_assistant';

export const LogAIAssistant = dynamic(() => import('./log_ai_assistant'));

Expand All @@ -17,3 +18,21 @@ export function createLogAIAssistant({
<LogAIAssistant observabilityAIAssistant={observabilityAIAssistant} {...props} />
);
}

export const createLogsAIAssistantRenderer =
(LogAIAssistantRender: ReturnType<typeof createLogAIAssistant>) =>
({ doc }: ObservabilityLogsAIAssistantFeatureRenderDeps) => {
if (!doc) return;

const mappedDoc = useMemo(
() => ({
fields: Object.entries(doc.flattened).map(([field, value]) => ({
field,
value,
})) as LogAIAssistantDocument['fields'],
}),
[doc]
);

return <LogAIAssistantRender key={doc.id} doc={mappedDoc} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
NodeLogsLocatorDefinition,
TraceLogsLocatorDefinition,
} from '../common/locators';
import { createLogAIAssistant } from './components/log_ai_assistant';
import { createLogAIAssistant, createLogsAIAssistantRenderer } from './components/log_ai_assistant';
import { LogViewsService } from './services/log_views';
import {
LogsSharedClientCoreSetup,
Expand Down Expand Up @@ -52,7 +52,7 @@ export class LogsSharedPlugin implements LogsSharedClientPluginClass {

public start(core: CoreStart, plugins: LogsSharedClientStartDeps) {
const { http } = core;
const { data, dataViews, observabilityAIAssistant } = plugins;
const { data, dataViews, discoverShared, observabilityAIAssistant } = plugins;

const logViews = this.logViews.start({
http,
Expand All @@ -68,6 +68,11 @@ export class LogsSharedPlugin implements LogsSharedClientPluginClass {

const LogAIAssistant = createLogAIAssistant({ observabilityAIAssistant });

discoverShared.features.registry.register({
id: 'observability-logs-ai-assistant',
render: createLogsAIAssistantRenderer(LogAIAssistant),
});

return {
logViews,
LogAIAssistant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type { CoreSetup, CoreStart, Plugin as PluginClass } from '@kbn/core/public';
import type { DataPublicPluginStart } from '@kbn/data-plugin/public';
import type { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public';
import type { DiscoverSharedPublicStart } from '@kbn/discover-shared-plugin/public';
import type { ObservabilityAIAssistantPublicStart } from '@kbn/observability-ai-assistant-plugin/public';
import { SharePluginSetup } from '@kbn/share-plugin/public';
import { UiActionsStart } from '@kbn/ui-actions-plugin/public';
Expand Down Expand Up @@ -35,6 +36,7 @@ export interface LogsSharedClientSetupDeps {
export interface LogsSharedClientStartDeps {
data: DataPublicPluginStart;
dataViews: DataViewsPublicPluginStart;
discoverShared: DiscoverSharedPublicStart;
observabilityAIAssistant?: ObservabilityAIAssistantPublicStart;
uiActions: UiActionsStart;
}
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4488,6 +4488,10 @@
version "0.0.0"
uid ""

"@kbn/discover-shared-plugin@link:src/plugins/discover_shared":
version "0.0.0"
uid ""

"@kbn/discover-utils@link:packages/kbn-discover-utils":
version "0.0.0"
uid ""
Expand Down

0 comments on commit bfc2d75

Please sign in to comment.