@@ -126,7 +126,7 @@ protected function success($data): void {
126
126
127
127
protected function proxyAIResponse (AIResponse $ aiResponse )
128
128
{
129
- $ headerStream = (int )$ aiResponse ->getStream ();
129
+ $ headerStream = (int )$ aiResponse ->isStream ();
130
130
header ("x-modai-service: {$ aiResponse ->getService ()}" );
131
131
header ("x-modai-parser: {$ aiResponse ->getParser ()}" );
132
132
header ("x-modai-stream: $ headerStream " );
@@ -137,7 +137,7 @@ protected function proxyAIResponse(AIResponse $aiResponse)
137
137
$ this ->success ([
138
138
'forExecutor ' => [
139
139
'service ' => $ aiResponse ->getService (),
140
- 'stream ' => $ aiResponse ->getStream (),
140
+ 'stream ' => $ aiResponse ->isStream (),
141
141
'parser ' => $ aiResponse ->getParser (),
142
142
'url ' => $ aiResponse ->getUrl (),
143
143
'headers ' => $ aiResponse ->getHeaders (),
@@ -155,24 +155,53 @@ protected function proxyAIResponse(AIResponse $aiResponse)
155
155
}
156
156
157
157
$ ch = curl_init ($ aiResponse ->getUrl ());
158
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , !$ aiResponse ->getStream ());
158
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , !$ aiResponse ->isStream ());
159
159
curl_setopt ($ ch , CURLOPT_POST , true );
160
160
curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ aiResponse ->getBody ());
161
161
curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
162
+ curl_setopt ($ ch , CURLOPT_HEADER , false );
163
+
164
+ $ statusCode = 200 ;
165
+ $ headersSent = false ;
166
+ $ bodyBuffer = '' ;
167
+
168
+
169
+ if ($ aiResponse ->isStream ()) {
170
+ curl_setopt ($ ch , CURLOPT_HEADERFUNCTION , function ($ curl , $ header_line ) use (&$ statusCode ) {
171
+ if (strpos ($ header_line , 'HTTP/ ' ) === 0 ) {
172
+ preg_match ('#HTTP/\S+ (\d+)# ' , $ header_line , $ matches );
173
+ if (isset ($ matches [1 ])) {
174
+ $ statusCode = (int )$ matches [1 ];
175
+ }
176
+ }
177
+ return strlen ($ header_line );
178
+ });
162
179
163
- if ($ aiResponse ->getStream ()) {
164
180
header ('Content-Type: text/event-stream ' );
165
181
header ('Connection: keep-alive ' );
166
182
header ('Cache-Control: no-cache ' );
167
183
flush ();
168
184
ob_flush ();
169
185
170
- curl_setopt ($ ch , CURLOPT_WRITEFUNCTION , function ($ curl , $ chunk ) {
171
- echo $ chunk ;
172
- flush ();
173
- ob_flush ();
174
-
175
- return strlen ($ chunk );
186
+ curl_setopt ($ ch , CURLOPT_WRITEFUNCTION , function ($ curl , $ chunk ) use (&$ statusCode , &$ headersSent , &$ bodyBuffer ) {
187
+ if ($ statusCode >= 400 ) {
188
+ $ bodyBuffer .= $ chunk ;
189
+ return strlen ($ chunk );
190
+ } else {
191
+ if (!$ headersSent ) {
192
+ header ('Content-Type: text/event-stream ' );
193
+ header ('Connection: keep-alive ' );
194
+ header ('Cache-Control: no-cache ' );
195
+ flush ();
196
+ ob_flush ();
197
+ $ headersSent = true ;
198
+ }
199
+
200
+ echo $ chunk ;
201
+ flush ();
202
+ ob_flush ();
203
+ return strlen ($ chunk );
204
+ }
176
205
});
177
206
}
178
207
@@ -183,10 +212,20 @@ protected function proxyAIResponse(AIResponse $aiResponse)
183
212
throw new \Exception ($ error_msg );
184
213
}
185
214
186
- $ httpCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
187
- http_response_code ($ httpCode );
215
+ if (!$ aiResponse ->isStream ()) {
216
+ $ statusCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
217
+ $ bodyBuffer = $ response ;
218
+ }
219
+
220
+ http_response_code ($ statusCode );
221
+
222
+ if ($ statusCode >= 400 ) {
223
+ header ('Content-Type: application/json ' );
224
+ echo $ bodyBuffer ;
225
+ return ;
226
+ }
188
227
189
- if (!$ aiResponse ->getStream ()) {
228
+ if (!$ aiResponse ->isStream ()) {
190
229
header ("Content-Type: application/json " );
191
230
echo $ response ;
192
231
return ;
0 commit comments