diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index 376c4e373..8b16ade0e 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -316,27 +316,45 @@ // Scroll down window.scrollTo({ top: document.body.scrollHeight }); + const messagesBody = [ + $settings.system + ? { + role: 'system', + content: $settings.system + } + : undefined, + ...messages + ] + .filter((message) => message) + .map((message, idx, arr) => ({ + role: message.role, + content: arr.length - 2 !== idx ? message.content : message?.raContent ?? message.content, + ...(message.files && { + images: message.files + .filter((file) => file.type === 'image') + .map((file) => file.url.slice(file.url.indexOf(',') + 1)) + }) + })); + + let lastImageIndex = -1; + + // Find the index of the last object with images + messagesBody.forEach((item, index) => { + if (item.images) { + lastImageIndex = index; + } + }); + + // Remove images from all but the last one + messagesBody.forEach((item, index) => { + if (index !== lastImageIndex) { + delete item.images; + } + }); + const [res, controller] = await generateChatCompletion(localStorage.token, { model: model, - messages: [ - $settings.system - ? { - role: 'system', - content: $settings.system - } - : undefined, - ...messages - ] - .filter((message) => message) - .map((message, idx, arr) => ({ - role: message.role, - content: arr.length - 2 !== idx ? message.content : message?.raContent ?? message.content, - ...(message.files && { - images: message.files - .filter((file) => file.type === 'image') - .map((file) => file.url.slice(file.url.indexOf(',') + 1)) - }) - })), + messages: messagesBody, options: { ...($settings.options ?? {}) }, diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index 83e72c62c..60eb7cec0 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -330,27 +330,45 @@ // Scroll down window.scrollTo({ top: document.body.scrollHeight }); + const messagesBody = [ + $settings.system + ? { + role: 'system', + content: $settings.system + } + : undefined, + ...messages + ] + .filter((message) => message) + .map((message, idx, arr) => ({ + role: message.role, + content: arr.length - 2 !== idx ? message.content : message?.raContent ?? message.content, + ...(message.files && { + images: message.files + .filter((file) => file.type === 'image') + .map((file) => file.url.slice(file.url.indexOf(',') + 1)) + }) + })); + + let lastImageIndex = -1; + + // Find the index of the last object with images + messagesBody.forEach((item, index) => { + if (item.images) { + lastImageIndex = index; + } + }); + + // Remove images from all but the last one + messagesBody.forEach((item, index) => { + if (index !== lastImageIndex) { + delete item.images; + } + }); + const [res, controller] = await generateChatCompletion(localStorage.token, { model: model, - messages: [ - $settings.system - ? { - role: 'system', - content: $settings.system - } - : undefined, - ...messages - ] - .filter((message) => message) - .map((message, idx, arr) => ({ - role: message.role, - content: arr.length - 2 !== idx ? message.content : message?.raContent ?? message.content, - ...(message.files && { - images: message.files - .filter((file) => file.type === 'image') - .map((file) => file.url.slice(file.url.indexOf(',') + 1)) - }) - })), + messages: messagesBody, options: { ...($settings.options ?? {}) }, @@ -358,6 +376,8 @@ }); if (res && res.ok) { + console.log('controller', controller); + const reader = res.body .pipeThrough(new TextDecoderStream()) .pipeThrough(splitStream('\n')) @@ -375,6 +395,7 @@ } currentRequestId = null; + break; }