Add example JavaScript ChatCommand usage
Describe the issue
The chat command inputStream field is a very unique API. There should be a full example of its usage. Making an AsyncIterator in JavaScript can include a lot of boilerplate code.
Links
https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/qbusiness/command/ChatCommand/
Hi @eviljoe - thanks for checking in.
Could you elaborate on the detail of your request? The link of the docs you provided does have example code. You may also refer to API reference docs from service: https://docs.aws.amazon.com/amazonq/latest/api-reference/API_Chat.html
It has an example, but when I tried to use an object for the inputStream, TypeScript said that it did not match up with the type for an AsyncIterator<ChatInputSteam> | undefined. All of these code snippets are invalid in TypeScript. The solution that I came up with to create an AsyncIterator was not ideal. I am not sure how the developer of the API envisioned someone setting the inputStream
Invalid Example 1
static async chat(
credentials: Credentials,
userMessage: string,
parentMessageId?: string,
conversationId?: string,
): Promise<ChatSyncCommandOutput> {
const res = await this._newClient(credentials).send(new ChatCommand({
applicationId: EnvUtils.get(EnvVar.QBusinessAppId),
parentMessageId,
conversationId,
inputStream: [
{
configurationEvent: {
chatMode: ChatMode.RETRIEVAL_MODE,
},
},
{
textEvent: {
userMessage,
},
},
{
endOfInputEvent: {},
},
],
}));
}
Invalid Example 2
static async chat(
credentials: Credentials,
userMessage: string,
parentMessageId?: string,
conversationId?: string,
): Promise<ChatSyncCommandOutput> {
const res = await this._newClient(credentials).send(new ChatCommand({
applicationId: EnvUtils.get(EnvVar.QBusinessAppId),
parentMessageId,
conversationId,
inputStream: {
{
configurationEvent: {
chatMode: ChatMode.RETRIEVAL_MODE,
},
},
{
textEvent: {
userMessage,
},
},
{
endOfInputEvent: {},
},
},
}));
}
Working Example
class ChatUtils {
static async chat(
credentials: Credentials,
userMessage: string,
parentMessageId?: string,
conversationId?: string,
): Promise<ChatSyncCommandOutput> {
const res = await this._newClient(credentials).send(new ChatCommand({
applicationId: EnvUtils.get(EnvVar.QBusinessAppId),
parentMessageId,
conversationId,
inputStream: toAsyncIt([
{
configurationEvent: {
chatMode: ChatMode.RETRIEVAL_MODE,
},
},
{
textEvent: {
userMessage,
},
},
{
endOfInputEvent: {},
},
]),
}));
}
static async* toAsyncIt(array: ChatInputStream[]): AsyncGenerator<ChatInputStream> {
for(const elem of array) {
yield elem;
}
}
}