-
Notifications
You must be signed in to change notification settings - Fork 23
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 mechanism to register messageParser #5
Changes from all commits
a25b176
17174c9
8fd7c05
aac7903
02523c3
8b48a7c
8331a95
d598bff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# CHANGELOG | ||
|
||
Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) | ||
|
||
### 📈 Features/Enhancements | ||
|
||
- Add support for registerMessageParser ([#5](https://github.com/opensearch-project/dashboards-assistant/pull/5)) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# `registerMessageParser` — Register your customized parser logic into Chatbot. | ||
|
||
**Interaction** refers to a question-answer pair in Chatbot application. In most cases, an interaction consists of two messages: an `Input` message and an `Output` message. However, as the Chatbot evolves to become more powerful, it may display new messages such as visualizations, data explorers, or data grids. Therefore, it is crucial to implement a mechanism that allows other plugins to register their customized parser logic based on each interaction body. | ||
|
||
![message parser](https://github.com/opensearch-project/dashboards-assistant/assets/13493605/b4ec1ff8-5339-4119-ad20-b2c31057bb0b) | ||
|
||
## API | ||
|
||
### registerMessageParser | ||
|
||
```typescript | ||
dashboardAssistant.registerMessageParser({ | ||
id: 'foo_parser', | ||
parserProvider: async (interaction) => { | ||
if (interaction.input) { | ||
return [ | ||
{ | ||
type: 'input', | ||
contentType: 'text', | ||
content: interaction.input, | ||
}, | ||
]; | ||
} | ||
return []; | ||
}, | ||
}); | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { MessageParserRunner } from './message_parser_runner'; | ||
|
||
describe('MessageParserRunner', () => { | ||
it('run with correct result', async () => { | ||
const messageParserRunner = new MessageParserRunner([ | ||
{ | ||
id: 'test', | ||
parserProvider(interaction) { | ||
return Promise.resolve([ | ||
{ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: interaction.response, | ||
}, | ||
]); | ||
}, | ||
}, | ||
]); | ||
|
||
expect( | ||
await messageParserRunner.run({ | ||
response: 'output', | ||
input: 'input', | ||
}) | ||
).toEqual([ | ||
{ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'output', | ||
}, | ||
]); | ||
}); | ||
|
||
it('run with correct result when different order is present', async () => { | ||
const messageParserRunner = new MessageParserRunner([ | ||
{ | ||
id: 'testA', | ||
order: 2, | ||
parserProvider() { | ||
return Promise.resolve([ | ||
{ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'A', | ||
}, | ||
]); | ||
}, | ||
}, | ||
{ | ||
id: 'testOrder1000', | ||
order: 1000, | ||
parserProvider() { | ||
return Promise.resolve([ | ||
{ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'order1000', | ||
}, | ||
]); | ||
}, | ||
}, | ||
{ | ||
id: 'testNoOrder', | ||
parserProvider(interaction) { | ||
return Promise.resolve([ | ||
{ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'NoOrder', | ||
}, | ||
]); | ||
}, | ||
}, | ||
{ | ||
id: 'testB', | ||
order: 1, | ||
parserProvider() { | ||
return Promise.resolve([ | ||
{ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'B', | ||
}, | ||
]); | ||
}, | ||
}, | ||
]); | ||
|
||
expect( | ||
await messageParserRunner.run({ | ||
response: 'output', | ||
input: 'input', | ||
}) | ||
).toEqual([ | ||
{ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'B', | ||
}, | ||
{ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'A', | ||
}, | ||
{ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'NoOrder', | ||
}, | ||
{ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'order1000', | ||
}, | ||
]); | ||
}); | ||
|
||
it('Do not append messages that are throwed with error or not an array', async () => { | ||
const messageParserRunner = new MessageParserRunner([ | ||
{ | ||
id: 'test_with_error', | ||
parserProvider() { | ||
throw new Error('error'); | ||
}, | ||
}, | ||
{ | ||
id: 'test_with_incorrect_format_of_return', | ||
parserProvider() { | ||
return Promise.resolve({ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'order1000', | ||
}); | ||
}, | ||
}, | ||
]); | ||
|
||
expect( | ||
await messageParserRunner.run({ | ||
response: 'output', | ||
input: 'input', | ||
}) | ||
).toEqual([]); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { IMessage } from '../../common/types/chat_saved_object_attributes'; | ||
import { Interaction, MessageParser } from '../types'; | ||
|
||
export class MessageParserRunner { | ||
constructor(private readonly messageParsers: MessageParser[]) {} | ||
async run(interaction: Interaction): Promise<IMessage[]> { | ||
const sortedParsers = [...this.messageParsers]; | ||
sortedParsers.sort((parserA, parserB) => { | ||
const { order: orderA = 999 } = parserA; | ||
const { order: orderB = 999 } = parserB; | ||
return orderA - orderB; | ||
}); | ||
let results: IMessage[] = []; | ||
for (const messageParser of sortedParsers) { | ||
let tempResult: IMessage[] = []; | ||
try { | ||
tempResult = await messageParser.parserProvider(interaction); | ||
/** | ||
* Make sure the tempResult is an array. | ||
*/ | ||
if (!Array.isArray(tempResult)) { | ||
tempResult = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So here we just ignore the parse results if it's not an array? If we consider this is invalid data, shall we throw error here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we only accept those response with correct format. I just do not want to do some error handling like wrapping a single message into an array. We should comply with the type restriction. |
||
} | ||
} catch (e) { | ||
tempResult = []; | ||
} | ||
results = [...results, ...tempResult]; | ||
} | ||
return results; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need to manually maintain changelog? can it be generated automatically with
https://github.com/tj/git-extras/blob/HEAD/Commands.md#git-changelog
or
https://github.com/orhun/git-cliff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it was a requirement set by the org. But I think this project is private so there wouldn't be an initial source to compare it to so I'd probably skip this changelog