Skip to content

Commit ea28417

Browse files
committed
fix(): show error message when conversation finished early
1 parent d049e26 commit ea28417

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

bricks/ai-portal/src/chat-panel/index.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import type {
1818
ModalMapEvents,
1919
} from "@next-bricks/containers/modal";
2020
import { http } from "@next-core/http";
21+
import type { UseBrickConf } from "@next-core/types";
22+
import { ReactUseMultipleBricks } from "@next-core/react-runtime";
2123
import styles from "./styles.module.css";
2224
import type { ChatInput } from "../chat-input";
2325
import type {
@@ -39,13 +41,11 @@ import { AssistantMessage } from "../chat-stream/AssistantMessage/AssistantMessa
3941
import scrollStyles from "../chat-stream/ScrollDownButton.module.css";
4042
import floatingStyles from "../shared/FloatingButton.module.css";
4143
import backgroundImage from "../home-container/images/background.png";
42-
import { DONE_STATES } from "../shared/constants";
44+
import { DONE_STATES, NON_WORKING_STATES } from "../shared/constants";
4345
import { WrappedChatInput, WrappedIcon } from "../shared/bricks";
4446
import { FilePreview } from "../shared/FilePreview/FilePreview.js";
4547
import { ImagesPreview } from "../shared/FilePreview/ImagesPreview.js";
4648
import { TaskContext, type TaskContextValue } from "../shared/TaskContext";
47-
import type { UseBrickConf } from "@next-core/types";
48-
import { ReactUseMultipleBricks } from "@next-core/react-runtime";
4949

5050
const WrappedModal = wrapBrick<
5151
Modal,
@@ -327,6 +327,9 @@ function LegacyChatPanelComponent(
327327
[handleChatSubmit]
328328
);
329329

330+
const earlyFinished =
331+
conversation?.finished && !NON_WORKING_STATES.includes(conversationState!);
332+
330333
return (
331334
<TaskContext.Provider value={taskContextValue}>
332335
<WrappedModal
@@ -365,11 +368,17 @@ function LegacyChatPanelComponent(
365368
<AssistantMessage
366369
chunks={msg.chunks}
367370
scopeState={conversation.state}
368-
isLatest={index === list.length - 1}
371+
isLatest={index === list.length - 1 && !earlyFinished}
372+
finished={conversation.finished}
369373
/>
370374
)}
371375
</div>
372376
))}
377+
{earlyFinished && (
378+
<div className={styles.message}>
379+
<AssistantMessage earlyFinished />
380+
</div>
381+
)}
373382
</div>
374383
</div>
375384
<button

bricks/ai-portal/src/chat-stream/AssistantMessage/AssistantMessage.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,33 @@ import { NodeChunk } from "../NodeChunk/NodeChunk";
1717
import { HumanInTheLoop } from "../HumanInTheLoop/HumanInTheLoop";
1818

1919
export interface AssistantMessageProps {
20-
chunks: MessageChunk[];
21-
scopeState: ConversationState | TaskState | JobState | undefined;
20+
chunks?: MessageChunk[];
21+
scopeState?: ConversationState | TaskState | JobState | undefined;
2222
isLatest?: boolean;
2323
isSubTask?: boolean;
24+
finished?: boolean;
25+
earlyFinished?: boolean;
2426
}
2527

28+
const EARLY_FINISHED_ERROR_CHUNK: MessageChunk = {
29+
type: "error",
30+
error:
31+
"The conversation was marked as finished early due to an unexpected issue.\nPlease refresh the page or try again later.",
32+
};
33+
2634
export function AssistantMessage({
2735
chunks,
2836
scopeState,
2937
isLatest,
3038
isSubTask,
39+
finished,
40+
earlyFinished,
3141
}: AssistantMessageProps) {
3242
const [working, lastJob] = useMemo(() => {
43+
if (!chunks || chunks.length === 0) {
44+
return [false, undefined];
45+
}
46+
3347
const lastChunk = chunks[chunks.length - 1];
3448
let lastJob: Job | undefined;
3549
if (lastChunk) {
@@ -79,10 +93,10 @@ export function AssistantMessage({
7993
className={styles.avatar}
8094
/>
8195
<div className={styles.body}>
82-
{chunks.map((chunk, index) => (
96+
{chunks?.map((chunk, index) => (
8397
<NodeChunk key={index} chunk={chunk} isSubTask={isSubTask} />
8498
))}
85-
{working && <div className={styles.texting}></div>}
99+
{working && !finished && <div className={styles.texting}></div>}
86100
{isLatest &&
87101
lastJob &&
88102
!GENERAL_DONE_STATES.includes(scopeState) &&
@@ -98,6 +112,7 @@ export function AssistantMessage({
98112
/>
99113
)
100114
))}
115+
{earlyFinished && <NodeChunk chunk={EARLY_FINISHED_ERROR_CHUNK} />}
101116
</div>
102117
</div>
103118
);

bricks/ai-portal/src/chat-stream/ChatStream.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import { UserMessage } from "./UserMessage/UserMessage.js";
1717
import { AssistantMessage } from "./AssistantMessage/AssistantMessage.js";
1818
import { TaskContext } from "../shared/TaskContext.js";
1919
import { ChatBox } from "../shared/ChatBox/ChatBox.js";
20-
import { DONE_STATES, ICON_CANVAS } from "../shared/constants.js";
20+
import {
21+
DONE_STATES,
22+
ICON_CANVAS,
23+
NON_WORKING_STATES,
24+
} from "../shared/constants.js";
2125
import { ExpandedView } from "../shared/ExpandedView/ExpandedView.js";
2226
import { Aside } from "./Aside/Aside.js";
2327
import { StreamContext } from "./StreamContext.js";
@@ -384,6 +388,11 @@ export function ChatStreamComponent(
384388
scrollContainerRef.current?.focus();
385389
}, [conversationAvailable, depsReady]);
386390

391+
const earlyFinished =
392+
!replay &&
393+
conversation?.finished &&
394+
!NON_WORKING_STATES.includes(conversationState!);
395+
387396
return (
388397
<TaskContext.Provider value={taskContextValue}>
389398
<StreamContext.Provider value={streamContextValue}>
@@ -412,11 +421,17 @@ export function ChatStreamComponent(
412421
<AssistantMessage
413422
chunks={msg.chunks}
414423
scopeState={conversationState}
415-
isLatest={index === list.length - 1}
424+
isLatest={index === list.length - 1 && !earlyFinished}
425+
finished={conversation.finished}
416426
/>
417427
)}
418428
</div>
419429
))}
430+
{earlyFinished && (
431+
<div className={styles.message}>
432+
<AssistantMessage earlyFinished />
433+
</div>
434+
)}
420435
{replay
421436
? conversation?.finished && (
422437
<>

0 commit comments

Comments
 (0)