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

feat: add visualization card in customized parser #16

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions server/parsers/visualization_card_parser.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good place to add some unit tests for the parser :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, added cases accordingly.

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);
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add the TODO comment

// TODO use a more robust CSV parsing library
const extractNthColumn = (csv: string, column: number) => {

theoretically id can contain any character, i haven't tested whether " in ids would be encoded or left as is, but current logic is not enough either way

also should output_builders/saved_objects.ts be removed? and could there be some tests similar to

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am using csv-parser package, which is a popular and robust package to handle with csv format.

And sure, I migrate the test cases in save_objects.test.ts and add some edge cases.


export const VisualizationCardParser = {
id: 'core_visualization',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should there be an order explicitly? and i remember parsers should use the MessageParser interface?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If order is not specified, default order value will be 999.

async parserProvider(interaction: Interaction) {
const additionalInfo = interaction.additional_info as {
'VisualizationTool.output': string[];
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type doesn't seem right, on line 21, additionalInfo is nullable, but additional_info in Interaction is Record<string, unknown>. Is additional_info actually optional field?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think assertion should happen after field check

const visualizationOutputs = additionalInfo?.['VisualizationTool.output'];
SuZhou-Joe marked this conversation as resolved.
Show resolved Hide resolved
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
Loading