streaming.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | trait Meow_MWAI_Engines_Trait_Streaming { |
| 4 | protected $streamCallback = null; |
| 5 | protected $streamBuffer = ''; |
| 6 | protected $streamContent = ''; |
| 7 | |
| 8 | /** |
| 9 | * Initialize streaming for a request |
| 10 | */ |
| 11 | protected function init_streaming( $callback = null ) { |
| 12 | $this->streamCallback = $callback; |
| 13 | $this->streamBuffer = ''; |
| 14 | $this->streamContent = ''; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Handle streaming data chunk |
| 19 | */ |
| 20 | protected function handle_stream_chunk( $data ) { |
| 21 | $this->streamBuffer .= $data; |
| 22 | |
| 23 | // Process complete lines |
| 24 | while ( ( $pos = strpos( $this->streamBuffer, "\n" ) ) !== false ) { |
| 25 | $line = substr( $this->streamBuffer, 0, $pos ); |
| 26 | $this->streamBuffer = substr( $this->streamBuffer, $pos + 1 ); |
| 27 | |
| 28 | if ( !empty( $line ) ) { |
| 29 | $this->process_stream_line( $line ); |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Process a single stream line |
| 36 | */ |
| 37 | protected function process_stream_line( $line ) { |
| 38 | // Remove "data: " prefix if present |
| 39 | if ( strpos( $line, 'data: ' ) === 0 ) { |
| 40 | $line = substr( $line, 6 ); |
| 41 | } |
| 42 | |
| 43 | // Handle special cases |
| 44 | if ( $line === '[DONE]' ) { |
| 45 | $this->finalize_stream(); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // Parse JSON data |
| 50 | $data = json_decode( trim( $line ), true ); |
| 51 | if ( $data ) { |
| 52 | $this->handle_stream_data( $data ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Handle parsed stream data |
| 58 | */ |
| 59 | protected function handle_stream_data( $data ) { |
| 60 | // Extract content from different response formats |
| 61 | $content = null; |
| 62 | |
| 63 | // OpenAI Chat Completion format |
| 64 | if ( isset( $data['choices'][0]['delta']['content'] ) ) { |
| 65 | $content = $data['choices'][0]['delta']['content']; |
| 66 | } |
| 67 | // Anthropic format |
| 68 | elseif ( isset( $data['delta']['text'] ) ) { |
| 69 | $content = $data['delta']['text']; |
| 70 | } |
| 71 | // Google format |
| 72 | elseif ( isset( $data['candidates'][0]['content']['parts'][0]['text'] ) ) { |
| 73 | $content = $data['candidates'][0]['content']['parts'][0]['text']; |
| 74 | } |
| 75 | |
| 76 | if ( $content !== null ) { |
| 77 | $this->streamContent .= $content; |
| 78 | |
| 79 | // Call the stream callback if set |
| 80 | if ( $this->streamCallback ) { |
| 81 | call_user_func( $this->streamCallback, $content ); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Finalize the stream |
| 88 | */ |
| 89 | protected function finalize_stream() { |
| 90 | // Process any remaining buffer |
| 91 | if ( !empty( $this->streamBuffer ) ) { |
| 92 | $this->process_stream_line( $this->streamBuffer ); |
| 93 | } |
| 94 | |
| 95 | // Return the complete content |
| 96 | return $this->streamContent; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Build streaming headers |
| 101 | */ |
| 102 | protected function build_stream_headers( $headers = [] ) { |
| 103 | $headers['Accept'] = 'text/event-stream'; |
| 104 | $headers['Cache-Control'] = 'no-cache'; |
| 105 | return $headers; |
| 106 | } |
| 107 | } |
| 108 |