Module: AgentClientProtocol::Constants

Defined in:
lib/agent_client_protocol/constants.rb

Overview

Convenience constants for enum-like schema values.

Auto-extracted from the JSON Schema at load time so they stay in sync with the canonical definitions. Use them instead of raw strings:

AgentClientProtocol::ToolKind::READ        # => "read"
AgentClientProtocol::ToolCallStatus::FAILED # => "failed"
AgentClientProtocol::StopReason::END_TURN   # => "end_turn"

Defined Under Namespace

Modules: Unstable

Constant Summary collapse

SKIP =

Names already defined elsewhere (e.g. ErrorCode in error.rb).

%w[ErrorCode].freeze

Class Method Summary collapse

Class Method Details

.build_constants_module(values) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/agent_client_protocol/constants.rb', line 46

def build_constants_module(values)
  mod = Module.new
  values.each do |value|
    mod.const_set(ruby_const_name(value), value.freeze)
  end
  mod.freeze
end

.define_all!(unstable: false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/agent_client_protocol/constants.rb', line 19

def define_all!(unstable: false)
  defs = SchemaRegistry.defs(unstable: unstable)
  target = unstable ? Unstable : self

  defs.each do |name, schema|
    next if SKIP.include?(name)

    values = extract_enum_values(schema)
    next if values.empty?
    next if target.const_defined?(name, false)

    target.const_set(name, build_constants_module(values))
  end
end

.extract_enum_values(schema) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/agent_client_protocol/constants.rb', line 34

def extract_enum_values(schema)
  if schema["enum"].is_a?(Array)
    return schema["enum"].select { |v| v.is_a?(String) || v.is_a?(Integer) }
  end

  variants = schema["oneOf"] || schema["anyOf"]
  return [] unless variants.is_a?(Array)

  consts = variants.filter_map { |v| v["const"] }
  consts.select { |c| c.is_a?(String) || c.is_a?(Integer) }
end

.ruby_const_name(value) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/agent_client_protocol/constants.rb', line 54

def ruby_const_name(value)
  value.to_s
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .tr("-", "_")
    .upcase
end