-
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.
feat: add mechannism to register messageParser
Signed-off-by: SuZhou-Joe <[email protected]>
- Loading branch information
1 parent
88eb43e
commit 23e550f
Showing
9 changed files
with
161 additions
and
7 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
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,37 @@ | ||
/* | ||
* 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', | ||
}, | ||
]); | ||
}); | ||
}); |
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,19 @@ | ||
/* | ||
* 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(); | ||
for (const messageParser of this.messageParsers) { | ||
await messageParser.parserProvider(interaction, messageParserHelper); | ||
} | ||
return messageParserHelper.messages; | ||
} | ||
} |