Class: Cloudflare::AI::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/homura/runtime/ai.rb

Overview

Streaming wrapper. Holds the raw JS ReadableStream<Uint8Array> returned by env.AI.run when ‘stream: true` is set. Sinatra routes return this from a route body and `build_js_response` recognises it via duck-typing (`#sse_stream?`) to pass the stream straight into `new Response(stream, …)`.

Phase 11A: unified with Cloudflare::SSEStream so both stream types go through the same ‘response_headers` path in `build_js_response`. The AI::Stream wraps a pre-existing JS ReadableStream (produced by env.AI.run), whereas SSEStream produces its own. The adapter doesn’t need to care — it just calls ‘#js_stream` and `#response_headers`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(js_stream, headers: nil) ⇒ Stream

Returns a new instance of Stream.



445
446
447
448
# File 'lib/homura/runtime/ai.rb', line 445

def initialize(js_stream, headers: nil)
  @js_stream = js_stream
  @extra_headers = headers || {}
end

Instance Attribute Details

#js_streamObject (readonly)

Returns the value of attribute js_stream.



443
444
445
# File 'lib/homura/runtime/ai.rb', line 443

def js_stream
  @js_stream
end

Instance Method Details

#closeObject



477
478
# File 'lib/homura/runtime/ai.rb', line 477

def close
end

#eachObject



475
476
# File 'lib/homura/runtime/ai.rb', line 475

def each
end

#response_headersObject

Merged SSE headers — same shape as SSEStream#response_headers, so build_js_response can pass the stream through without a special AI branch. Reference Cloudflare::SSEStream lazily so this file still loads if stream.rb hasn’t been required yet (Phase 11A load-order flip: ai.rb currently loads first).



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/homura/runtime/ai.rb', line 459

def response_headers
  defaults =
    (
      if defined?(::Cloudflare::SSEStream)
        ::Cloudflare::SSEStream::DEFAULT_HEADERS
      else
        {
          "content-type" => "text/event-stream; charset=utf-8",
          "cache-control" => "no-cache, no-transform",
          "x-accel-buffering" => "no"
        }
      end
    )
  defaults.merge(@extra_headers)
end

#sse_stream?Boolean

Returns:

  • (Boolean)


450
451
452
# File 'lib/homura/runtime/ai.rb', line 450

def sse_stream?
  true
end