diff --git a/server/parsers/visualization_card_parser.ts b/server/parsers/visualization_card_parser.ts new file mode 100644 index 00000000..f833dfa8 --- /dev/null +++ b/server/parsers/visualization_card_parser.ts @@ -0,0 +1,41 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { IMessage, Interaction } from '../../common/types/chat_saved_object_attributes'; + +const extractNthColumn = (csv: string, column: number) => { + const lines = csv.split(/\r?\n/).slice(1); + return lines + .map((line) => line.split(',').at(column)) + .filter((v: T | null | undefined): v is T => v !== null && v !== undefined); +}; + +export const VisualizationCardParser = { + id: 'core_visualization', + async parserProvider(interaction: Interaction) { + const additionalInfo = interaction.additional_info as { + 'VisualizationTool.output': string[]; + }; + const visualizationOutputs = additionalInfo?.['VisualizationTool.output']; + if (!visualizationOutputs) { + return []; + } + const visualizationIds = visualizationOutputs.flatMap((output) => extractNthColumn(output, 1)); // second column is id field + + const visOutputs: IMessage[] = visualizationIds.map((id) => ({ + type: 'output', + content: id, + contentType: 'visualization', + suggestedActions: [ + { + message: 'View in Visualize', + actionType: 'view_in_dashboards', + }, + ], + })); + + return visOutputs; + }, +}; diff --git a/server/plugin.ts b/server/plugin.ts index 745e3b0c..521d855c 100644 --- a/server/plugin.ts +++ b/server/plugin.ts @@ -17,13 +17,13 @@ import { import { OpenSearchAlertingPlugin } from './adaptors/opensearch_alerting_plugin'; import { OpenSearchObservabilityPlugin } from './adaptors/opensearch_observability_plugin'; import { PPLPlugin } from './adaptors/ppl_plugin'; -import { AssistantServerConfig } from './config/schema'; import './fetch-polyfill'; import { setupRoutes } from './routes/index'; import { chatSavedObject } from './saved_objects/chat_saved_object'; import { AssistantPluginSetup, AssistantPluginStart, MessageParser } from './types'; import { chatConfigSavedObject } from './saved_objects/chat_config_saved_object'; import { BasicInputOutputParser } from './parsers/basic_input_output_parser'; +import { VisualizationCardParser } from './parsers/visualization_card_parser'; export class AssistantPlugin implements Plugin { private readonly logger: Logger; @@ -79,6 +79,7 @@ export class AssistantPlugin implements Plugin