Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

StreamChunkConverter.php

298 lines10.9 KB

1<?php
2​
3namespace TanStack\AI;
4​
5/**
6 * Converts provider-specific streaming events to TanStack AI StreamChunk format.
7 *
8 * Supports:
9 * - Anthropic streaming events
10 * - OpenAI streaming events
11 */
12class StreamChunkConverter
14 private string $model;
15 private string $provider;
16 private int $timestamp;
17 private string $accumulatedContent = '';
18 private array $toolCallsMap = [];
19 private int $currentToolIndex = -1;
20 private bool $doneEmitted = false;
21​
22 public function __construct(string $model, string $provider = 'anthropic')
23 {
24 $this->model = $model;
25 $this->provider = strtolower($provider);
26 $this->timestamp = (int)(microtime(true) * 1000);
27 }
28​
29 /**
30 * Generate a unique ID for the chunk
31 */
32 public function generateId(): string
33 {
34 return 'chatcmpl-' . bin2hex(random_bytes(4));
35 }
36​
37 /**
38 * Get event type from either array or object
39 */
40 private function getEventType(mixed $event): string
41 {
42 if (is_array($event)) {
43 return $event['type'] ?? '';
44 }
45 return is_object($event) && property_exists($event, 'type') ? $event->type : '';
46 }
47​
48 /**
49 * Get attribute from either array or object
50 */
51 private function getAttr(mixed $obj, string $attr, mixed $default = null): mixed
52 {
53 if (is_array($obj)) {
54 return $obj[$attr] ?? $default;
55 }
56 if (is_object($obj)) {
57 return property_exists($obj, $attr) ? $obj->$attr : $default;
58 }
59 return $default;
60 }
61​
62 /**
63 * Convert Anthropic streaming event to StreamChunk format
64 */
65 public function convertAnthropicEvent(mixed $event): array
66 {
67 $chunks = [];
68 $eventType = $this->getEventType($event);
69​
70 if ($eventType === 'content_block_start') {
71 // Tool call is starting
72 $contentBlock = $this->getAttr($event, 'content_block');
73 if ($contentBlock && $this->getAttr($contentBlock, 'type') === 'tool_use') {
74 $this->currentToolIndex++;
75 $this->toolCallsMap[$this->currentToolIndex] = [
76 'id' => $this->getAttr($contentBlock, 'id'),
77 'name' => $this->getAttr($contentBlock, 'name'),
78 'input' => ''
79 ];
80 }
81 } elseif ($eventType === 'content_block_delta') {
82 $delta = $this->getAttr($event, 'delta');
83​
84 if ($delta && $this->getAttr($delta, 'type') === 'text_delta') {
85 // Text content delta
86 $deltaText = $this->getAttr($delta, 'text', '');
87 $this->accumulatedContent .= $deltaText;
88​
89 $chunks[] = [
90 'type' => 'content',
91 'id' => $this->generateId(),
92 'model' => $this->model,
93 'timestamp' => $this->timestamp,
94 'delta' => $deltaText,
95 'content' => $this->accumulatedContent,
96 'role' => 'assistant'
97 ];
98 } elseif ($delta && $this->getAttr($delta, 'type') === 'input_json_delta') {
99 // Tool input is being streamed
100 $partialJson = $this->getAttr($delta, 'partial_json', '');
101 $toolCall = $this->toolCallsMap[$this->currentToolIndex] ?? null;
102​
103 if ($toolCall) {
104 $toolCall['input'] .= $partialJson;
105 $this->toolCallsMap[$this->currentToolIndex] = $toolCall;
106​
107 $chunks[] = [
108 'type' => 'tool_call',
109 'id' => $this->generateId(),
110 'model' => $this->model,
111 'timestamp' => $this->timestamp,
112 'toolCall' => [
113 'id' => $toolCall['id'],
114 'type' => 'function',
115 'function' => [
116 'name' => $toolCall['name'],
117 'arguments' => $partialJson // Incremental JSON
118 ]
119 ],
120 'index' => $this->currentToolIndex
121 ];
122 }
123 }
124 } elseif ($eventType === 'message_delta') {
125 // Message metadata update (includes stop_reason and usage)
126 $delta = $this->getAttr($event, 'delta');
127 $usage = $this->getAttr($event, 'usage');
128​
129 $stopReason = $delta ? $this->getAttr($delta, 'stop_reason') : null;
130 if ($stopReason) {
131 // Map Anthropic stop_reason to TanStack format
132 $finishReason = match ($stopReason) {
133 'tool_use' => 'tool_calls',
134 'end_turn' => 'stop',
135 default => $stopReason
136 };
137​
138 $usageDict = null;
139 if ($usage) {
140 $usageDict = [
141 'promptTokens' => $this->getAttr($usage, 'input_tokens', 0),
142 'completionTokens' => $this->getAttr($usage, 'output_tokens', 0),
143 'totalTokens' => ($this->getAttr($usage, 'input_tokens', 0) + $this->getAttr($usage, 'output_tokens', 0))
144 ];
145 }
146​
147 $this->doneEmitted = true;
148 $chunks[] = [
149 'type' => 'done',
150 'id' => $this->generateId(),
151 'model' => $this->model,
152 'timestamp' => $this->timestamp,
153 'finishReason' => $finishReason,
154 'usage' => $usageDict
155 ];
156 }
157 } elseif ($eventType === 'message_stop') {
158 // Stream completed - this is a fallback if message_delta didn't emit done
159 if (!$this->doneEmitted) {
160 $this->doneEmitted = true;
161 $chunks[] = [
162 'type' => 'done',
163 'id' => $this->generateId(),
164 'model' => $this->model,
165 'timestamp' => $this->timestamp,
166 'finishReason' => 'stop'
167 ];
168 }
169 }
170​
171 return $chunks;
172 }
173​
174 /**
175 * Convert OpenAI streaming event to StreamChunk format
176 */
177 public function convertOpenAIEvent(mixed $event): array
178 {
179 $chunks = [];
180​
181 // OpenAI events have chunk.choices[0].delta structure
182 $choices = $this->getAttr($event, 'choices', []);
183 $choice = !empty($choices) ? $choices[0] : $event;
184​
185 $delta = $this->getAttr($choice, 'delta');
186​
187 // Handle content delta
188 if ($delta) {
189 $content = $this->getAttr($delta, 'content');
190 if ($content !== null) {
191 $this->accumulatedContent .= $content;
192 $chunks[] = [
193 'type' => 'content',
194 'id' => $this->getAttr($event, 'id', $this->generateId()),
195 'model' => $this->getAttr($event, 'model', $this->model),
196 'timestamp' => $this->timestamp,
197 'delta' => $content,
198 'content' => $this->accumulatedContent,
199 'role' => 'assistant'
200 ];
201 }
202​
203 // Handle tool calls
204 $toolCalls = $this->getAttr($delta, 'tool_calls');
205 if ($toolCalls) {
206 foreach ($toolCalls as $index => $toolCall) {
207 $function = $this->getAttr($toolCall, 'function', []);
208 $chunks[] = [
209 'type' => 'tool_call',
210 'id' => $this->getAttr($event, 'id', $this->generateId()),
211 'model' => $this->getAttr($event, 'model', $this->model),
212 'timestamp' => $this->timestamp,
213 'toolCall' => [
214 'id' => $this->getAttr($toolCall, 'id', 'call_' . $this->timestamp),
215 'type' => 'function',
216 'function' => [
217 'name' => $this->getAttr($function, 'name', ''),
218 'arguments' => $this->getAttr($function, 'arguments', '')
219 ]
220 ],
221 'index' => $this->getAttr($toolCall, 'index', $index)
222 ];
223 }
224 }
225 }
226​
227 // Handle completion
228 $finishReason = $this->getAttr($choice, 'finish_reason');
229 if ($finishReason) {
230 $usage = $this->getAttr($event, 'usage');
231 $usageDict = null;
232 if ($usage) {
233 $usageDict = [
234 'promptTokens' => $this->getAttr($usage, 'prompt_tokens', 0),
235 'completionTokens' => $this->getAttr($usage, 'completion_tokens', 0),
236 'totalTokens' => $this->getAttr($usage, 'total_tokens', 0)
237 ];
238 }
239​
240 $this->doneEmitted = true;
241 $chunks[] = [
242 'type' => 'done',
243 'id' => $this->getAttr($event, 'id', $this->generateId()),
244 'model' => $this->getAttr($event, 'model', $this->model),
245 'timestamp' => $this->timestamp,
246 'finishReason' => $finishReason,
247 'usage' => $usageDict
248 ];
249 }
250​
251 return $chunks;
252 }
253​
254 /**
255 * Convert provider streaming event to StreamChunk format.
256 * Automatically detects provider based on event structure.
257 */
258 public function convertEvent(mixed $event): array
259 {
260 if ($this->provider === 'anthropic') {
261 return $this->convertAnthropicEvent($event);
262 } elseif ($this->provider === 'openai') {
263 return $this->convertOpenAIEvent($event);
264 } else {
265 // Try to auto-detect based on event structure
266 $eventType = $this->getEventType($event);
267​
268 // Anthropic events have types like "content_block_start", "message_delta"
269 // OpenAI events have chunk.choices structure
270 if (in_array($eventType, ['content_block_start', 'content_block_delta', 'message_delta', 'message_stop'])) {
271 return $this->convertAnthropicEvent($event);
272 } elseif ($this->getAttr($event, 'choices') !== null) {
273 return $this->convertOpenAIEvent($event);
274 } else {
275 // Default to Anthropic format
276 return $this->convertAnthropicEvent($event);
277 }
278 }
279 }
280​
281 /**
282 * Convert an error to ErrorStreamChunk format
283 */
284 public function convertError(\Throwable $error): array
285 {
286 return [
287 'type' => 'error',
288 'id' => $this->generateId(),
289 'model' => $this->model,
290 'timestamp' => $this->timestamp,
291 'error' => [
292 'message' => $error->getMessage(),
293 'code' => $error->getCode()
294 ]
295 ];
296 }
298​