-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from opensearch-project/feature/add-builder-reg…
…istration feat: add mechanism to register messageParser
- Loading branch information
Showing
9 changed files
with
281 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
}, | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; | ||
} | ||
} catch (e) { | ||
tempResult = []; | ||
} | ||
results = [...results, ...tempResult]; | ||
} | ||
return results; | ||
} | ||
} |