Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 5x 5x 5x 5x 5x 5x 4x 5x 5x 4x 3x 5x 1x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 2x 2x 4x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 7x 6x 8x 8x 8x 3x 1x 1x 3x 8x 5x 5x 4x 4x 4x 4x 3x 3x 3x 1x 1x 1x 1x 1x 5x 5x 5x 4x 4x 4x 5x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4x 4x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 4x 4x 4x 4x 4x 4x 8x 4x 4x 8x 8x 8x 8x | // Libs
import { useEffect, useRef, useState } from 'react';
import { AssistantMessageProps, Markdown } from '@copilotkit/react-ui';
import { useCopilotChat } from '@copilotkit/react-core';
import {
ActionExecutionMessage,
Message,
Role,
TextMessage,
} from '@copilotkit/runtime-client-gql';
// Constants
import {
CHATBOT_MESSAGES,
COPILOT_LABEL,
IMAGES,
LONG_RESPONSE_TIME,
} from '@shared/constants';
// Components
import { ErrorBoundaryChatBot } from './ErrorBoundaryChatBot';
import { ImageFallback } from '../common';
import { InterruptedMessage } from './InterruptedMessage';
import { ProcessingIndicator } from './ProcessingIndicator';
const isUnresponsiveTextMessage = (message: TextMessage) => {
return message?.type === 'TextMessage' && !(message as TextMessage)?.content;
};
const re = /(^|_)call_/; // matches 'call_' or 'result-call_'
const findLatestCallItem = (messages: Message[]) => {
for (let i = messages.length - 1; i >= 0; i--) {
const item = messages[i];
if (item?.type === 'AgentStateMessage') {
break; // stop scanning when an AgentStateMessage appears after the candidate
}
if (
item?.id &&
re.test(item.id) &&
(item as unknown as { name: string })?.name ===
'searchKitchenStatisticTool'
) {
return item;
}
}
return null;
};
export const isUnresponsiveActionMessage = (
message: TextMessage | ActionExecutionMessage,
messages: Message[],
) => {
const lastMessage = messages.at(-1);
const isFormOpening =
!!document?.querySelector('.cb-form-container') ||
!!document?.querySelector('.cb-form-description');
return (
message.type === 'ActionExecutionMessage' &&
lastMessage?.id === message.id &&
!isFormOpening
);
};
export const handleUnResponsiveMessage = (
appendMessage: (message: Message) => void,
stopGeneration: () => void,
content: string = CHATBOT_MESSAGES.HANG_ON,
messages: Message[],
) => {
const lastMessage = messages.at(-1);
if (
lastMessage?.type === 'TextMessage' &&
(lastMessage as TextMessage)?.content?.trim() !==
CHATBOT_MESSAGES.HANG_ON &&
(lastMessage as TextMessage)?.content?.trim() !==
CHATBOT_MESSAGES.INTERRUPTED
) {
const unresponsiveMessage = new TextMessage({
content,
role: Role.Assistant,
createdAt: lastMessage.createdAt,
});
appendMessage(unresponsiveMessage);
}
stopGeneration();
};
export const AssistantMessage = ({
message: originalMessage,
subComponent,
isLoading,
rawData,
isGenerating,
}: AssistantMessageProps) => {
const [isSlowResponse, setIsSlowResponse] = useState(false);
const {
stopGeneration,
appendMessage,
visibleMessages,
isLoading: isGlobalProcessing,
} = useCopilotChat();
// Normalize incoming message to a safe string value
const message =
typeof originalMessage === 'string'
? (originalMessage as string).trim()
: (
(originalMessage as unknown as { content?: string })?.content ?? ''
).trim();
const isInitialMessage = message === COPILOT_LABEL.INITIAL_MESSAGE;
const isProcessing = isGenerating || isLoading;
const isInternalGenerating =
isGlobalProcessing &&
findLatestCallItem(visibleMessages)?.id === rawData?.id;
const unresponsiveSystem =
message === CHATBOT_MESSAGES.HANG_ON ||
message === CHATBOT_MESSAGES.INTERRUPTED ||
isSlowResponse;
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
/**
* Process if the message is unresponsive or slow
*/
useEffect(() => {
if (isLoading) {
timeoutRef.current = setTimeout(() => {
setIsSlowResponse(true);
stopGeneration();
}, LONG_RESPONSE_TIME); // Trigger after 30s of loading
} else {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
timeoutRef.current = null;
}
if (!isSlowResponse) {
const isUnresponsive =
rawData?.type === 'TextMessage'
? isUnresponsiveTextMessage(rawData)
: rawData?.type === 'ActionExecutionMessage'
? isUnresponsiveActionMessage(rawData, visibleMessages)
: false;
if (!isUnresponsive) {
setIsSlowResponse(false);
return;
}
// Delay marking as slow to avoid false positives
timeoutRef.current = setTimeout(() => {
setIsSlowResponse(true); // Trigger long-response marker
stopGeneration();
}, LONG_RESPONSE_TIME);
}
}
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
timeoutRef.current = null;
}
};
}, [
isLoading,
isSlowResponse,
setIsSlowResponse,
rawData,
visibleMessages,
stopGeneration,
]);
const cleanedMessage = message?.replace(/\{[^}]*\}/g, '').trim() ?? '';
const content = unresponsiveSystem
? cleanedMessage || CHATBOT_MESSAGES.HANG_ON
: cleanedMessage;
return (
<ErrorBoundaryChatBot
onRespondError={() => {
handleUnResponsiveMessage(
appendMessage,
stopGeneration,
CHATBOT_MESSAGES.ERROR,
visibleMessages,
);
}}
fallback={
<InterruptedMessage message={CHATBOT_MESSAGES.ERROR_FALLBACK} />
}
>
<div className="max-w-cb-message-box gap-4 pt-2.5 pb-1">
{/** INITIAL MESSAGE */}
{isInitialMessage && (
<>
<div className="relative w-cb-logo h-cb-logo ml-4 mb-1">
<ImageFallback
src={IMAGES.APP_LOGO}
alt="Logo for chatbot system"
className="object-cover"
/>
</div>
<div className="relative py-2.5 px-[15px] rounded-lg w-fit max-w-cb-message-box text-3xs leading-5 border shadow-sm border-primary-700 text-primary-700 dark:text-background dark:bg-secondary-900 dark:border-secondary-900">
<Markdown content={cleanedMessage} />
</div>
</>
)}
{/** INDICATOR DURING PROCESSING */}
{isProcessing && !isInternalGenerating && <ProcessingIndicator />}
{/** RESET BUTTON FOR UNRESPONSIVE MESSAGE */}
{!isProcessing && !isInitialMessage && (
<>
{content && (
<div className="relative py-2.5 px-[15px] rounded-lg w-fit max-w-cb-message-box text-3xs leading-5 border shadow-sm border-primary-700 text-primary-700 dark:text-background dark:bg-secondary-900 dark:border-secondary-900">
<Markdown content={content} />
</div>
)}
</>
)}
{!unresponsiveSystem &&
(isInternalGenerating ? (
<div className="mb-[15px]">{subComponent}</div>
) : (
subComponent
))}
{/** INDICATOR DURING Generation */}
{isInternalGenerating && <ProcessingIndicator />}
</div>
</ErrorBoundaryChatBot>
);
};
|