Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Only display ai actions that compatible with the datasource #352

Merged
merged 1 commit into from
Oct 17, 2024
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
38 changes: 35 additions & 3 deletions public/components/ui_action_context_menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,73 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useState, useRef } from 'react';
import React, { useState, useRef, useEffect } from 'react';
import useAsync from 'react-use/lib/useAsync';
import { EuiButtonEmpty, EuiContextMenu, EuiPopover } from '@elastic/eui';
import { i18n } from '@osd/i18n';

import { buildContextMenuForActions } from '../../../../src/plugins/ui_actions/public';
import { AI_ASSISTANT_QUERY_EDITOR_TRIGGER } from '../ui_triggers';
import { getUiActions } from '../services';
import { DataPublicPluginSetup } from '../../../../src/plugins/data/public';

interface Props {
data: DataPublicPluginSetup;
label?: string;
}

export const ActionContextMenu = (props: Props) => {
const uiActions = getUiActions();
const actionsRef = useRef(uiActions.getTriggerActions(AI_ASSISTANT_QUERY_EDITOR_TRIGGER));
const [open, setOpen] = useState(false);
const [actionContext, setActionContext] = useState({
datasetId: props.data.query.queryString.getQuery().dataset?.id ?? '',
datasetType: props.data.query.queryString.getQuery().dataset?.type ?? '',
dataSourceId: props.data.query.queryString.getQuery().dataset?.dataSource?.id,
});

useEffect(() => {
const subscription = props.data.query.queryString.getUpdates$().subscribe((query) => {
if (query.dataset) {
setActionContext((state) => ({
...state,
datasetId: query.dataset?.id ?? '',
datasetType: query.dataset?.type ?? '',
dataSourceId: query.dataset?.dataSource?.id,
}));
}
});
return () => {
subscription.unsubscribe();
};
}, [props.data.query.queryString]);

const panels = useAsync(
() =>
buildContextMenuForActions({
actions: actionsRef.current.map((action) => ({
action,
context: {},
context: {
datasetId: actionContext.datasetId,
datasetType: actionContext.datasetType,
dataSourceId: actionContext.dataSourceId,
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
trigger: AI_ASSISTANT_QUERY_EDITOR_TRIGGER as any,
})),
closeMenu: () => setOpen(false),
title: '',
}),
[]
[actionContext.datasetId, actionContext.datasetType, actionContext.dataSourceId]
);

if (actionsRef.current.length === 0) {
return null;
}

// If context menu has no item, the action button should be disabled
const actionDisabled = (panels.value?.[0]?.items ?? []).length === 0;

return (
<EuiPopover
button={
Expand All @@ -51,6 +81,8 @@ export const ActionContextMenu = (props: Props) => {
onClick={() => setOpen(!open)}
iconSide="right"
flush="both"
isDisabled={actionDisabled}
isLoading={panels.loading}
>
{props.label ||
i18n.translate('dashboardAssistant.branding.assistantActionButton.label', {
Expand Down
28 changes: 26 additions & 2 deletions public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ import {
} from './services';
import { ConfigSchema } from '../common/types/config';
import { DataSourceService } from './services/data_source_service';
import { ASSISTANT_API, DEFAULT_USER_NAME } from '../common/constants/llm';
import {
ASSISTANT_API,
DEFAULT_USER_NAME,
TEXT2PPL_AGENT_CONFIG_ID,
TEXT2VEGA_RULE_BASED_AGENT_CONFIG_ID,
TEXT2VEGA_WITH_INSTRUCTIONS_AGENT_CONFIG_ID,
} from '../common/constants/llm';
import { IncontextInsightProps } from './components/incontext_insight';
import { AssistantService } from './services/assistant_service';
import { ActionContextMenu } from './components/ui_action_context_menu';
Expand Down Expand Up @@ -260,7 +266,7 @@ export class AssistantPlugin
order: 2000,
isEnabled$: () => of(true),
getSearchBarButton: () => {
return <ActionContextMenu label={this.config.branding.label} />;
return <ActionContextMenu label={this.config.branding.label} data={setupDeps.data} />;
},
},
},
Expand Down Expand Up @@ -328,6 +334,24 @@ export class AssistantPlugin
order: 1,
getDisplayName: () => 'Generate visualization',
getIconType: () => 'visLine' as const,
// T2Viz is only compatible with data sources that have certain agents configured
isCompatible: async (context) => {
// t2viz only supports selecting index pattern at the moment
if (context.datasetType === 'INDEX_PATTERN' && context.datasetId) {
const res = await assistantServiceStart.client.agentConfigExists(
[
TEXT2VEGA_RULE_BASED_AGENT_CONFIG_ID,
TEXT2VEGA_WITH_INSTRUCTIONS_AGENT_CONFIG_ID,
TEXT2PPL_AGENT_CONFIG_ID,
],
{
dataSourceId: context.dataSourceId,
}
);
return res.exists;
}
return false;
},
execute: async () => {
core.application.navigateToApp(TEXT2VIZ_APP_ID);
},
Expand Down
6 changes: 5 additions & 1 deletion public/ui_triggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ export const AI_ASSISTANT_QUERY_EDITOR_TRIGGER = 'AI_ASSISTANT_QUERY_EDITOR_TRIG

declare module '../../../src/plugins/ui_actions/public' {
export interface TriggerContextMapping {
[AI_ASSISTANT_QUERY_EDITOR_TRIGGER]: {};
[AI_ASSISTANT_QUERY_EDITOR_TRIGGER]: {
datasetId: string;
datasetType: string;
dataSourceId?: string;
};
}
}

Expand Down
Loading