@@ -3,6 +3,9 @@ import { generateMessageId } from '../mcp/client'
33import type { AnnotatedFile } from '@/lib/utils/code-interpreter'
44import { stopStreamProcessing } from '@/lib/utils/streaming'
55import { getTimestamp } from '@/lib/utils/date'
6+ import { createLogger } from '@/lib/logger'
7+
8+ const log = createLogger ( 'use-streaming-chat' )
69
710export type AssistantStreamEvent = {
811 type : 'assistant'
@@ -249,7 +252,7 @@ export function useStreamingChat(): UseStreamingChatReturn {
249252 } , [ ] )
250253
251254 const handleError = useCallback ( ( error : Error ) => {
252- console . error ( 'Chat error:' , error )
255+ log . error ( { err : error } , 'Chat error' )
253256 setStreamBuffer ( ( prev : Array < StreamEvent > ) => [
254257 ...prev ,
255258 {
@@ -268,10 +271,13 @@ export function useStreamingChat(): UseStreamingChatReturn {
268271 setRequestId ( xRequestId )
269272
270273 if ( ! response . ok ) {
271- console . error (
272- 'Chat response error:' ,
273- response . status ,
274- response . statusText ,
274+ log . error (
275+ {
276+ requestId : xRequestId ,
277+ status : response . status ,
278+ statusText : response . statusText ,
279+ } ,
280+ 'Chat response error' ,
275281 )
276282 setStreamBuffer ( ( prev : Array < StreamEvent > ) => [
277283 ...prev ,
@@ -333,7 +339,7 @@ export function useStreamingChat(): UseStreamingChatReturn {
333339 } ,
334340 ] )
335341 } catch ( e ) {
336- console . error ( 'Failed to parse error data:' , e )
342+ log . error ( { err : e } , 'Failed to parse error data' )
337343 setStreamBuffer ( ( prev : Array < StreamEvent > ) => [
338344 ...prev ,
339345 {
@@ -349,13 +355,13 @@ export function useStreamingChat(): UseStreamingChatReturn {
349355 try {
350356 const toolStateStr = line . slice ( 2 )
351357 if ( ! toolStateStr || ! toolStateStr . trim ( ) ) {
352- console . warn ( 'Empty tool state string' )
358+ log . warn ( 'Empty tool state string' )
353359 return
354360 }
355361
356362 const toolState = JSON . parse ( toolStateStr )
357363 if ( ! toolState || typeof toolState !== 'object' ) {
358- console . warn ( 'Invalid tool state object' )
364+ log . warn ( 'Invalid tool state object' )
359365 return
360366 }
361367 if ( toolState . type === 'tool_call_completed' ) {
@@ -597,10 +603,13 @@ export function useStreamingChat(): UseStreamingChatReturn {
597603 ? JSON . parse ( toolState . delta )
598604 : { }
599605 } catch ( e ) {
600- console . error ( 'Failed to parse delta:' , {
601- delta : toolState . delta ,
602- e,
603- } )
606+ log . error (
607+ {
608+ delta : toolState . delta ,
609+ err : e ,
610+ } ,
611+ 'Failed to parse delta' ,
612+ )
604613 toolState . delta = { }
605614 }
606615 }
@@ -611,10 +620,13 @@ export function useStreamingChat(): UseStreamingChatReturn {
611620 ? JSON . parse ( toolState . arguments )
612621 : { }
613622 } catch ( e ) {
614- console . error ( 'Failed to parse arguments:' , {
615- arguments : toolState . arguments ,
616- e,
617- } )
623+ log . error (
624+ {
625+ arguments : toolState . arguments ,
626+ err : e ,
627+ } ,
628+ 'Failed to parse arguments' ,
629+ )
618630 toolState . arguments = { }
619631 }
620632
@@ -668,7 +680,7 @@ export function useStreamingChat(): UseStreamingChatReturn {
668680 return [ ...prev , toolEvent ]
669681 } )
670682 } catch ( e ) {
671- console . error ( 'Failed to parse tool state:' , e )
683+ log . error ( { err : e } , 'Failed to parse tool state' )
672684 }
673685 }
674686
@@ -699,13 +711,13 @@ export function useStreamingChat(): UseStreamingChatReturn {
699711 try {
700712 const textChunk = line . slice ( 2 )
701713 if ( ! textChunk || ! textChunk . trim ( ) ) {
702- console . warn ( 'Empty text chunk' )
714+ log . warn ( 'Empty text chunk' )
703715 return
704716 }
705717
706718 const text = JSON . parse ( textChunk )
707719 if ( typeof text !== 'string' ) {
708- console . warn ( 'Text chunk is not a string:' , text )
720+ log . warn ( { text } , 'Text chunk is not a string' )
709721 return
710722 }
711723
@@ -714,7 +726,7 @@ export function useStreamingChat(): UseStreamingChatReturn {
714726 }
715727 updateAssistantText ( text , assistantId )
716728 } catch ( e ) {
717- console . error ( 'Failed to parse text chunk:' , e )
729+ log . error ( { err : e } , 'Failed to parse text chunk' )
718730 }
719731 }
720732 }
@@ -758,7 +770,7 @@ export function useStreamingChat(): UseStreamingChatReturn {
758770 return
759771 }
760772
761- console . error ( 'Error reading stream chunk:' , error )
773+ log . error ( { err : error } , 'Error reading stream chunk' )
762774 setStreamBuffer ( ( prev : Array < StreamEvent > ) => [
763775 ...prev ,
764776 {
@@ -780,13 +792,13 @@ export function useStreamingChat(): UseStreamingChatReturn {
780792
781793 const addUserMessage = useCallback ( ( content : string ) => {
782794 if ( ! content || typeof content !== 'string' ) {
783- console . warn ( 'addUserMessage: Invalid content provided' , content )
795+ log . warn ( { content } , 'addUserMessage: Invalid content provided' )
784796 return
785797 }
786798
787799 const trimmedContent = content . trim ( )
788800 if ( ! trimmedContent ) {
789- console . warn ( 'addUserMessage: Empty content after trimming' )
801+ log . warn ( 'addUserMessage: Empty content after trimming' )
790802 return
791803 }
792804
0 commit comments