Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/components/AIStateIndicator/hooks/useAIState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const AIStates = {
ExternalSources: 'AI_STATE_EXTERNAL_SOURCES',
Generating: 'AI_STATE_GENERATING',
Idle: 'AI_STATE_IDLE',
Stop: 'AI_STATE_STOP',
Thinking: 'AI_STATE_THINKING',
};

Expand Down Expand Up @@ -37,9 +38,17 @@ export const useAIState = (channel?: Channel): { aiState: AIState } => {
}
});

const indicatorStoppedListener = channel.on('ai_indicator.stop', (event) => {
const { cid } = event;
if (channel.cid === cid) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is useless but I'm keeping it.

setAiState(AIStates.Stop);
}
});

return () => {
indicatorChangedListener.unsubscribe();
indicatorClearedListener.unsubscribe();
indicatorStoppedListener.unsubscribe();
};
}, [channel]);

Expand Down
13 changes: 10 additions & 3 deletions src/components/Message/StreamedMessageText.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';
import React, { useEffect } from 'react';

import type { MessageTextProps } from './MessageText';
import { MessageText } from './MessageText';

import { useMessageContext } from '../../context';
import { useChannelStateContext, useMessageContext } from '../../context';
import { useMessageTextStreaming } from './hooks';

export type StreamedMessageTextProps = Pick<
Expand All @@ -22,14 +22,21 @@ export const StreamedMessageText = (props: StreamedMessageTextProps) => {
streamingLetterIntervalMs,
} = props;
const { message: messageFromContext } = useMessageContext('StreamedMessageText');
const { channel } = useChannelStateContext();
const message = messageFromProps || messageFromContext;
const { text = '' } = message;
const { streamedMessageText } = useMessageTextStreaming({
const { skipAnimation, streamedMessageText } = useMessageTextStreaming({
renderingLetterCount,
streamingLetterIntervalMs,
text,
});

useEffect(() => {
channel?.on('ai_indicator.stop', () => {
skipAnimation();
});
}, [channel, skipAnimation]);

return (
<MessageText
message={{ ...message, text: streamedMessageText }}
Expand Down
12 changes: 10 additions & 2 deletions src/components/Message/hooks/useMessageTextStreaming.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useRef, useState } from 'react';

import { useStableCallback } from '../../../utils/useStableCallback';
import type { StreamedMessageTextProps } from '../StreamedMessageText';

export type UseMessageTextStreamingProps = Pick<
Expand All @@ -22,15 +23,17 @@ export const useMessageTextStreaming = ({
renderingLetterCount = DEFAULT_RENDERING_LETTER_COUNT,
streamingLetterIntervalMs = DEFAULT_LETTER_INTERVAL,
text,
}: UseMessageTextStreamingProps): { streamedMessageText: string } => {
}: UseMessageTextStreamingProps) => {
const [streamedMessageText, setStreamedMessageText] = useState<string>(text);
const textCursor = useRef<number>(text.length);

useEffect(() => {
const textLength = text.length;

const interval = setInterval(() => {
if (!text || textCursor.current >= textLength) {
clearInterval(interval);
return;
}
const newCursorValue = textCursor.current + renderingLetterCount;
const newText = text.substring(0, newCursorValue);
Expand All @@ -43,5 +46,10 @@ export const useMessageTextStreaming = ({
};
}, [streamingLetterIntervalMs, renderingLetterCount, text]);

return { streamedMessageText };
const skipAnimation = useStableCallback(() => {
textCursor.current = text.length;
setStreamedMessageText(text);
});

return { skipAnimation, streamedMessageText } as const;
};
Loading