瀏覽代碼

Fix empty message ID in message_start event

The message_start event was returning an empty "id" field because:
1. ClaudeStreamHandler wasn't receiving the conversation_id from app.py
2. The handle_event method was using payload.get('conversationId', ...)
   which would return empty string if the key exists but value is empty

Changes:
1. Pass conversation_id to ClaudeStreamHandler constructor
2. Use `or` chain to ensure we always have a valid ID:
   payload.get('conversationId') or self.conversation_id or uuid.uuid4()

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
jerry-271828 1 月之前
父節點
當前提交
b6239f4996
共有 2 個文件被更改,包括 6 次插入4 次删除
  1. 1 1
      app.py
  2. 5 3
      claude_stream.py

+ 1 - 1
app.py

@@ -654,7 +654,7 @@ async def claude_messages(
                         text_to_count += item.get("text", "")
 
         input_tokens = count_tokens(text_to_count, apply_multiplier=True)
-        handler = ClaudeStreamHandler(model=req.model, input_tokens=input_tokens)
+        handler = ClaudeStreamHandler(model=req.model, input_tokens=input_tokens, conversation_id=conversation_id)
 
         # Try to get the first event to ensure the connection is valid
         # This allows us to return proper HTTP error codes before starting the stream

+ 5 - 3
claude_stream.py

@@ -1,6 +1,7 @@
 import json
 import logging
 import importlib.util
+import uuid
 from pathlib import Path
 from typing import AsyncGenerator, Optional, Dict, Any, List, Set
 import tiktoken
@@ -71,7 +72,7 @@ except Exception as e:
     def build_tool_use_input_delta(*args, **kwargs): return ""
 
 class ClaudeStreamHandler:
-    def __init__(self, model: str, input_tokens: int = 0):
+    def __init__(self, model: str, input_tokens: int = 0, conversation_id: Optional[str] = None):
         self.model = model
         self.input_tokens = input_tokens
         self.response_buffer: List[str] = []
@@ -80,7 +81,7 @@ class ClaudeStreamHandler:
         self.content_block_start_sent: bool = False
         self.content_block_stop_sent: bool = False
         self.message_start_sent: bool = False
-        self.conversation_id: Optional[str] = None
+        self.conversation_id: Optional[str] = conversation_id
 
         # Tool use state
         self.current_tool_use: Optional[Dict[str, Any]] = None
@@ -108,7 +109,8 @@ class ClaudeStreamHandler:
         # 1. Message Start (initial-response)
         if event_type == "initial-response":
             if not self.message_start_sent:
-                conv_id = payload.get('conversationId', self.conversation_id or 'unknown')
+                # Use conversation_id from payload if available, otherwise use the one passed to constructor
+                conv_id = payload.get('conversationId') or self.conversation_id or str(uuid.uuid4())
                 self.conversation_id = conv_id
                 yield build_message_start(conv_id, self.model, self.input_tokens)
                 self.message_start_sent = True