Skip to content

Commit 1f1fe4f

Browse files
committed
feat: connect chat component with history
1 parent 1c3072c commit 1f1fe4f

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

packages/webapp/index.html

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,26 @@
6262
AI Chat with Enterprise Data
6363
</nav>
6464
<main>
65-
<azc-history onLoadSession="chat.messages = session.messages"></azc-history>
65+
<azc-history id="chatHistory"></azc-history>
6666
<azc-chat id="chat"></azc-chat>
6767
</main>
6868
<script type="module" src="/src/index.ts"></script>
69+
<script>
70+
// Generate a unique ID and store it in local storage
71+
const userId = localStorage.getItem('userId') || crypto.randomUUID();
72+
localStorage.setItem('userId', userId);
73+
74+
window.chatHistory.userId = userId;
75+
window.chatHistory.addEventListener('loadSession', (e) => {
76+
const { id, messages } = e.detail;
77+
window.chat.sessionId = id;
78+
window.chat.messages = messages;
79+
});
80+
81+
window.chat.userId = userId;
82+
window.chat.addEventListener('messagesUpdated', () => {
83+
window.chatHistory.refresh();
84+
});
85+
</script>
6986
</body>
7087
</html>

packages/webapp/src/components/chat.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ export class ChatComponent extends LitElement {
9797
window.open(path, '_blank');
9898
}
9999

100+
onNewChatClicked() {
101+
this.messages = [];
102+
this.sessionId = '';
103+
this.fireMessagesUpdatedEvent();
104+
}
105+
100106
async onKeyPressed(event: KeyboardEvent) {
101107
if (event.key === 'Enter') {
102108
event.preventDefault();
@@ -124,7 +130,14 @@ export class ChatComponent extends LitElement {
124130
this.isLoading = true;
125131
this.scrollToLastMessage();
126132
try {
127-
const response = getCompletion({ ...this.options, messages: this.messages });
133+
const response = getCompletion({
134+
...this.options,
135+
messages: this.messages,
136+
context: {
137+
userId: this.userId,
138+
sessionId: this.sessionId,
139+
},
140+
});
128141
const chunks = response as AsyncGenerator<AIChatCompletionDelta>;
129142
const { messages } = this;
130143
const message: AIChatMessage = {
@@ -136,12 +149,17 @@ export class ChatComponent extends LitElement {
136149
this.isStreaming = true;
137150
message.content += chunk.delta.content;
138151
this.messages = [...messages, message];
139-
this.scrollToLastMessage();
152+
}
153+
154+
const sessionId = (chunk.context as any)?.sessionId;
155+
if (!this.sessionId && sessionId) {
156+
this.sessionId = sessionId;
140157
}
141158
}
142159

143160
this.isLoading = false;
144161
this.isStreaming = false;
162+
this.fireMessagesUpdatedEvent();
145163
} catch (error) {
146164
this.hasError = true;
147165
this.isLoading = false;
@@ -152,11 +170,7 @@ export class ChatComponent extends LitElement {
152170

153171
override requestUpdate(name?: string, oldValue?: any) {
154172
if (name === 'messages') {
155-
const messagesUpdatedEvent = new CustomEvent('messagesUpdated', {
156-
detail: { messages: this.messages },
157-
bubbles: true,
158-
});
159-
this.dispatchEvent(messagesUpdatedEvent);
173+
this.scrollToLastMessage();
160174
} else if (name === 'hasError' || name === 'isLoading' || name === 'isStreaming') {
161175
const state = {
162176
hasError: this.hasError,
@@ -173,6 +187,14 @@ export class ChatComponent extends LitElement {
173187
super.requestUpdate(name, oldValue);
174188
}
175189

190+
protected fireMessagesUpdatedEvent() {
191+
const messagesUpdatedEvent = new CustomEvent('messagesUpdated', {
192+
detail: { messages: this.messages },
193+
bubbles: true,
194+
});
195+
this.dispatchEvent(messagesUpdatedEvent);
196+
}
197+
176198
protected scrollToLastMessage() {
177199
// Need to be delayed to run after the DOM refresh
178200
setTimeout(() => {
@@ -286,7 +308,7 @@ export class ChatComponent extends LitElement {
286308
<button
287309
class="button new-chat-button"
288310
@click=${() => {
289-
this.messages = [];
311+
this.onNewChatClicked();
290312
}}
291313
title=${this.options.strings.newChatButton}
292314
.disabled=${this.messages?.length === 0 || this.isLoading || this.isStreaming}

0 commit comments

Comments
 (0)