@@ -276,7 +276,7 @@ func handleStreamingMessages(c *fiber.Ctx, openaiReq *models.OpenAIRequest, cfg
276276 return nil
277277}
278278
279- // ToolCallState tracks the state of a tool call during streaming (matches Python current_tool_calls)
279+ // ToolCallState tracks the state of a tool call during streaming
280280type ToolCallState struct {
281281 ID string // Tool call ID from OpenAI
282282 Name string // Function name
@@ -307,13 +307,13 @@ func streamOpenAIToClaude(w *bufio.Writer, reader io.Reader, providerModel strin
307307 scanner := bufio .NewScanner (reader )
308308 scanner .Buffer (make ([]byte , 64 * 1024 ), 1024 * 1024 ) // Increase buffer size
309309
310- // State variables (matches Python implementation)
310+ // State variables
311311 messageID := fmt .Sprintf ("msg_%d" , time .Now ().UnixNano ())
312312 textBlockIndex := 1 // Text block is index 1 (thinking is 0)
313313 toolBlockCounter := 2 // Tool calls start at index 2
314- currentToolCalls := make (map [int ]* ToolCallState ) // Python: current_tool_calls = {}
315- finalStopReason := "end_turn" // Python: final_stop_reason = "end_turn"
316- usageData := map [string ]interface {}{ // Python: usage_data = {...}
314+ currentToolCalls := make (map [int ]* ToolCallState )
315+ finalStopReason := "end_turn"
316+ usageData := map [string ]interface {}{
317317 "input_tokens" : 0 ,
318318 "output_tokens" : 0 ,
319319 "cache_creation_input_tokens" : 0 ,
@@ -330,7 +330,7 @@ func streamOpenAIToClaude(w *bufio.Writer, reader io.Reader, providerModel strin
330330 thinkingBlockHasContent := false
331331 textBlockStarted := false // Track if we've sent text block_start
332332
333- // Send initial SSE events (matches Python lines 96-101)
333+ // Send initial SSE events
334334 writeSSEEvent (w , "message_start" , map [string ]interface {}{
335335 "type" : "message_start" ,
336336 "message" : map [string ]interface {}{
@@ -360,7 +360,7 @@ func streamOpenAIToClaude(w *bufio.Writer, reader io.Reader, providerModel strin
360360
361361 _ = w .Flush ()
362362
363- // Process streaming chunks (matches Python lines 111-210)
363+ // Process streaming chunks
364364 for scanner .Scan () {
365365 line := scanner .Text ()
366366
@@ -391,7 +391,7 @@ func streamOpenAIToClaude(w *bufio.Writer, reader io.Reader, providerModel strin
391391 fmt .Printf ("[DEBUG] Raw chunk from OpenRouter: %s\n " , dataJSON )
392392 }
393393
394- // Handle usage data (matches Python lines 120-131)
394+ // Handle usage data
395395 if usage , ok := chunk ["usage" ].(map [string ]interface {}); ok {
396396 if cfg .Debug {
397397 usageJSON , _ := json .Marshal (usage )
@@ -553,7 +553,7 @@ func streamOpenAIToClaude(w *bufio.Writer, reader io.Reader, providerModel strin
553553 _ = w .Flush ()
554554 }
555555
556- // Handle text delta (matches Python lines 146-147)
556+ // Handle text delta
557557 if content , ok := delta ["content" ].(string ); ok && content != "" {
558558 // Send content_block_start for text block on first text delta
559559 if ! textBlockStarted {
@@ -580,7 +580,7 @@ func streamOpenAIToClaude(w *bufio.Writer, reader io.Reader, providerModel strin
580580 _ = w .Flush ()
581581 }
582582
583- // Handle tool call deltas (matches Python lines 149-198)
583+ // Handle tool call deltas
584584 if toolCallsRaw , ok := delta ["tool_calls" ]; ok {
585585 // Debug: Log raw tool_calls from provider
586586 if cfg .Debug {
@@ -596,13 +596,13 @@ func streamOpenAIToClaude(w *bufio.Writer, reader io.Reader, providerModel strin
596596 continue
597597 }
598598
599- // Get tool call index (matches Python line 152)
599+ // Get tool call index
600600 tcIndex := 0
601601 if idx , ok := tcDelta ["index" ].(float64 ); ok {
602602 tcIndex = int (idx )
603603 }
604604
605- // Initialize tool call tracking if not exists (matches Python lines 155-163)
605+ // Initialize tool call tracking if not exists
606606 if _ , exists := currentToolCalls [tcIndex ]; ! exists {
607607 currentToolCalls [tcIndex ] = & ToolCallState {
608608 ID : "" ,
@@ -616,18 +616,18 @@ func streamOpenAIToClaude(w *bufio.Writer, reader io.Reader, providerModel strin
616616
617617 toolCall := currentToolCalls [tcIndex ]
618618
619- // Update tool call ID if provided (matches Python lines 168-169)
619+ // Update tool call ID if provided
620620 if id , ok := tcDelta ["id" ].(string ); ok {
621621 toolCall .ID = id
622622 }
623623
624- // Update function name (matches Python lines 172-174)
624+ // Update function name
625625 if functionData , ok := tcDelta ["function" ].(map [string ]interface {}); ok {
626626 if name , ok := functionData ["name" ].(string ); ok {
627627 toolCall .Name = name
628628 }
629629
630- // Start content block when we have complete initial data (matches Python lines 177-183)
630+ // Start content block when we have complete initial data
631631 if toolCall .ID != "" && toolCall .Name != "" && ! toolCall .Started {
632632 toolBlockCounter ++
633633 claudeIndex := textBlockIndex + toolBlockCounter
@@ -647,16 +647,15 @@ func streamOpenAIToClaude(w *bufio.Writer, reader io.Reader, providerModel strin
647647 _ = w .Flush ()
648648 }
649649
650- // Handle function arguments (matches Python lines 186-198)
651- // Python checks: "arguments" in function_data and tool_call["started"] and function_data["arguments"] is not None
652- // Go equivalent: type assertion handles nil check, Started flag, and we process even empty strings
650+ // Handle function arguments
651+ // Type assertion handles nil check, Started flag, and we process even empty strings
653652 if args , ok := functionData ["arguments" ].(string ); ok && toolCall .Started {
654653 // Only accumulate if args is not empty
655654 if args != "" {
656655 toolCall .ArgsBuffer += args
657656 }
658657
659- // Try to parse complete JSON and send delta when we have valid JSON (matches Python 190-195)
658+ // Try to parse complete JSON and send delta when we have valid JSON
660659 if toolCall .ArgsBuffer != "" {
661660 var jsonTest interface {}
662661 if err := json .Unmarshal ([]byte (toolCall .ArgsBuffer ), & jsonTest ); err == nil {
@@ -682,7 +681,7 @@ func streamOpenAIToClaude(w *bufio.Writer, reader io.Reader, providerModel strin
682681 }
683682 }
684683
685- // Handle finish reason (matches Python lines 200-210)
684+ // Handle finish reason
686685 // NOTE: Don't break here - with stream_options.include_usage, OpenAI sends usage in a chunk AFTER finish_reason
687686 if finishReason , ok := choice ["finish_reason" ].(string ); ok && finishReason != "" {
688687 switch finishReason {
@@ -699,9 +698,9 @@ func streamOpenAIToClaude(w *bufio.Writer, reader io.Reader, providerModel strin
699698 }
700699 }
701700
702- // Send final SSE events (matches Python lines 225-234)
701+ // Send final SSE events
703702
704- // Send content_block_stop for text block if it was started (matches Python line 226)
703+ // Send content_block_stop for text block if it was started
705704 if textBlockStarted {
706705 writeSSEEvent (w , "content_block_stop" , map [string ]interface {}{
707706 "type" : "content_block_stop" ,
@@ -710,9 +709,9 @@ func streamOpenAIToClaude(w *bufio.Writer, reader io.Reader, providerModel strin
710709 _ = w .Flush ()
711710 }
712711
713- // Send content_block_stop for each tool call (matches Python lines 228-230)
712+ // Send content_block_stop for each tool call
714713 for _ , toolData := range currentToolCalls {
715- // Python checks both Started AND claude_index is not None (line 229)
714+ // Check both Started AND claude_index is not None
716715 if toolData .Started && toolData .ClaudeIndex != 0 {
717716 writeSSEEvent (w , "content_block_stop" , map [string ]interface {}{
718717 "type" : "content_block_stop" ,
@@ -741,8 +740,7 @@ func streamOpenAIToClaude(w *bufio.Writer, reader io.Reader, providerModel strin
741740 }
742741
743742 // Send message_delta with stop_reason and accumulated usage data
744- // NOTE: Unlike Python (which resets to zeros on line 232), we send the actual accumulated usage
745- // This fixes the "0 tokens" issue in Claude Code
743+ // NOTE: We send the actual accumulated usage to fix the "0 tokens" issue in Claude Code
746744 if cfg .Debug {
747745 usageDataJSON , _ := json .Marshal (usageData )
748746 fmt .Printf ("[DEBUG] Sending message_delta with usageData: %s\n " , string (usageDataJSON ))
@@ -751,13 +749,13 @@ func streamOpenAIToClaude(w *bufio.Writer, reader io.Reader, providerModel strin
751749 "type" : "message_delta" ,
752750 "delta" : map [string ]interface {}{
753751 "stop_reason" : finalStopReason ,
754- "stop_sequence" : nil , // Python includes this (line 233)
752+ "stop_sequence" : nil ,
755753 },
756754 "usage" : usageData ,
757755 })
758756 _ = w .Flush ()
759757
760- // Send message_stop (matches Python line 234)
758+ // Send message_stop
761759 writeSSEEvent (w , "message_stop" , map [string ]interface {}{
762760 "type" : "message_stop" ,
763761 })
0 commit comments