Class: Gemini::Live::MessageBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/gemini/live/message_builder.rb

Overview

Helper class to build Live API messages

Constant Summary collapse

VALID_SCHEDULING =
%w[INTERRUPT WHEN_IDLE SILENT].freeze

Class Method Summary collapse

Class Method Details

.activity_endObject

Build activity end message (for manual VAD)



120
121
122
123
124
125
126
# File 'lib/gemini/live/message_builder.rb', line 120

def activity_end
  {
    realtimeInput: {
      activityEnd: {}
    }
  }
end

.activity_startObject

Build activity start message (for manual VAD)



111
112
113
114
115
116
117
# File 'lib/gemini/live/message_builder.rb', line 111

def activity_start
  {
    realtimeInput: {
      activityStart: {}
    }
  }
end

.client_content(text:, turn_complete: true, role: "user") ⇒ Object

Build client content message (text)



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gemini/live/message_builder.rb', line 54

def client_content(text:, turn_complete: true, role: "user")
  {
    clientContent: {
      turns: [
        {
          role: role,
          parts: [{ text: text }]
        }
      ],
      turnComplete: turn_complete
    }
  }
end

.client_content_parts(parts:, turn_complete: true, role: "user") ⇒ Object

Build client content with multiple parts



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gemini/live/message_builder.rb', line 69

def client_content_parts(parts:, turn_complete: true, role: "user")
  {
    clientContent: {
      turns: [
        {
          role: role,
          parts: parts
        }
      ],
      turnComplete: turn_complete
    }
  }
end

.realtime_input(audio_data: nil, video_data: nil, mime_type:) ⇒ Object

Build realtime input message (audio/video) using the legacy mediaChunks field. NOTE: mediaChunks is deprecated by the API in favor of the dedicated audio/video fields built by realtime_audio and realtime_video. Kept for backward compatibility with older Live models that still accept it.



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gemini/live/message_builder.rb', line 88

def realtime_input(audio_data: nil, video_data: nil, mime_type:)
  data = audio_data || video_data
  {
    realtimeInput: {
      mediaChunks: [
        {
          mimeType: mime_type,
          data: data
        }
      ]
    }
  }
end

.realtime_text(text) ⇒ Object

Build a realtime text input message. This is the universal text-input form for the Live API and is required by newer Live models such as gemini-3.1-flash-live-preview, which reject the turn-based clientContent payload.



106
107
108
# File 'lib/gemini/live/message_builder.rb', line 106

def realtime_text(text)
  { realtimeInput: { text: text.to_s } }
end

.setup(config) ⇒ Object

Build setup message from configuration



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gemini/live/message_builder.rb', line 11

def setup(config)
  message = {
    setup: {
      model: normalize_model_name(config.model)
    }
  }

  generation_config = build_generation_config(config)
  message[:setup][:generationConfig] = generation_config unless generation_config.empty?

  # System instruction
  if config.system_instruction
    message[:setup][:systemInstruction] = {
      parts: [{ text: config.system_instruction }]
    }
  end

  # Tools configuration
  message[:setup][:tools] = config.tools if config.tools

  # Context window compression
  if config.context_window_compression
    message[:setup][:contextWindowCompression] = config.context_window_compression
  end

  # Session resumption
  if config.session_resumption
    message[:setup][:sessionResumption] = config.session_resumption
  end

  # VAD (Voice Activity Detection) settings
  unless config.automatic_activity_detection
    message[:setup][:realtimeInputConfig] = {
      automaticActivityDetection: {
        disabled: true
      }
    }
  end

  message
end

.tool_response(function_responses) ⇒ Object

Build tool response message.

Each function response hash supports:

:id       - The function call id from the server
:name     - The function name
:response - The function result (Hash or scalar). When using
            NON_BLOCKING (async) function calls, include
            `scheduling: "INTERRUPT" | "WHEN_IDLE" | "SILENT"`
            inside the response hash.
:scheduling - (optional) Top-level shortcut. When provided,
              it is merged into the response hash as
              `response[:scheduling]`. Accepts Symbol or String.

Raises ArgumentError if scheduling is not one of the valid values.



142
143
144
145
146
147
148
# File 'lib/gemini/live/message_builder.rb', line 142

def tool_response(function_responses)
  {
    toolResponse: {
      functionResponses: function_responses.map { |resp| build_function_response(resp) }
    }
  }
end