Module: Cloudflare::AI

Defined in:
lib/homura/runtime/ai.rb

Defined Under Namespace

Classes: Binding, Stream

Constant Summary collapse

DEFAULT_OPTIONS =

Default REST options forwarded to env.AI.run as the third argument.

{}.freeze
DEFAULT_CHAT_MODEL =
"@cf/moonshotai/kimi-k2.6"
DEFAULT_TRANSCRIBE_MODEL =
"@cf/openai/whisper"
DEFAULT_SPEAK_MODEL =
"@cf/deepgram/aura-1"

Class Method Summary collapse

Class Method Details

.audio_input(audio) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/homura/runtime/ai.rb', line 319

def self.audio_input(audio)
  if audio.respond_to?(:to_uint8_array)
    uint8 = audio.to_uint8_array
    return `Array.from(#{uint8})`
  end

  return audio if audio.is_a?(Array)
  if `typeof #{audio} !== 'undefined' && #{audio} instanceof Uint8Array`
    return `Array.from(#{audio})`
  end

  audio
end

.build_messages(prompt = nil, messages: nil, system: nil) ⇒ Object

Raises:

  • (ArgumentError)


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/homura/runtime/ai.rb', line 303

def self.build_messages(prompt = nil, messages: nil, system: nil)
  out = []
  out << {role: "system", content: system.to_s} if system
  if messages
    unless messages.is_a?(Array)
      raise ArgumentError, "messages must be an Array of chat messages"
    end

    out.concat(messages)
  end

  out << {role: "user", content: prompt.to_s} unless prompt.nil?
  raise ArgumentError, "chat requires a prompt or messages" if out.empty?
  out
end

.chat_input_options(model, input_options) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/homura/runtime/ai.rb', line 333

def self.chat_input_options(model, input_options)
  return input_options unless model.to_s == DEFAULT_CHAT_MODEL

  options = input_options.dup
  key = if options.key?(:chat_template_kwargs)
    :chat_template_kwargs
  elsif options.key?("chat_template_kwargs")
    "chat_template_kwargs"
  end

  template_kwargs = key ? options[key] : nil
  if template_kwargs && !template_kwargs.is_a?(Hash)
    raise ArgumentError, "chat_template_kwargs must be a Hash"
  end

  merged = (template_kwargs || {}).dup
  unless merged.key?(:thinking) || merged.key?("thinking")
    merged[:thinking] = false
  end

  options[key || :chat_template_kwargs] = merged
  options
end

.extract_text(out) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/homura/runtime/ai.rb', line 357

def self.extract_text(out)
  return out.to_s unless out.is_a?(Hash)
  if out["choices"].is_a?(Array) && !out["choices"].empty?
    msg = out["choices"][0].is_a?(Hash) ? out["choices"][0]["message"] : nil
    text = message_hash_text(msg)
    return text unless text.empty?
  end

  if out["messages"].is_a?(Array) && !out["messages"].empty?
    msg = out["messages"].find do |entry|
      entry.is_a?(Hash) && entry["role"].to_s == "assistant"
    end ||
      out["messages"][0]
    text = message_hash_text(msg)
    return text unless text.empty?
  end

  %w[text transcription response result output].each do |key|
    value = message_content_text(out[key])
    return value unless value.empty?
  end

  nested = out["result"]
  return extract_text(nested) if nested.is_a?(Hash)
  ""
end

.message_content_text(value) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/homura/runtime/ai.rb', line 384

def self.message_content_text(value)
  case value
  when String
    value
  when Array
    value
      .map { |part| part.is_a?(Hash) ? part["text"].to_s : part.to_s }
      .join
  else
    ""
  end
end

.message_hash_text(value) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/homura/runtime/ai.rb', line 397

def self.message_hash_text(value)
  return "" unless value.is_a?(Hash)
  %w[
    content
    reasoning
    reasoning_content
    reasoningContent
    text
  ].each do |key|
    text = message_content_text(value[key])
    return text unless text.empty?
  end

  ""
end

.ruby_to_js(val) ⇒ Object

Convert a Ruby value (Hash / Array / String / Numeric / true / false / nil) into a plain JS object suitable for env.AI.run inputs.



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/homura/runtime/ai.rb', line 415

def self.ruby_to_js(val)
  if `#{val} != null && typeof #{val} === 'object' && #{val}.$$class == null`
    return val
  end

  if val.is_a?(Hash)
    obj = `({})`
    val.each do |k, v|
      ks = k.to_s
      jv = ruby_to_js(v)
      `#{obj}[#{ks}] = #{jv}`
    end

    obj
  elsif val.is_a?(Array)
    arr = `([])`
    val.each do |v|
      jv = ruby_to_js(v)
      `#{arr}.push(#{jv})`
    end

    arr
  elsif val.is_a?(Symbol)
    val.to_s
  else
    val
  end
end

.run(model, inputs, binding: nil, options: nil, raw_response: false) ⇒ Object

Run a Workers AI model. Returns a JS Promise that resolves to a Ruby Hash for non-streaming calls, or to a Cloudflare::AI::Stream wrapping the JS ReadableStream for streaming calls.

