Skip to content

Commit 1eb3814

Browse files
authored
♻️ Refactor: Front-end agent configuration, knowledge base related constants and types are placed in specific files
2 parents 3d436e6 + abfcaf4 commit 1eb3814

29 files changed

+493
-347
lines changed

frontend/app/[locale]/chat/internal/chatInterface.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useRouter } from "next/navigation";
77
import { v4 as uuidv4 } from "uuid";
88
import { useTranslation } from "react-i18next";
99

10+
import { ROLE_ASSISTANT } from "@/const/agentConfig";
1011
import { useConfig } from "@/hooks/useConfig";
1112
import { useAuth } from "@/hooks/useAuth";
1213
import { conversationService } from "@/services/conversationService";
@@ -298,7 +299,7 @@ export function ChatInterface() {
298299
const assistantMessageId = uuidv4();
299300
const initialAssistantMessage: ChatMessageType = {
300301
id: assistantMessageId,
301-
role: "assistant",
302+
role: ROLE_ASSISTANT,
302303
content: "",
303304
timestamp: new Date(),
304305
isComplete: false,
@@ -424,7 +425,7 @@ export function ChatInterface() {
424425
...(prev[currentConversationId] || []),
425426
{
426427
id: uuidv4(),
427-
role: "assistant",
428+
role: ROLE_ASSISTANT,
428429
content: "",
429430
timestamp: new Date(),
430431
isComplete: false,
@@ -465,7 +466,7 @@ export function ChatInterface() {
465466
newMessages[currentConversationId]?.[
466467
newMessages[currentConversationId].length - 1
467468
];
468-
if (lastMsg && lastMsg.role === "assistant") {
469+
if (lastMsg && lastMsg.role === ROLE_ASSISTANT) {
469470
if (!lastMsg.steps) lastMsg.steps = [];
470471
// Find the latest preprocessing step
471472
let step = lastMsg.steps.find(
@@ -536,7 +537,7 @@ export function ChatInterface() {
536537
newMessages[currentConversationId]?.[
537538
newMessages[currentConversationId].length - 1
538539
];
539-
if (lastMsg && lastMsg.role === "assistant") {
540+
if (lastMsg && lastMsg.role === ROLE_ASSISTANT) {
540541
lastMsg.error = t("chatInterface.fileProcessingFailed", {
541542
error: result.error,
542543
});
@@ -561,7 +562,7 @@ export function ChatInterface() {
561562
.map((msg) => ({
562563
role: msg.role,
563564
content:
564-
msg.role === "assistant"
565+
msg.role === ROLE_ASSISTANT
565566
? msg.finalAnswer?.trim() || msg.content || ""
566567
: msg.content || "",
567568
})),
@@ -645,7 +646,7 @@ export function ChatInterface() {
645646
newMessages[currentConversationId]?.[
646647
newMessages[currentConversationId].length - 1
647648
];
648-
if (lastMsg && lastMsg.role === "assistant") {
649+
if (lastMsg && lastMsg.role === ROLE_ASSISTANT) {
649650
lastMsg.error = t("chatInterface.requestTimeoutRetry");
650651
lastMsg.isComplete = true;
651652
lastMsg.thinking = undefined;
@@ -737,7 +738,7 @@ export function ChatInterface() {
737738
newMessages[currentConversationId]?.[
738739
newMessages[currentConversationId].length - 1
739740
];
740-
if (lastMsg && lastMsg.role === "assistant") {
741+
if (lastMsg && lastMsg.role === ROLE_ASSISTANT) {
741742
lastMsg.content = t("chatInterface.conversationStopped");
742743
lastMsg.isComplete = true;
743744
lastMsg.thinking = undefined; // Explicitly clear thinking state
@@ -756,7 +757,7 @@ export function ChatInterface() {
756757
newMessages[currentConversationId]?.[
757758
newMessages[currentConversationId].length - 1
758759
];
759-
if (lastMsg && lastMsg.role === "assistant") {
760+
if (lastMsg && lastMsg.role === ROLE_ASSISTANT) {
760761
lastMsg.content = errorMessage;
761762
lastMsg.isComplete = true;
762763
lastMsg.error = errorMessage;
@@ -960,7 +961,7 @@ export function ChatInterface() {
960961
conversationData.create_time
961962
);
962963
formattedMessages.push(formattedUserMsg);
963-
} else if (dialog_msg.role === "assistant") {
964+
} else if (dialog_msg.role === ROLE_ASSISTANT) {
964965
const formattedAssistantMsg: ChatMessageType =
965966
extractAssistantMsgFromResponse(
966967
dialog_msg,
@@ -1103,7 +1104,7 @@ export function ChatInterface() {
11031104
conversationData.create_time
11041105
);
11051106
formattedMessages.push(formattedUserMsg);
1106-
} else if (dialog_msg.role === "assistant") {
1107+
} else if (dialog_msg.role === ROLE_ASSISTANT) {
11071108
const formattedAssistantMsg: ChatMessageType =
11081109
extractAssistantMsgFromResponse(
11091110
dialog_msg,
@@ -1303,7 +1304,7 @@ export function ChatInterface() {
13031304
const lastMsg =
13041305
newMessages[conversationId]?.[newMessages[conversationId].length - 1];
13051306

1306-
if (lastMsg && lastMsg.role === "assistant" && lastMsg.images) {
1307+
if (lastMsg && lastMsg.role === ROLE_ASSISTANT && lastMsg.images) {
13071308
// Filter out failed images
13081309
lastMsg.images = lastMsg.images.filter((url) => url !== imageUrl);
13091310
}
@@ -1356,7 +1357,7 @@ export function ChatInterface() {
13561357
const newMessages = { ...prev };
13571358
const lastMsg =
13581359
newMessages[conversationId]?.[newMessages[conversationId].length - 1];
1359-
if (lastMsg && lastMsg.role === "assistant") {
1360+
if (lastMsg && lastMsg.role === ROLE_ASSISTANT) {
13601361
lastMsg.isComplete = true;
13611362
lastMsg.thinking = undefined; // Explicitly clear thinking state
13621363

@@ -1406,7 +1407,7 @@ export function ChatInterface() {
14061407
const newMessages = { ...prev };
14071408
const lastMsg =
14081409
newMessages[conversationId]?.[newMessages[conversationId].length - 1];
1409-
if (lastMsg && lastMsg.role === "assistant") {
1410+
if (lastMsg && lastMsg.role === ROLE_ASSISTANT) {
14101411
lastMsg.isComplete = true;
14111412
lastMsg.thinking = undefined; // Explicitly clear thinking state
14121413
lastMsg.error = t(

frontend/app/[locale]/chat/internal/extractMsgFromHistoryResponse.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use client";
22

3+
import { ROLE_ASSISTANT } from "@/const/agentConfig";
34
import {
45
ApiMessage,
56
SearchResult,
@@ -237,7 +238,7 @@ export function extractAssistantMsgFromResponse(
237238
// create the formatted assistant message
238239
const formattedAssistantMsg: ChatMessageType = {
239240
id: `assistant-${index}-${Date.now()}`,
240-
role: "assistant",
241+
role: ROLE_ASSISTANT,
241242
message_id: dialog_msg.message_id,
242243
content: "",
243244
opinion_flag: dialog_msg.opinion_flag,

frontend/app/[locale]/chat/streaming/chatStreamFinalMessage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { conversationService } from "@/services/conversationService";
1616
import { copyToClipboard } from "@/lib/clipboard";
1717

1818
import { ChatAttachment, AttachmentItem } from "../internal/chatAttachment";
19+
import { ROLE_ASSISTANT } from "@/const/agentConfig";
1920

2021
interface FinalMessageProps {
2122
message: ChatMessageType;
@@ -259,7 +260,7 @@ export function ChatStreamFinalMessage({
259260
)}
260261

261262
{/* Assistant message part - show final answer or content */}
262-
{message.role === "assistant" &&
263+
{message.role === ROLE_ASSISTANT &&
263264
(message.finalAnswer || message.content !== undefined) && (
264265
<div className="bg-white rounded-lg w-full -mt-2">
265266
<MarkdownRenderer

frontend/app/[locale]/chat/streaming/chatStreamHandler.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Tool function for processing chat streaming response
22

3+
import { ROLE_ASSISTANT } from "@/const/agentConfig";
34
import { ChatMessageType, AgentStep } from "@/types/chat";
45

56
import {
@@ -744,7 +745,7 @@ export const handleStreamResponse = async (
744745
const newMessages = [...prev];
745746
const lastMsg = newMessages[newMessages.length - 1];
746747

747-
if (lastMsg && lastMsg.role === "assistant") {
748+
if (lastMsg && lastMsg.role === ROLE_ASSISTANT) {
748749
// Update the current step
749750
if (currentStep) {
750751
if (!lastMsg.steps) lastMsg.steps = [];
@@ -805,7 +806,7 @@ export const handleStreamResponse = async (
805806
const newMessages = [...prev];
806807
const lastMsg = newMessages[newMessages.length - 1];
807808

808-
if (lastMsg && lastMsg.role === "assistant") {
809+
if (lastMsg && lastMsg.role === ROLE_ASSISTANT) {
809810
lastMsg.isComplete = true;
810811

811812
// Check and remove duplicate steps
@@ -840,7 +841,7 @@ export const handleStreamResponse = async (
840841
const history = newMessages.map((msg) => ({
841842
role: msg.role as "user" | "assistant",
842843
content:
843-
msg.role === "assistant"
844+
msg.role === ROLE_ASSISTANT
844845
? msg.finalAnswer || msg.content || ""
845846
: msg.content || "",
846847
}));

frontend/app/[locale]/chat/streaming/chatStreamMain.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { motion, AnimatePresence } from "framer-motion";
55

66
import { ScrollArea } from "@/components/ui/scrollArea";
77
import { Button } from "@/components/ui/button";
8+
import { ROLE_ASSISTANT } from "@/const/agentConfig";
89
import { ChatMessageType } from "@/types/chat";
910

1011
import { ChatInput, FilePreview } from "../components/chatInput";
@@ -129,7 +130,7 @@ export function ChatStreamMain({
129130
}
130131
}
131132
// Assistant messages need further processing
132-
else if (message.role === "assistant") {
133+
else if (message.role === ROLE_ASSISTANT) {
133134
// If there is a final answer or content (including empty string), add it to the final message array
134135
if (message.finalAnswer || message.content !== undefined) {
135136
finalMsgs.push(message);

frontend/app/[locale]/chat/streaming/chatStreamMessage.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
TooltipProvider,
1818
TooltipTrigger,
1919
} from "@/components/ui/tooltip";
20+
import { ROLE_ASSISTANT } from "@/const/agentConfig";
2021
import { ChatMessageType } from "@/types/chat";
2122
import { useConfig } from "@/hooks/useConfig";
2223
import { copyToClipboard } from "@/lib/clipboard";
@@ -64,7 +65,7 @@ export function ChatStreamMessage({
6465
// When the message is updated, scroll the element into the visible area
6566
useEffect(() => {
6667
if (
67-
message.role === "assistant" &&
68+
message.role === ROLE_ASSISTANT &&
6869
!message.isComplete &&
6970
messageRef.current
7071
) {
@@ -127,7 +128,7 @@ export function ChatStreamMessage({
127128
}`}
128129
>
129130
{/* Avatar section - only show avatar for AI assistant */}
130-
{message.role === "assistant" && (
131+
{message.role === ROLE_ASSISTANT && (
131132
<div className="flex-shrink-0">
132133
<div className="h-8 w-8 rounded-full overflow-hidden bg-primary/10">
133134
<img
@@ -175,7 +176,7 @@ export function ChatStreamMessage({
175176
)}
176177

177178
{/* Assistant message section */}
178-
{message.role === "assistant" && (
179+
{message.role === ROLE_ASSISTANT && (
179180
<>
180181
{/* Attachment section - placed above text */}
181182
{message.attachments && message.attachments.length > 0 && (

frontend/app/[locale]/setup/agentSetup/components/DebugConfig.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ChatMessageType, TaskMessageType } from "@/types/chat";
1010
import { handleStreamResponse } from "@/app/chat/streaming/chatStreamHandler";
1111
import { ChatStreamFinalMessage } from "@/app/chat/streaming/chatStreamFinalMessage";
1212
import { TaskWindow } from "@/app/chat/streaming/taskWindow";
13+
import { ROLE_ASSISTANT } from "@/const/agentConfig";
1314

1415
// Agent debugging component Props interface
1516
interface AgentDebuggingProps {
@@ -58,7 +59,7 @@ function AgentDebugging({
5859
step.contents.forEach((content) => {
5960
taskMsgs.push({
6061
id: content.id,
61-
role: "assistant",
62+
role: ROLE_ASSISTANT,
6263
content: content.content,
6364
timestamp: new Date(),
6465
type: content.type,
@@ -72,7 +73,7 @@ function AgentDebugging({
7273
if (step.thinking && step.thinking.content) {
7374
taskMsgs.push({
7475
id: `thinking-${step.id}`,
75-
role: "assistant",
76+
role: ROLE_ASSISTANT,
7677
content: step.thinking.content,
7778
timestamp: new Date(),
7879
type: "model_output_thinking",
@@ -83,7 +84,7 @@ function AgentDebugging({
8384
if (step.code && step.code.content) {
8485
taskMsgs.push({
8586
id: `code-${step.id}`,
86-
role: "assistant",
87+
role: ROLE_ASSISTANT,
8788
content: step.code.content,
8889
timestamp: new Date(),
8990
type: "model_output_code",
@@ -94,7 +95,7 @@ function AgentDebugging({
9495
if (step.output && step.output.content) {
9596
taskMsgs.push({
9697
id: `output-${step.id}`,
97-
role: "assistant",
98+
role: ROLE_ASSISTANT,
9899
content: step.output.content,
99100
timestamp: new Date(),
100101
type: "tool",
@@ -113,7 +114,7 @@ function AgentDebugging({
113114
{messages.map((message, index) => {
114115
// Process the task content of the current message
115116
const currentTaskMessages =
116-
message.role === "assistant" ? processMessageSteps(message) : [];
117+
message.role === ROLE_ASSISTANT ? processMessageSteps(message) : [];
117118

118119
return (
119120
<div key={message.id || index} className="flex flex-col gap-2">
@@ -132,7 +133,7 @@ function AgentDebugging({
132133
)}
133134

134135
{/* Assistant message task window */}
135-
{message.role === "assistant" &&
136+
{message.role === ROLE_ASSISTANT &&
136137
currentTaskMessages.length > 0 && (
137138
<TaskWindow
138139
messages={currentTaskMessages}
@@ -141,7 +142,7 @@ function AgentDebugging({
141142
)}
142143

143144
{/* Assistant message final answer */}
144-
{message.role === "assistant" && (
145+
{message.role === ROLE_ASSISTANT && (
145146
<ChatStreamFinalMessage
146147
message={message}
147148
onSelectMessage={() => {}}
@@ -257,7 +258,7 @@ export default function DebugConfig({ agentId }: DebugConfigProps) {
257258
setMessages((prev) => {
258259
const newMessages = [...prev];
259260
const lastMsg = newMessages[newMessages.length - 1];
260-
if (lastMsg && lastMsg.role === "assistant") {
261+
if (lastMsg && lastMsg.role === ROLE_ASSISTANT) {
261262
lastMsg.isComplete = true;
262263
lastMsg.thinking = undefined; // Explicitly clear thinking state
263264
lastMsg.content = t("agent.debug.stopped");
@@ -284,7 +285,7 @@ export default function DebugConfig({ agentId }: DebugConfigProps) {
284285
// Add assistant message (initial state)
285286
const assistantMessage: ChatMessageType = {
286287
id: (Date.now() + 1).toString(),
287-
role: "assistant",
288+
role: ROLE_ASSISTANT,
288289
content: "",
289290
timestamp: new Date(),
290291
isComplete: false,
@@ -339,7 +340,7 @@ export default function DebugConfig({ agentId }: DebugConfigProps) {
339340
setMessages((prev) => {
340341
const newMessages = [...prev];
341342
const lastMsg = newMessages[newMessages.length - 1];
342-
if (lastMsg && lastMsg.role === "assistant") {
343+
if (lastMsg && lastMsg.role === ROLE_ASSISTANT) {
343344
lastMsg.content = t("agent.debug.stopped");
344345
lastMsg.isComplete = true;
345346
lastMsg.thinking = undefined; // Explicitly clear thinking state
@@ -356,7 +357,7 @@ export default function DebugConfig({ agentId }: DebugConfigProps) {
356357
setMessages((prev) => {
357358
const newMessages = [...prev];
358359
const lastMsg = newMessages[newMessages.length - 1];
359-
if (lastMsg && lastMsg.role === "assistant") {
360+
if (lastMsg && lastMsg.role === ROLE_ASSISTANT) {
360361
lastMsg.content = errorMessage;
361362
lastMsg.isComplete = true;
362363
lastMsg.error = errorMessage;

0 commit comments

Comments
 (0)