Skip to content

Commit

Permalink
feat: add implement visualization in customized parser
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Nov 27, 2023
1 parent 15ea29b commit a74ff7f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
41 changes: 41 additions & 0 deletions server/parsers/visualization_card_parser.ts
Original file line number Diff line number Diff line change
@@ -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(<T>(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;
},
};
3 changes: 2 additions & 1 deletion server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssistantPluginSetup, AssistantPluginStart> {
private readonly logger: Logger;
Expand Down Expand Up @@ -79,6 +79,7 @@ export class AssistantPlugin implements Plugin<AssistantPluginSetup, AssistantPl
};

registerMessageParser(BasicInputOutputParser);
registerMessageParser(VisualizationCardParser);

return {
registerMessageParser,
Expand Down

0 comments on commit a74ff7f

Please sign in to comment.