diff --git a/src/apps/call/Telephone.tsx b/src/apps/call/Telephone.tsx index e964ab8c0..a762f5bf0 100644 --- a/src/apps/call/Telephone.tsx +++ b/src/apps/call/Telephone.tsx @@ -224,8 +224,9 @@ export function Telephone(props: { responseAbortController.current = new AbortController(); let finalText = ''; let error: any | null = null; - llmStreamingChatGenerate(chatLLMId, callPrompt, null, null, responseAbortController.current.signal, (updatedMessage: Partial) => { - const text = updatedMessage.text?.trim(); + setPersonaTextInterim('💭...'); + llmStreamingChatGenerate(chatLLMId, callPrompt, null, null, responseAbortController.current.signal, ({ textSoFar }) => { + const text = textSoFar?.trim(); if (text) { finalText = text; setPersonaTextInterim(text); @@ -354,7 +355,8 @@ export function Telephone(props: { text={message.text} variant={message.role === 'assistant' ? 'solid' : 'soft'} color={message.role === 'assistant' ? 'neutral' : 'primary'} - role={message.role} />, + role={message.role} + />, )} {/* Persona streaming text... */} diff --git a/src/apps/chat/editors/chat-stream.ts b/src/apps/chat/editors/chat-stream.ts index 3501f4647..e00ca7950 100644 --- a/src/apps/chat/editors/chat-stream.ts +++ b/src/apps/chat/editors/chat-stream.ts @@ -1,4 +1,5 @@ -import { DLLMId } from '~/modules/llms/store-llms'; +import type { DLLMId } from '~/modules/llms/store-llms'; +import type { StreamingClientUpdate } from '~/modules/llms/vendors/unifiedStreamingClient'; import { SystemPurposeId } from '../../../data'; import { autoSuggestions } from '~/modules/aifn/autosuggestions/autoSuggestions'; import { conversationAutoTitle } from '~/modules/aifn/autotitle/autoTitle'; @@ -67,7 +68,6 @@ async function streamAssistantMessage( const messages = history.map(({ role, text }) => ({ role, content: text })); - const incrementalAnswer: Partial = { text: '' }; // Throttling setup let lastCallTime = 0; @@ -83,33 +83,34 @@ async function streamAssistantMessage( } } + const incrementalAnswer: Partial = { text: '' }; + try { - await llmStreamingChatGenerate(llmId, messages, null, null, abortSignal, - ({ originLLM, textSoFar, typing }) => { + await llmStreamingChatGenerate(llmId, messages, null, null, abortSignal, (update: StreamingClientUpdate) => { + const textSoFar = update.textSoFar; - // grow the incremental message - if (originLLM) incrementalAnswer.originLLM = originLLM; - if (textSoFar) incrementalAnswer.text = textSoFar; - if (typing !== undefined) incrementalAnswer.typing = typing; + // grow the incremental message + if (update.originLLM) incrementalAnswer.originLLM = update.originLLM; + if (textSoFar) incrementalAnswer.text = textSoFar; + if (update.typing !== undefined) incrementalAnswer.typing = update.typing; - // Update the data store, with optional max-frequency throttling (e.g. OpenAI is downsamped 50 -> 12Hz) - // This can be toggled from the settings - throttledEditMessage(incrementalAnswer); + // Update the data store, with optional max-frequency throttling (e.g. OpenAI is downsamped 50 -> 12Hz) + // This can be toggled from the settings + throttledEditMessage(incrementalAnswer); - // 📢 TTS: first-line - if (textSoFar && autoSpeak === 'firstLine' && !spokenLine) { - let cutPoint = textSoFar.lastIndexOf('\n'); - if (cutPoint < 0) - cutPoint = textSoFar.lastIndexOf('. '); - if (cutPoint > 100 && cutPoint < 400) { - spokenLine = true; - const firstParagraph = textSoFar.substring(0, cutPoint); - // fire/forget: we don't want to stall this loop - void speakText(firstParagraph); - } + // 📢 TTS: first-line + if (textSoFar && autoSpeak === 'firstLine' && !spokenLine) { + let cutPoint = textSoFar.lastIndexOf('\n'); + if (cutPoint < 0) + cutPoint = textSoFar.lastIndexOf('. '); + if (cutPoint > 100 && cutPoint < 400) { + spokenLine = true; + const firstParagraph = textSoFar.substring(0, cutPoint); + // fire/forget: we don't want to stall this loop + void speakText(firstParagraph); } - }, - ); + } + }); } catch (error: any) { if (error?.name !== 'AbortError') { console.error('Fetch request error:', error); diff --git a/src/modules/aifn/useLLMChain.ts b/src/modules/aifn/useLLMChain.ts index 35253609b..091ca03fd 100644 --- a/src/modules/aifn/useLLMChain.ts +++ b/src/modules/aifn/useLLMChain.ts @@ -115,7 +115,7 @@ export function useLLMChain(steps: LLMChainStep[], llmId: DLLMId | undefined, ch // LLM call (streaming, cancelable) llmStreamingChatGenerate(llmId, llmChatInput, null, null, stepAbortController.signal, - ({textSoFar}) => { + ({ textSoFar }) => { textSoFar && setChainStepInterimText(interimText = textSoFar); }) .then(() => {