11"use client"
22
3- import { useState , useRef } from 'react'
3+ import { useState , useRef , useEffect } from 'react'
44import { Input } from 'antd'
55import { useTranslation } from 'react-i18next'
66import { conversationService } from '@/services/conversationService'
@@ -23,9 +23,6 @@ interface DebugConfigProps {
2323 agentId ?: number ; // Make agentId an optional prop
2424}
2525
26- // Counter for generating unique IDs
27- const stepIdCounter = { current : 0 } ;
28-
2926/**
3027 * Agent debugging component
3128 */
@@ -200,6 +197,21 @@ export default function DebugConfig({
200197 const [ isStreaming , setIsStreaming ] = useState ( false ) ;
201198 const timeoutRef = useRef < NodeJS . Timeout | null > ( null ) ;
202199 const abortControllerRef = useRef < AbortController | null > ( null ) ;
200+ // Maintain an independent step ID counter per Agent
201+ const stepIdCounter = useRef < { current : number } > ( { current : 0 } ) ;
202+
203+ // Reset debug state when agentId changes
204+ useEffect ( ( ) => {
205+ // Clear debug history
206+ setMessages ( [ ] ) ;
207+ // Reset step ID counter
208+ stepIdCounter . current . current = 0 ;
209+ // Stop both frontend and backend when switching agent (debug mode)
210+ const hasActiveStream = isStreaming || abortControllerRef . current !== null ;
211+ if ( hasActiveStream ) {
212+ handleStop ( ) ;
213+ }
214+ } , [ agentId ] ) ;
203215
204216 // Reset timeout timer
205217 const resetTimeout = ( ) => {
@@ -306,7 +318,7 @@ export default function DebugConfig({
306318 reader ,
307319 setMessages ,
308320 resetTimeout ,
309- stepIdCounter ,
321+ stepIdCounter . current ,
310322 ( ) => { } , // setIsSwitchedConversation - Debug mode does not need
311323 false , // isNewConversation - Debug mode does not need
312324 ( ) => { } , // setConversationTitle - Debug mode does not need
@@ -359,7 +371,8 @@ export default function DebugConfig({
359371
360372 return (
361373 < div className = "w-full h-full bg-white" >
362- < AgentDebugging
374+ < AgentDebugging
375+ key = { agentId } // Re-render when agentId changes to ensure state resets
363376 onAskQuestion = { handleTestQuestion }
364377 onStop = { handleStop }
365378 isStreaming = { isStreaming }
0 commit comments