Skip to content

Commit a4411bb

Browse files
authored
Merge pull request #96 from iceljc/features/refine-chat-window
fix postback error
2 parents 9790bb3 + c5348f1 commit a4411bb

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/routes/chat/[agentId]/[conversationId]/chat-box.svelte

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
}) || [];
150150
151151
const savedMessages = conversationUserMessageStore.get();
152+
// @ts-ignore
152153
const otherConvMessages = savedMessages?.messages?.filter(x => x.conversationId !== params.conversationId) || [];
153154
const allMessages = [...otherConvMessages, ...curConvMessages];
154155
const trimmedMessages = trimUserSentMessages(allMessages);
@@ -273,7 +274,7 @@
273274
truncateDialogs(data.message_id);
274275
}
275276
276-
async function newConversationHandler() {
277+
async function handleNewConversation() {
277278
const conversation = await newConversation(params.agentId);
278279
conversationStore.set(conversation);
279280
const url = `chat/${params.agentId}/${conversation.id}`;
@@ -289,7 +290,7 @@
289290
isSendingMsg = true;
290291
renewUserSentMessages(msgText);
291292
292-
const postback = buildPostbackMessage(dialogs, data?.payload || null);
293+
const postback = buildPostbackMessage(dialogs, data?.payload || msgText, data?.truncateMsgId);
293294
/** @type {import('$types').MessageData?} */
294295
const messageData = {
295296
...data,
@@ -381,11 +382,19 @@
381382
/**
382383
* @param {import('$types').ChatResponseModel[]} dialogs
383384
* @param {string?} content
385+
* @param {string?} [truncateMsgId]
384386
*/
385-
function buildPostbackMessage(dialogs, content) {
387+
function buildPostbackMessage(dialogs, content, truncateMsgId) {
386388
/** @type {import('$types').Postback?} */
387389
let postback = null;
388-
const lastMsg = dialogs.slice(-1)[0];
390+
let lastMsg = dialogs.slice(-1)[0];
391+
392+
if (!!truncateMsgId) {
393+
const foundIdx = dialogs.findIndex(x => x.message_id === truncateMsgId);
394+
const truncatedDialogs = dialogs.filter((x, idx) => idx < foundIdx);
395+
lastMsg = truncatedDialogs.slice(-1)[0];
396+
}
397+
389398
if (!!lastMsg?.rich_content?.fill_postback
390399
&& !!lastMsg?.function
391400
&& lastMsg?.sender?.role === UserRole.Assistant) {
@@ -748,7 +757,7 @@
748757
</Dropdown>
749758
</li>
750759
{/if}
751-
<DropdownItem on:click={newConversationHandler}>New Conversation</DropdownItem>
760+
<DropdownItem on:click={handleNewConversation}>New Conversation</DropdownItem>
752761
</DropdownMenu>
753762
</Dropdown>
754763
</li>

0 commit comments

Comments
 (0)