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,28 @@ 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 any ongoing debug run
210+ if ( abortControllerRef . current ) {
211+ abortControllerRef . current . abort ( ) ;
212+ abortControllerRef . current = null ;
213+ }
214+ // Reset streaming state
215+ setIsStreaming ( false ) ;
216+ // Clear timeout timer
217+ if ( timeoutRef . current ) {
218+ clearTimeout ( timeoutRef . current ) ;
219+ timeoutRef . current = null ;
220+ }
221+ } , [ agentId ] ) ;
203222
204223 // Reset timeout timer
205224 const resetTimeout = ( ) => {
@@ -306,7 +325,7 @@ export default function DebugConfig({
306325 reader ,
307326 setMessages ,
308327 resetTimeout ,
309- stepIdCounter ,
328+ stepIdCounter . current ,
310329 ( ) => { } , // setIsSwitchedConversation - Debug mode does not need
311330 false , // isNewConversation - Debug mode does not need
312331 ( ) => { } , // setConversationTitle - Debug mode does not need
@@ -359,7 +378,8 @@ export default function DebugConfig({
359378
360379 return (
361380 < div className = "w-full h-full bg-white" >
362- < AgentDebugging
381+ < AgentDebugging
382+ key = { agentId } // Re-render when agentId changes to ensure state resets
363383 onAskQuestion = { handleTestQuestion }
364384 onStop = { handleStop }
365385 isStreaming = { isStreaming }
0 commit comments