with the run method `const resp = await this.ai.run(model, input);` it is returning a `ReadableStrea

with the run method const resp = await this.ai.run(model, input); it is returning a ReadableStream<Uint8Array> rather than a Uint8Array like the types are saying. I'm having to coerce it into the correct type before being able to manipulate it

here is some working code:

      const resp = (await this.ai.run(model, input)) as unknown;

      let buffer: Uint8Array;

      if (resp instanceof ReadableStream) {
        const chunks: Uint8Array[] = [];
        // eslint-disable-next-line no-restricted-syntax
        for await (const chunk of resp as ReadableStream<Uint8Array>) {
          chunks.push(chunk);
        }
        buffer = new Uint8Array(chunks.reduce((acc, chunk) => acc + chunk.length, 0));
        let offset = 0;
        // eslint-disable-next-line no-restricted-syntax
        for (const chunk of chunks) {
          buffer.set(chunk, offset);
          offset += chunk.length;
        }
      }
      else if (resp instanceof Uint8Array) {
        buffer = resp;
      }
      else {
        throw new Error("Unknown return type for the ai run");
      }

      const base64Image = fromUint8Array(buffer);
Was this page helpful?