Class: Cloudflare::AI::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudflare_workers/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.



139
140
141
142
# File 'lib/cloudflare_workers/ai.rb', line 139

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.



137
138
139
# File 'lib/cloudflare_workers/ai.rb', line 137

def js_stream
  @js_stream
end

Instance Method Details

#closeObject



163
# File 'lib/cloudflare_workers/ai.rb', line 163

def close; end

#eachObject



162
# File 'lib/cloudflare_workers/ai.rb', line 162

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



153
154
155
156
157
158
159
160
# File 'lib/cloudflare_workers/ai.rb', line 153

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

#sse_stream?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/cloudflare_workers/ai.rb', line 144

def sse_stream?
  true
end