Open History to see the last commit
1"""
2TanStack AI Stream Chunk Converter
3
4Converts streaming events from various AI providers (Anthropic, OpenAI)
5into TanStack AI StreamChunk format.
6"""
7import uuid
8from typing import List, Dict, Any, Optional
9from datetime import datetime
10
11
12class StreamChunkConverter:
13 """
14 Converts provider-specific streaming events to TanStack AI StreamChunk format.
16 Supports:
17 - Anthropic streaming events
18 - OpenAI streaming events
19 """
21 def __init__(self, model: str, provider: str = "anthropic"):
22 """
23 Initialize converter.
25 Args:
26 model: Model name (e.g., "claude-3-haiku-20240307", "gpt-4o")
27 provider: Provider type ("anthropic" or "openai")
28 """
29 self.model = model
30 self.provider = provider.lower()
31 self.timestamp = int(datetime.now().timestamp() * 1000)
32 self.accumulated_content = ""
33 self.tool_calls_map: Dict[int, Dict[str, Any]] = {}
34 self.current_tool_index = -1
35 self.done_emitted = False
37 def generate_id(self) -> str:
38 """Generate a unique ID for the chunk"""
39 return f"chatcmpl-{uuid.uuid4().hex[:8]}"
41 def _get_event_type(self, event: Any) -> str:
42 """Get event type from either dict or object"""
43 if isinstance(event, dict):
44 return event.get("type", "")
45 return getattr(event, "type", "")
47 def _get_attr(self, obj: Any, attr: str, default: Any = None) -> Any:
48 """Get attribute from either dict or object"""
49 if isinstance(obj, dict):
50 return obj.get(attr, default)
51 return getattr(obj, attr, default)
53 async def convert_anthropic_event(self, event: Any) -> List[Dict[str, Any]]:
54 """Convert Anthropic streaming event to StreamChunk format"""
55 chunks = []
56 event_type = self._get_event_type(event)
58 if event_type == "content_block_start":
59 # Tool call is starting
60 content_block = self._get_attr(event, "content_block")
61 if content_block and self._get_attr(content_block, "type") == "tool_use":
62 self.current_tool_index += 1
63 self.tool_calls_map[self.current_tool_index] = {
64 "id": self._get_attr(content_block, "id"),
65 "name": self._get_attr(content_block, "name"),
66 "input": ""
67 }
69 elif event_type == "content_block_delta":
70 delta = self._get_attr(event, "delta")
72 if delta and self._get_attr(delta, "type") == "text_delta":
73 # Text content delta
74 delta_text = self._get_attr(delta, "text", "")
75 self.accumulated_content += delta_text
77 chunks.append({
78 "type": "content",
79 "id": self.generate_id(),
80 "model": self.model,
81 "timestamp": self.timestamp,
82 "delta": delta_text,
83 "content": self.accumulated_content,
84 "role": "assistant"
85 })
87 elif delta and self._get_attr(delta, "type") == "input_json_delta":
88 # Tool input is being streamed
89 partial_json = self._get_attr(delta, "partial_json", "")
90 tool_call = self.tool_calls_map.get(self.current_tool_index)
92 if tool_call:
93 tool_call["input"] += partial_json
95 chunks.append({
96 "type": "tool_call",
97 "id": self.generate_id(),
98 "model": self.model,
99 "timestamp": self.timestamp,
100 "toolCall": {
101 "id": tool_call["id"],
102 "type": "function",
103 "function": {
104 "name": tool_call["name"],
105 "arguments": partial_json # Incremental JSON
106 }
107 },
108 "index": self.current_tool_index
109 })
111 elif event_type == "message_delta":
112 # Message metadata update (includes stop_reason and usage)
113 delta = self._get_attr(event, "delta")
114 usage = self._get_attr(event, "usage")
116 stop_reason = self._get_attr(delta, "stop_reason") if delta else None
117 if stop_reason:
118 # Map Anthropic stop_reason to TanStack format
119 if stop_reason == "tool_use":
120 finish_reason = "tool_calls"
121 elif stop_reason == "end_turn":
122 finish_reason = "stop"
123 else:
124 finish_reason = stop_reason
126 usage_dict = None
127 if usage:
128 usage_dict = {
129 "promptTokens": self._get_attr(usage, "input_tokens", 0),
130 "completionTokens": self._get_attr(usage, "output_tokens", 0),
131 "totalTokens": self._get_attr(usage, "input_tokens", 0) + self._get_attr(usage, "output_tokens", 0)
132 }
134 self.done_emitted = True
135 chunks.append({
136 "type": "done",
137 "id": self.generate_id(),
138 "model": self.model,
139 "timestamp": self.timestamp,
140 "finishReason": finish_reason,
141 "usage": usage_dict
142 })
144 elif event_type == "message_stop":
145 # Stream completed - this is a fallback if message_delta didn't emit done
146 if not self.done_emitted:
147 self.done_emitted = True
148 chunks.append({
149 "type": "done",
150 "id": self.generate_id(),
151 "model": self.model,
152 "timestamp": self.timestamp,
153 "finishReason": "stop"
154 })
156 return chunks
158 async def convert_openai_event(self, event: Any) -> List[Dict[str, Any]]:
159 """Convert OpenAI streaming event to StreamChunk format"""
160 chunks = []
162 # OpenAI events have chunk.choices[0].delta structure
163 choice = self._get_attr(event, "choices", [])
164 if choice and len(choice) > 0:
165 choice = choice[0]
166 else:
167 # Try direct access
168 choice = event
170 delta = self._get_attr(choice, "delta")
172 # Handle content delta
173 if delta:
174 content = self._get_attr(delta, "content")
175 if content:
176 self.accumulated_content += content
177 chunks.append({
178 "type": "content",
179 "id": self._get_attr(event, "id", self.generate_id()),
180 "model": self._get_attr(event, "model", self.model),
181 "timestamp": self.timestamp,
182 "delta": content,
183 "content": self.accumulated_content,
184 "role": "assistant"
185 })
187 # Handle tool calls
188 tool_calls = self._get_attr(delta, "tool_calls")
189 if tool_calls:
190 for tool_call in tool_calls:
191 chunks.append({
192 "type": "tool_call",
193 "id": self._get_attr(event, "id", self.generate_id()),
194 "model": self._get_attr(event, "model", self.model),
195 "timestamp": self.timestamp,
196 "toolCall": {
197 "id": self._get_attr(tool_call, "id", f"call_{self.timestamp}"),
198 "type": "function",
199 "function": {
200 "name": self._get_attr(self._get_attr(tool_call, "function", {}), "name", ""),
201 "arguments": self._get_attr(self._get_attr(tool_call, "function", {}), "arguments", "")
202 }
203 },
204 "index": self._get_attr(tool_call, "index", 0)
205 })
207 # Handle completion
208 finish_reason = self._get_attr(choice, "finish_reason")
209 if finish_reason:
210 usage = self._get_attr(event, "usage")
211 usage_dict = None
212 if usage:
213 usage_dict = {
214 "promptTokens": self._get_attr(usage, "prompt_tokens", 0),
215 "completionTokens": self._get_attr(usage, "completion_tokens", 0),
216 "totalTokens": self._get_attr(usage, "total_tokens", 0)
217 }
219 self.done_emitted = True
220 chunks.append({
221 "type": "done",
222 "id": self._get_attr(event, "id", self.generate_id()),
223 "model": self._get_attr(event, "model", self.model),
224 "timestamp": self.timestamp,
225 "finishReason": finish_reason,
226 "usage": usage_dict
227 })
229 return chunks
231 async def convert_event(self, event: Any) -> List[Dict[str, Any]]:
232 """
233 Convert provider streaming event to StreamChunk format.
234 Automatically detects provider based on event structure.
235 """
236 if self.provider == "anthropic":
237 return await self.convert_anthropic_event(event)
238 elif self.provider == "openai":
239 return await self.convert_openai_event(event)
240 else:
241 # Try to auto-detect based on event structure
242 event_type = self._get_event_type(event)
244 # Anthropic events have types like "content_block_start", "message_delta"
245 # OpenAI events have chunk.choices structure
246 if event_type in ["content_block_start", "content_block_delta", "message_delta", "message_stop"]:
247 return await self.convert_anthropic_event(event)
248 elif self._get_attr(event, "choices") is not None:
249 return await self.convert_openai_event(event)
250 else:
251 # Default to Anthropic format
252 return await self.convert_anthropic_event(event)
254 async def convert_error(self, error: Exception) -> Dict[str, Any]:
255 """Convert an error to ErrorStreamChunk format"""
256 return {
257 "type": "error",
258 "id": self.generate_id(),
259 "model": self.model,
260 "timestamp": self.timestamp,
261 "error": {
262 "message": str(error),
263 "code": getattr(error, "code", None)
264 }
265 }
266