Skip to content
Merged
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 @@ -24,6 +24,7 @@ import { AIChatExperience } from '@kbn/ai-assistant-common';
import { AI_CHAT_EXPERIENCE_TYPE } from '@kbn/management-settings-ids';
import { useKibana } from '../../hooks/use_kibana';
import { useLicense } from '../../hooks/use_license';
import { useGenAIConnectors } from '../../hooks/use_genai_connectors';
import { StartConversationButton } from './start_conversation_button';
import { AiInsightErrorBanner } from './ai_insight_error_banner';

Expand Down Expand Up @@ -62,6 +63,8 @@ export function AiInsight({ title, fetchInsight, buildAttachments }: AiInsightPr
const [chatExperience] = useUiSetting$<AIChatExperience>(AI_CHAT_EXPERIENCE_TYPE);
const isAgentChatExperienceEnabled = chatExperience === AIChatExperience.Agent;

const { hasConnectors } = useGenAIConnectors();

const hasEnterpriseLicense = license?.hasAtLeast('enterprise');
const hasAgentBuilderAccess = application?.capabilities.agentBuilder?.show === true;

Expand Down Expand Up @@ -90,6 +93,7 @@ export function AiInsight({ title, fetchInsight, buildAttachments }: AiInsightPr

if (
!onechat ||
!hasConnectors ||
!isAgentChatExperienceEnabled ||
!hasAgentBuilderAccess ||
!hasEnterpriseLicense
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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 { renderHook, waitFor } from '@testing-library/react';
import type { InferenceConnector } from '@kbn/inference-common';
import { InferenceConnectorType } from '@kbn/inference-common';
import { useGenAIConnectors } from './use_genai_connectors';
import { useKibana } from './use_kibana';

jest.mock('./use_kibana');

const mockUseKibana = useKibana as jest.Mock;
const mockGetConnectors = jest.fn();

const mockConnectors: InferenceConnector[] = [
{
connectorId: 'connector-1',
name: 'OpenAI Connector',
type: InferenceConnectorType.OpenAI,
config: {},
capabilities: {},
},
{
connectorId: 'connector-2',
name: 'Bedrock Connector',
type: InferenceConnectorType.Bedrock,
config: {},
capabilities: {},
},
];

describe('useGenAIConnectors', () => {
beforeEach(() => {
mockGetConnectors.mockReset();
mockUseKibana.mockReturnValue({
services: {
inference: {
getConnectors: mockGetConnectors,
},
},
});
});

afterEach(() => {
jest.clearAllMocks();
});

it('should fetch connectors and set hasConnectors to true when connectors exist', async () => {
mockGetConnectors.mockResolvedValue(mockConnectors);
const { result, unmount } = renderHook(() => useGenAIConnectors());

await waitFor(() => {
expect(result.current.loading).toBe(false);
});

expect(result.current.connectors).toEqual(mockConnectors);
expect(result.current.hasConnectors).toBe(true);

unmount();
});

it('should set hasConnectors to false when no connectors exist', async () => {
mockGetConnectors.mockResolvedValue([]);
const { result, unmount } = renderHook(() => useGenAIConnectors());

await waitFor(() => {
expect(result.current.loading).toBe(false);
});

expect(result.current.connectors).toEqual([]);
expect(result.current.hasConnectors).toBe(false);

unmount();
});

it('should handle API errors', async () => {
mockGetConnectors.mockRejectedValue(new Error('API failed'));
const { result, unmount } = renderHook(() => useGenAIConnectors());

await waitFor(() => {
expect(result.current.loading).toBe(false);
});

expect(result.current.connectors).toEqual([]);
expect(result.current.hasConnectors).toBe(false);

unmount();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 useAsync from 'react-use/lib/useAsync';
import type { InferenceConnector } from '@kbn/inference-common';
import { useKibana } from './use_kibana';

export interface UseGenAIConnectorsResult {
connectors: InferenceConnector[];
hasConnectors: boolean;
loading: boolean;
}

/**
* Hook to fetch available GenAI connectors.
*/
export function useGenAIConnectors(): UseGenAIConnectorsResult {
const {
services: { inference },
} = useKibana();

const { value: connectors = [], loading } = useAsync(async () => {
if (!inference) {
return [];
}
return inference.getConnectors();
}, [inference]);

return {
connectors,
hasConnectors: connectors.length > 0,
loading,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import type { ComponentType } from 'react';
import type { OnechatPluginStart } from '@kbn/onechat-plugin/public';
import type { DiscoverSharedPublicStart } from '@kbn/discover-shared-plugin/public';
import type { InferencePublicStart } from '@kbn/inference-plugin/public';
import type { LicensingPluginStart } from '@kbn/licensing-plugin/public';
import type { AlertAiInsightProps, ErrorSampleAiInsightProps } from './components/insights';

Expand All @@ -24,5 +25,6 @@ export interface ObservabilityAgentBuilderPluginSetupDependencies {}
export interface ObservabilityAgentBuilderPluginStartDependencies {
discoverShared: DiscoverSharedPublicStart;
onechat: OnechatPluginStart;
inference: InferencePublicStart;
licensing: LicensingPluginStart;
}