Skip to content

Commit

Permalink
Only display ai actions that compatible with the datasource (#350)
Browse files Browse the repository at this point in the history
* feat: do not display AI actions if the selected data source is incompatible

Signed-off-by: Yulong Ruan <ruanyl@amazon.com>

* refactor: add data source id to action context

Signed-off-by: Yulong Ruan <ruanyl@amazon.com>

* chore: update CHANGELOG

Signed-off-by: Yulong Ruan <ruanyl@amazon.com>

---------

Signed-off-by: Yulong Ruan <ruanyl@amazon.com>
  • Loading branch information
ruanyl authored Oct 17, 2024
1 parent f7fde7d commit 19ca923
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- fix: Remove the cache of insight agent id in node server([#343](https://github.com/opensearch-project/dashboards-assistant/pull/343))
- fix: Fix error time field and add filter for same name index patterns([#345](https://github.com/opensearch-project/dashboards-assistant/pull/345))
- refactor: Add data source info in discover url when navigating([#347](https://github.com/opensearch-project/dashboards-assistant/pull/347))
- feat: only display ai actions that compatible with the datasource([#350](https://github.com/opensearch-project/dashboards-assistant/pull/350))


### 📈 Features/Enhancements
Expand Down
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

0 comments on commit 19ca923

Please sign in to comment.