Score:0

Using StreamedResponse for AJAX?

in flag

I am looking for a way with a Form API form in Drupal to have an #ajax event on a form field. When submitted, this calls a remote API.

How can I stream the response out to the screen without the request being terminated before completing the stream read?

I thought maybe I could do this in the #ajax callback:

  /**
   * {@inheritdoc}
   */
  public function getResponse(array &$form, FormStateInterface $form_state) {
    $prompt = $form_state->getValue('prompt');
    $model = $form_state->getValue('model');

    $stream = $this->client->completions()->createStreamed(
      [
        'model' => $model,
        'prompt' => trim($prompt),
      ],
    );

    foreach ($stream as $response) {
      $value = $form['response']['#value'];
      $value .= ' ' . trim($response->choices[0]->text) ?? $this->t('No answer was provided.');
      $form['response']['#value'] = $value;
      return $form['response'];
    }
  }

But this won't work. Is there a way to do this within Drupal currently?

cn flag
Could you clarify what "won't work" means exactly? Is the response content unexpected? Error messages/codes? Is your server stack set up to flush buffers correctly per the [Symfony docs](https://symfony.com/doc/current/components/http_foundation.html#streaming-a-response)? Where are you flushing the response? (your sample code is missing context to know where it's run, it will never go past the first iteration)
cn flag
If you're more asking if there's a Drupal-specific pattern/wrapper for this, I think the answer is probably "not yet"
Kevin avatar
in flag
Updated question. Haven't really streamed anything before, so not sure what is missing.
cn flag
Is that the actual code? Including the `foreach` loop? That will never work, because a function can only return once. It'll never get past the first iteration of the loop. I'm not even sure there's really a use case for this - the AJAX response will be JSON, and valid JSON needs the whole document to be parsed. The receiver would have to wait until all parts of the response were streamed before it could make sense of the output
Kevin avatar
in flag
Ok, so it really cannot be done (from a form?). I have tried a handful of ways using StreamedResponse and other examples. Even if called from JS it would hit an internal route to validate user access. I was trying to stream large responses to the client so it didn't take dozens of seconds to see the response.
cn flag
Yeah I don't think partially streaming a structured text document is a valid concept (because it can't be parsed) - it's more useful for file downloads, maybe even socket comms. You _might_ be able to stream chunks of the data as fully structured JSON documents each time, but I'm not sure Drupal's AJAX would play along without some extra work
Alfred Armstrong avatar
cn flag
One way you could handle this would be to put your request/response in an iframe. So your original action would simply inject the iframe into the page. You would need a custom route and controller which would return the streamed response. I haven't tried this but I can't see why it shouldn't work, though there are bound to be a few wrinkles.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.