Skip to content

Commit c6a7bd2

Browse files
committed
fix: pass the chat history in the api call
1 parent bc6dbe9 commit c6a7bd2

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

packages/chat-component/src/components/chat-component.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { chatHttpOptions, globalConfig, teaserListTexts, requestOptions } from '
77
import { chatStyle } from '../styles/chat-component.js';
88
import { unsafeSVG } from 'lit/directives/unsafe-svg.js';
99
import { produce } from 'immer';
10+
import { chatEntryToString } from '../utils/index.js';
1011

1112
// TODO: allow host applications to customize these icons
1213

@@ -130,6 +131,27 @@ export class ChatComponent extends LitElement {
130131
}
131132
}
132133

134+
getMessageContext(): Message[] {
135+
if (this.interactionModel === 'ask') {
136+
return [];
137+
}
138+
139+
const history = [
140+
...this.chatThread,
141+
// include the history from the previous session if the user has enabled the chat history
142+
...(this.chatHistoryController.showChatHistory ? this.chatHistoryController.chatHistory : []),
143+
];
144+
145+
const messages: Message[] = history.map((entry) => {
146+
return {
147+
content: chatEntryToString(entry),
148+
role: entry.isUserMessage ? 'user' : 'assistant',
149+
};
150+
});
151+
152+
return messages;
153+
}
154+
133155
// Handle the click on the chat button and send the question to the API
134156
async handleUserChatSubmit(event: Event): Promise<void> {
135157
event.preventDefault();
@@ -148,6 +170,7 @@ export class ChatComponent extends LitElement {
148170
},
149171
question,
150172
type: this.interactionModel,
173+
messages: this.getMessageContext(),
151174
},
152175
{
153176
// use defaults

packages/chat-component/src/components/chat-thread-component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { styles } from '../styles/chat-thread-component.js';
66
import { globalConfig } from '../config/global-config.js';
77
import { unsafeSVG } from 'lit/directives/unsafe-svg.js';
88
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
9+
import { chatEntryToString } from '../utils/index.js';
910

1011
import iconSuccess from '../../public/svg/success-icon.svg?raw';
1112
import iconCopyToClipboard from '../../public/svg/copy-icon.svg?raw';
@@ -42,9 +43,7 @@ export class ChatThreadComponent extends LitElement {
4243

4344
// Copy response to clipboard
4445
copyResponseToClipboard(entry: ChatThreadEntry): void {
45-
const response = entry.text
46-
.map((textEntry) => textEntry.value + '\n\n' + textEntry.followingSteps?.map((s) => ' - ' + s).join('\n'))
47-
.join('\n\n');
46+
const response = chatEntryToString(entry);
4847

4948
navigator.clipboard.writeText(response);
5049
this.isResponseCopied = true;

packages/chat-component/src/core/http/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ChatResponseError } from '../../utils/index.js';
22

33
export async function callHttpApi(
4-
{ question, type, approach, overrides }: ChatRequestOptions,
4+
{ question, type, approach, overrides, messages }: ChatRequestOptions,
55
{ method, url, stream, signal }: ChatHttpOptions,
66
) {
77
return await fetch(`${url}/${type}`, {
@@ -12,6 +12,7 @@ export async function callHttpApi(
1212
signal,
1313
body: JSON.stringify({
1414
messages: [
15+
...(messages ?? []),
1516
{
1617
content: question,
1718
role: 'user',

packages/chat-component/src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ declare interface ChatRequestOptions {
4040
overrides: RequestOverrides;
4141
type: string;
4242
question: string;
43+
messages?: Message[];
4344
}
4445

4546
declare interface RequestOverrides {

packages/chat-component/src/utils/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ export function getTimestamp() {
7777
});
7878
}
7979

80+
export function chatEntryToString(entry: ChatThreadEntry) {
81+
const message = entry.text
82+
.map((textEntry) => textEntry.value + '\n\n' + textEntry.followingSteps?.map((s, i) => `${i + 1}.` + s).join('\n'))
83+
.join('\n\n')
84+
.replaceAll(/<sup[^>]*>(.*?)<\/sup>/g, ''); // remove the <sup> tags from the message
85+
86+
return message;
87+
}
88+
8089
// Creates a new chat message error
8190
export class ChatResponseError extends Error {
8291
code?: number;

0 commit comments

Comments
 (0)