-
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
Merged
SuZhou-Joe
merged 8 commits into
feature/agent-framework
from
feature/add-builder-registration
Nov 22, 2023
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a25b176
feat: add mechannism to register messageParser
SuZhou-Joe 17174c9
feat: sort when run
SuZhou-Joe 8fd7c05
feat: sort when run
SuZhou-Joe aac7903
feat: add doc change
SuZhou-Joe 02523c3
feat: add diagram
SuZhou-Joe 8b48a7c
feat: optimize code
SuZhou-Joe 8331a95
feat: add error handling
SuZhou-Joe d598bff
feat: optimize
SuZhou-Joe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# `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 | ||
|
||
``` | ||
SuZhou-Joe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dashboardAssistant.registerMessageParser({ | ||
id: "foo_parser", | ||
parserProvider: async (interaction, messageParserHelper) => { | ||
if (interaction.additional_info?.visualizationId) { | ||
messageParserHelper.addMessage({ | ||
contentType: "visualization", | ||
content: interaction.additional_info.visualizationId | ||
SuZhou-Joe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
} | ||
} | ||
}) | ||
``` |
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
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,24 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { MessageParserHelper } from './message_parser_helper'; | ||
|
||
describe('MessageParserHelper', () => { | ||
it('return with correct message', async () => { | ||
const messageParserHelper = new MessageParserHelper(); | ||
messageParserHelper.addMessage({ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'output', | ||
}); | ||
expect(messageParserHelper.messages).toEqual([ | ||
{ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'output', | ||
}, | ||
]); | ||
}); | ||
}); |
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,15 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { IMessage } from '../../common/types/chat_saved_object_attributes'; | ||
import { IMessageParserHelper } from '../types'; | ||
|
||
export class MessageParserHelper implements IMessageParserHelper { | ||
public messages: IMessage[] = []; | ||
addMessage(message: IMessage) { | ||
this.messages.push(message); | ||
return true; | ||
} | ||
} |
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,117 @@ | ||
/* | ||
* 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, messageParserHelper) { | ||
messageParserHelper.addMessage({ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: interaction.response, | ||
}); | ||
return Promise.resolve(''); | ||
}, | ||
}, | ||
]); | ||
|
||
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(interaction, messageParserHelper) { | ||
messageParserHelper.addMessage({ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'A', | ||
}); | ||
return Promise.resolve(''); | ||
}, | ||
}, | ||
{ | ||
id: 'testOrder1000', | ||
order: 1000, | ||
parserProvider(interaction, messageParserHelper) { | ||
messageParserHelper.addMessage({ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'order1000', | ||
}); | ||
return Promise.resolve(''); | ||
}, | ||
}, | ||
{ | ||
id: 'testNoOrder', | ||
parserProvider(interaction, messageParserHelper) { | ||
messageParserHelper.addMessage({ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'NoOrder', | ||
}); | ||
return Promise.resolve(''); | ||
}, | ||
}, | ||
{ | ||
id: 'testB', | ||
order: 1, | ||
parserProvider(interaction, messageParserHelper) { | ||
messageParserHelper.addMessage({ | ||
type: 'output', | ||
contentType: 'markdown', | ||
content: 'B', | ||
}); | ||
return Promise.resolve(''); | ||
}, | ||
}, | ||
]); | ||
|
||
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', | ||
}, | ||
]); | ||
}); | ||
}); |
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,25 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { IMessage } from '../../common/types/chat_saved_object_attributes'; | ||
import { Interaction, MessageParser } from '../types'; | ||
import { MessageParserHelper } from './message_parser_helper'; | ||
|
||
export class MessageParserRunner { | ||
constructor(private readonly messageParsers: MessageParser[]) {} | ||
async run(interaction: Interaction): Promise<IMessage[]> { | ||
const messageParserHelper = new MessageParserHelper(); | ||
const sortedParsers = [...this.messageParsers]; | ||
sortedParsers.sort((parserA, parserB) => { | ||
const { order: orderA = 999 } = parserA; | ||
const { order: orderB = 999 } = parserB; | ||
return orderA - orderB; | ||
}); | ||
for (const messageParser of sortedParsers) { | ||
await messageParser.parserProvider(interaction, messageParserHelper); | ||
} | ||
return messageParserHelper.messages; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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