Parameters:

  • model (String)

    catalog model id, e.g. β€˜@cf/google/gemma-4-26b-a4b-it’

  • inputs (Hash)

    model inputs (messages / prompt / max_tokens / etc.)

  • binding (JS object) (defaults to: nil)

    env.AI binding (required)

  • options (Hash) (defaults to: nil)

    gateway / extra options forwarded as the 3rd arg



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/homura/runtime/ai.rb', line 172

def self.run(model, inputs, binding: nil, options: nil, raw_response: false)
  if defined?(Binding) &&
      `(#{binding} != null && #{binding}.$$class === #{Binding})`
    binding = binding.js
  end
  # Use a JS-side null check because `binding` may be a raw JS object
  # (env.AI), which has no Ruby `#nil?` method on the prototype.
  bound = !`(#{binding} == null)`
  unless bound
    raise AIError.new("AI binding not bound (env.AI is null)", model: model)
  end

  js_inputs = ruby_to_js(inputs)
  js_options = options ? ruby_to_js(options) : `({})`
  ai_binding = binding
  err_klass = Cloudflare::AIError
  stream_klass = Cloudflare::AI::Stream
  # Streaming may be requested either via `inputs[:stream]` (the
  # newer Workers AI shape) or `options: { stream: true }` (the
  # 3rd-arg "options" contract). Accept both so callers can use
  # whichever idiom matches the model docs they're following.
  streaming = (inputs.is_a?(Hash) &&
    (inputs[:stream] == true || inputs["stream"] == true)) ||
    (options.is_a?(Hash) &&
      (options[:stream] == true || options["stream"] == true))
  cf = Cloudflare

  # NOTE: multi-line backtick β†’ Promise works HERE because the
  # value is assigned to `js_promise` (Opal emits the statement AND
  # keeps the returned value alive through the local). Do NOT
  # refactor this so the backtick is the method's last expression
  # or the Promise will be silently dropped (same pitfall
  # documented in lib/homura/runtime/{cache,queue}.rb β€”
  # Phase 11B audit).
  js_promise = `
    (async function() {
      var out;
      try {
        out = await #{ai_binding}.run(#{model}, #{js_inputs}, #{js_options});
      } catch (e) {
        #{Kernel}.$raise(#{err_klass}.$new(e && e.message ? e.message : String(e), Opal.hash({ model: #{model}, operation: 'run' })));
      }
      return out;
    })()
  `

  js_result = js_promise.__await__

  if raw_response
    RawResponse.new(js_result)
  elsif streaming
    # Workers AI returns a ReadableStream<Uint8Array> when stream:true.
    # Wrap it so the Sinatra route can return it as an SSE body.
    stream_klass.new(js_result)
  else
    cf.js_to_ruby(js_result)
  end
end

.speak(model, inputs, binding: nil, options: nil) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/homura/runtime/ai.rb', line 231

def self.speak(model, inputs, binding: nil, options: nil)
  if defined?(Binding) &&
      `(#{binding} != null && #{binding}.$$class === #{Binding})`
    binding = binding.js
  end

  bound = !`(#{binding} == null)`
  unless bound
    raise AIError.new("AI binding not bound (env.AI is null)", model: model)
  end

  js_inputs = ruby_to_js(inputs)
  js_options = ruby_to_js((options || {}).merge(returnRawResponse: true))
  ai_binding = binding
  err_klass = Cloudflare::AIError

  js_response = `
    (async function() {
      try {
        return await #{ai_binding}.run(#{model}, #{js_inputs}, #{js_options});
      } catch (e) {
        #{Kernel}.$raise(#{err_klass}.$new(e && e.message ? e.message : String(e), Opal.hash({ model: #{model}, operation: 'speak' })));
      }
    })()
  `
    .__await__

  content_type = `#{js_response}.headers.get("content-type") || "application/octet-stream"`
  cache_control = `#{js_response}.headers.get("cache-control")`
  BinaryBody.new(`#{js_response}.body`, content_type, cache_control)
end

.speak_data_url(model, inputs, binding: nil, options: nil) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/homura/runtime/ai.rb', line 263

def self.speak_data_url(model, inputs, binding: nil, options: nil)
  if defined?(Binding) &&
      `(#{binding} != null && #{binding}.$$class === #{Binding})`
    binding = binding.js
  end

  bound = !`(#{binding} == null)`
  unless bound
    raise AIError.new("AI binding not bound (env.AI is null)", model: model)
  end

  js_inputs = ruby_to_js(inputs)
  js_options = ruby_to_js((options || {}).merge(returnRawResponse: true))
  ai_binding = binding
  err_klass = Cloudflare::AIError

  js_response = `
    (async function() {
      try {
        return await #{ai_binding}.run(#{model}, #{js_inputs}, #{js_options});
      } catch (e) {
        #{Kernel}.$raise(#{err_klass}.$new(e && e.message ? e.message : String(e), Opal.hash({ model: #{model}, operation: 'speak_data_url' })));
      }
    })()
  `
    .__await__

  content_type = `#{js_response}.headers.get("content-type") || "application/octet-stream"`
  `
    (async function(resp, ct) {
      var buf = await resp.arrayBuffer();
      var bytes = new Uint8Array(buf);
      var bin = '';
      for (var i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]);
      return 'data:' + ct + ';base64,' + globalThis.btoa(bin);
    })(#{js_response}, #{content_type})
  `
    .__await__
end