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.



459
460
461
462
# File 'lib/homura/runtime/ai.rb', line 459

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.



457
458
459
# File 'lib/homura/runtime/ai.rb', line 457

def js_stream
  @js_stream
end

Instance Method Details

#closeObject



491
492
# File 'lib/homura/runtime/ai.rb', line 491

def close
end

#eachObject



488
489
# File 'lib/homura/runtime/ai.rb', line 488

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).



473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/homura/runtime/ai.rb', line 473

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)


464
465
466
# File 'lib/homura/runtime/ai.rb', line 464

def sse_stream?
  true
end