Skip to content
Snippets Groups Projects

Issue #3478167: Add a global JSON decoder for both streaming and none streaming responses

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

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
1 <?php
  • Marcus Johansson added 2 commits

    added 2 commits

    • 67f34da9 - Issue #3478167: Add a global JSON decoder for both streaming and none streaming responses
    • 9267b086 - Issue #3478167: Add a global JSON decoder for both streaming and none streaming responses

    Compare with previous version

  • added 1 commit

    • a9c7f4b9 - Issue #3478167: Add a global JSON decoder for both streaming and none streaming responses

    Compare with previous version

  • Marcus Johansson marked this merge request as draft

    marked this merge request as draft

  • Marcus Johansson removed review request for @scotteuser

    removed review request for @scotteuser

  • added 1 commit

    • fde2c874 - Issue #3478167: Add a global JSON decoder for both streaming and none streaming responses

    Compare with previous version

  • added 1 commit

    • 835f34fe - Issue #3478167: Add a global JSON decoder for both streaming and none streaming responses

    Compare with previous version

  • added 1 commit

    • 62012152 - Issue #3478167: Add a global JSON decoder for both streaming and none streaming responses

    Compare with previous version

  • Marcus Johansson marked this merge request as ready

    marked this merge request as ready

  • 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
    Loading