Fix automatic summarization Closes #2746
| @@ -15,6 +15,7 @@ import { | |||
| 15 | generateRaw, | 15 | generateRaw, |
| 16 | getMaxContextSize, | 16 | getMaxContextSize, |
| 17 | setExtensionPrompt, | 17 | setExtensionPrompt, |
| 18 | streamingProcessor, | ||
| 18 | } from '../../../script.js'; | 19 | } from '../../../script.js'; |
| 19 | import { is_group_generating, selected_group } from '../../group-chats.js'; | 20 | import { is_group_generating, selected_group } from '../../group-chats.js'; |
| 20 | import { loadMovingUIState } from '../../power-user.js'; | 21 | import { loadMovingUIState } from '../../power-user.js'; |
| @@ -408,8 +409,8 @@ async function onChatEvent() { | |||
| 408 | return; | 409 | return; |
| 409 | } | 410 | } |
| 410 | 411 | ||
| 411 | // Generation is in progress, summary prevented | 412 | // Streaming in-progress |
| 412 | if (is_send_press) { | 413 | if (streamingProcessor && !streamingProcessor.isFinished) { |
| 413 | return; | 414 | return; |
| 414 | } | 415 | } |
| 415 | 416 | ||
| @@ -446,15 +447,9 @@ async function onChatEvent() { | |||
| 446 | delete chat[chat.length - 1].extra.memory; | 447 | delete chat[chat.length - 1].extra.memory; |
| 447 | } | 448 | } |
| 448 | 449 | ||
| 449 | try { | 450 | summarizeChat(context) |
| 450 | await summarizeChat(context); | 451 | .catch(console.error) |
| 451 | } | 452 | .finally(saveLastValues); |
| 452 | catch (error) { | ||
| 453 | console.log(error); | ||
| 454 | } | ||
| 455 | finally { | ||
| 456 | saveLastValues(); | ||
| 457 | } | ||
| 458 | } | 453 | } |
| 459 | 454 | ||
| 460 | /** | 455 | /** |
| @@ -567,7 +562,7 @@ async function getSummaryPromptForNow(context, force) { | |||
| 567 | await waitUntilCondition(() => is_group_generating === false, 1000, 10); | 562 | await waitUntilCondition(() => is_group_generating === false, 1000, 10); |
| 568 | } | 563 | } |
| 569 | // Wait for the send button to be released | 564 | // Wait for the send button to be released |
| 570 | waitUntilCondition(() => is_send_press === false, 30000, 100); | 565 | await waitUntilCondition(() => is_send_press === false, 30000, 100); |
| 571 | } catch { | 566 | } catch { |
| 572 | console.debug('Timeout waiting for is_send_press'); | 567 | console.debug('Timeout waiting for is_send_press'); |
| 573 | return ''; | 568 | return ''; |
| @@ -650,9 +645,16 @@ async function summarizeChatWebLLM(context, force) { | |||
| 650 | params.max_tokens = extension_settings.memory.overrideResponseLength; | 645 | params.max_tokens = extension_settings.memory.overrideResponseLength; |
| 651 | } | 646 | } |
| 652 | 647 | ||
| 648 | try { | ||
| 649 | inApiCall = true; | ||
| 653 | const summary = await generateWebLlmChatPrompt(messages, params); | 650 | const summary = await generateWebLlmChatPrompt(messages, params); |
| 654 | const newContext = getContext(); | 651 | const newContext = getContext(); |
| 655 | 652 | ||
| 653 | if (!summary) { | ||
| 654 | console.warn('Empty summary received'); | ||
| 655 | return; | ||
| 656 | } | ||
| 657 | |||
| 656 | // something changed during summarization request | 658 | // something changed during summarization request |
| 657 | if (newContext.groupId !== context.groupId || | 659 | if (newContext.groupId !== context.groupId || |
| 658 | newContext.chatId !== context.chatId || | 660 | newContext.chatId !== context.chatId || |
| @@ -663,6 +665,9 @@ async function summarizeChatWebLLM(context, force) { | |||
| 663 | 665 | ||
| 664 | setMemoryContext(summary, true, lastUsedIndex); | 666 | setMemoryContext(summary, true, lastUsedIndex); |
| 665 | return summary; | 667 | return summary; |
| 668 | } finally { | ||
| 669 | inApiCall = false; | ||
| 670 | } | ||
| 666 | } | 671 | } |
| 667 | 672 | ||
| 668 | async function summarizeChatMain(context, force, skipWIAN) { | 673 | async function summarizeChatMain(context, force, skipWIAN) { |
| @@ -677,12 +682,18 @@ async function summarizeChatMain(context, force, skipWIAN) { | |||
| 677 | let index = null; | 682 | let index = null; |
| 678 | 683 | ||
| 679 | if (prompt_builders.DEFAULT === extension_settings.memory.prompt_builder) { | 684 | if (prompt_builders.DEFAULT === extension_settings.memory.prompt_builder) { |
| 685 | try { | ||
| 686 | inApiCall = true; | ||
| 680 | summary = await generateQuietPrompt(prompt, false, skipWIAN, '', '', extension_settings.memory.overrideResponseLength); | 687 | summary = await generateQuietPrompt(prompt, false, skipWIAN, '', '', extension_settings.memory.overrideResponseLength); |
| 688 | } finally { | ||
| 689 | inApiCall = false; | ||
| 690 | } | ||
| 681 | } | 691 | } |
| 682 | 692 | ||
| 683 | if ([prompt_builders.RAW_BLOCKING, prompt_builders.RAW_NON_BLOCKING].includes(extension_settings.memory.prompt_builder)) { | 693 | if ([prompt_builders.RAW_BLOCKING, prompt_builders.RAW_NON_BLOCKING].includes(extension_settings.memory.prompt_builder)) { |
| 684 | const lock = extension_settings.memory.prompt_builder === prompt_builders.RAW_BLOCKING; | 694 | const lock = extension_settings.memory.prompt_builder === prompt_builders.RAW_BLOCKING; |
| 685 | try { | 695 | try { |
| 696 | inApiCall = true; | ||
| 686 | if (lock) { | 697 | if (lock) { |
| 687 | deactivateSendButtons(); | 698 | deactivateSendButtons(); |
| 688 | } | 699 | } |
| @@ -700,12 +711,18 @@ async function summarizeChatMain(context, force, skipWIAN) { | |||
| 700 | summary = await generateRaw(rawPrompt, '', false, false, prompt, extension_settings.memory.overrideResponseLength); | 711 | summary = await generateRaw(rawPrompt, '', false, false, prompt, extension_settings.memory.overrideResponseLength); |
| 701 | index = lastUsedIndex; | 712 | index = lastUsedIndex; |
| 702 | } finally { | 713 | } finally { |
| 714 | inApiCall = false; | ||
| 703 | if (lock) { | 715 | if (lock) { |
| 704 | activateSendButtons(); | 716 | activateSendButtons(); |
| 705 | } | 717 | } |
| 706 | } | 718 | } |
| 707 | } | 719 | } |
| 708 | 720 | ||
| 721 | if (!summary) { | ||
| 722 | console.warn('Empty summary received'); | ||
| 723 | return; | ||
| 724 | } | ||
| 725 | |||
| 709 | const newContext = getContext(); | 726 | const newContext = getContext(); |
| 710 | 727 | ||
| 711 | // something changed during summarization request | 728 | // something changed during summarization request |
| @@ -840,6 +857,11 @@ async function summarizeChatExtras(context) { | |||
| 840 | const summary = await callExtrasSummarizeAPI(resultingString); | 857 | const summary = await callExtrasSummarizeAPI(resultingString); |
| 841 | const newContext = getContext(); | 858 | const newContext = getContext(); |
| 842 | 859 | ||
| 860 | if (!summary) { | ||
| 861 | console.warn('Empty summary received'); | ||
| 862 | return; | ||
| 863 | } | ||
| 864 | |||
| 843 | // something changed during summarization request | 865 | // something changed during summarization request |
| 844 | if (newContext.groupId !== context.groupId | 866 | if (newContext.groupId !== context.groupId |
| 845 | || newContext.chatId !== context.chatId | 867 | || newContext.chatId !== context.chatId |