Skip to content

Commit

Permalink
allowing multiple intro messages
Browse files Browse the repository at this point in the history
  • Loading branch information
OvidijusParsiunas committed Sep 13, 2024
1 parent 007a6c7 commit f6d4653
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion component/src/deepChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class DeepChat extends InternalHTML {
history?: MessageContent[];

@Property('object')
introMessage?: IntroMessage;
introMessage?: IntroMessage | IntroMessage[];

@Property('object')
avatars?: Avatars;
Expand Down
36 changes: 23 additions & 13 deletions component/src/views/chat/messages/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Messages extends MessagesBase {
private readonly _displayLoadingMessage?: boolean;
private readonly _permittedErrorPrefixes?: CustomErrors;
private readonly _displayServiceErrorMessages?: boolean;
private _introMessage?: IntroMessage;
private _introMessage?: IntroMessage | IntroMessage[];
customDemoResponse?: DemoResponse;

constructor(deepChat: DeepChat, serviceIO: ServiceIO, panel?: HTMLElement) {
Expand All @@ -55,7 +55,7 @@ export class Messages extends MessagesBase {
if (!this.addSetupMessageIfNeeded(deepChat, serviceIO)) {
this.populateIntroPanel(panel, introPanelMarkUp, deepChat.introPanelStyle);
}
this.addIntroductoryMessage(deepChat, serviceIO);
this.addIntroductoryMessages(deepChat, serviceIO);
new History(deepChat, this, serviceIO);
this._displayServiceErrorMessages = deepChat.errorMessages?.displayServiceErrorMessages;
deepChat.getMessages = () => JSON.parse(JSON.stringify(this.messages));
Expand Down Expand Up @@ -105,25 +105,35 @@ export class Messages extends MessagesBase {
}

// WORK - const file for deep chat classes
private addIntroductoryMessage(deepChat?: DeepChat, serviceIO?: ServiceIO) {
private addIntroductoryMessages(deepChat?: DeepChat, serviceIO?: ServiceIO) {
if (deepChat?.shadowRoot) this._introMessage = deepChat.introMessage;
let introMessage = this._introMessage;
// interface - introMessage
if (serviceIO?.isWebModel()) introMessage ??= (serviceIO as WebModel).getIntroMessage(introMessage);
if (introMessage) {
let elements;
if (introMessage?.text) {
elements = this.createAndAppendNewMessageElement(introMessage.text, MessageUtils.AI_ROLE);
} else if (introMessage?.html) {
elements = HTMLMessages.add(this, introMessage.html, MessageUtils.AI_ROLE, this.messageElementRefs);
}
if (elements) {
this.applyCustomStyles(elements, MessageUtils.AI_ROLE, false, this.messageStyles?.intro);
elements.outerContainer.classList.add('deep-chat-intro');
if (Array.isArray(introMessage)) {
introMessage.forEach((intro) => {
this.addIntroductoryMessage(intro);
});
} else {
this.addIntroductoryMessage(introMessage);
}
}
}

private addIntroductoryMessage(introMessage: IntroMessage) {
let elements;
if (introMessage?.text) {
elements = this.createAndAppendNewMessageElement(introMessage.text, MessageUtils.AI_ROLE);
} else if (introMessage?.html) {
elements = HTMLMessages.add(this, introMessage.html, MessageUtils.AI_ROLE, this.messageElementRefs);
}
if (elements) {
this.applyCustomStyles(elements, MessageUtils.AI_ROLE, false, this.messageStyles?.intro);
elements.outerContainer.classList.add('deep-chat-intro');
}
}

public removeIntroductoryMessage() {
const introMessage = this.messageElementRefs[0];
if (introMessage.outerContainer.classList.contains('deep-chat-intro')) {
Expand Down Expand Up @@ -321,7 +331,7 @@ export class Messages extends MessagesBase {
this.messageElementRefs = retainedElements;
if (isReset !== false) {
if (this._introPanel?._elementRef) this._introPanel.display();
this.addIntroductoryMessage();
this.addIntroductoryMessages();
}
this.messages.splice(0, this.messages.length);
this.textElementsToText.splice(0, this.textElementsToText.length);
Expand Down

0 comments on commit f6d4653

Please sign in to comment.