Issue #3478167: Add a global JSON decoder for both streaming and none streaming responses
3 open threads
I removed the implementation from it in the submodules, since that would just add a lot of files that are less important for the code review.
Merge request reports
Activity
requested review from @scotteuser
removed review request for @scotteuser
requested review from @scotteuser
50 * 51 * @return array|\Drupal\ai\OperationType\Chat\StreamedChatMessageIteratorInterface 52 * The decoded JSON payload or the message back. 53 */ 54 public function decodeStreamingMessage(StreamedChatMessageIteratorInterface $payload, int $chunks_to_test): array|StreamedChatMessageIteratorInterface { 55 // First we test if it could be json. 56 $i = 0; 57 // We store what we have tested. 58 $full = ""; 59 $could_be_json = FALSE; 60 foreach ($payload as $value) { 61 if ($value->getText() && $i <= $chunks_to_test) { 62 $full .= $value->getText(); 63 // Test against the different start combinations. 64 foreach ($this->jsonStart as $start) { 65 if (strpos($full, $start) !== FALSE) { 95 * Decode the JSON payload into an array. 96 * 97 * @param string $payload 98 * The JSON payload to decode. 99 * 100 * @return array|null 101 * The decoded JSON payload or NULL if the payload is not valid JSON. 102 */ 103 public function decodePayload(string $payload): ?array { 104 // Unescape the payload if its escaped. 105 $payload = stripcslashes($payload); 106 // Regex to find most patterns. 107 $pattern = '/\{(?:[^{}]|(?R))*\}|\[(?:[^\[\]]|(?R))*\]/'; 108 preg_match_all($pattern, $payload, $matches); 109 foreach ($matches[0] as $possible_json) { 110 $data = Json::decode(is_array($possible_json) ? $possible_json[0] : $possible_json);
Please register or sign in to reply