Module: AgUi::Protocol::JsonSchema

Defined in:
lib/ag_ui/protocol/json_schema.rb,
lib/ag_ui/protocol/json_schema/definition.rb,
lib/ag_ui/protocol/json_schema/validation_error.rb

Overview

Schema-validated AG-UI protocol objects, powered by json_schemer. Same mechanism as a2a's Protocol::JsonSchema, over the schema bundle generated from the reference Python SDK's pydantic models (data/generate-ag-ui-schema.py -> data/ag_ui.json).

AgUi::Protocol::JsonSchema["TextMessageContentEvent"]
#=> Class < Definition with .schema, .valid?, reader methods

AgUi::Protocol::JsonSchema.list_definitions
#=> ["ActivityDeltaEvent", "ActivityMessage", ...]

Defined Under Namespace

Classes: Definition, ValidationError

Constant Summary collapse

DATA_PATH =
File.expand_path("../../../data/ag_ui.json", __dir__).freeze

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object

Look up a definition by model class name.

AgUi::Protocol::JsonSchema["RunAgentInput"]
#=> Class < Definition


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ag_ui/protocol/json_schema.rb', line 32

def [](name)
  @definition_classes[name] ||= begin
    definitions = raw_schema.fetch("definitions", {})

    unless definitions.key?(name)
      raise "No AG-UI definition found for #{name.inspect}!" \
        "\nAvailable: #{list_definitions.join(", ")}"
    end

    ref_schema = schemer.ref("#/definitions/#{name}")
    build_definition_class(ref_schema, name, definitions[name])
  end
end

.event_typesObject

Definition names for concrete events (have a const "type"), mapped from wire event type to definition name:

{ "TEXT_MESSAGE_CONTENT" => "TextMessageContentEvent", ... }


56
57
58
59
60
61
62
63
# File 'lib/ag_ui/protocol/json_schema.rb', line 56

def event_types
  @event_types ||= raw_schema.fetch("definitions", {}).each_with_object({}) do |(name, defn), map|
    const = defn.dig("properties", "type", "const")
    if const
      map[const] = name
    end
  end.freeze
end

.list_definitionsObject

All available definition names, sorted.



47
48
49
# File 'lib/ag_ui/protocol/json_schema.rb', line 47

def list_definitions
  raw_schema.fetch("definitions", {}).keys.sort
end

.raw_schemaObject

The parsed schema hash (all $refs are already internal — the generator emits #/definitions/... pointers).



72
73
74
# File 'lib/ag_ui/protocol/json_schema.rb', line 72

def raw_schema
  @raw_schema ||= JSON.parse(File.read(DATA_PATH))
end

.reset!Object

Reset all cached state (useful for tests).



77
78
79
80
81
82
# File 'lib/ag_ui/protocol/json_schema.rb', line 77

def reset!
  @definition_classes.clear
  @schemer = nil
  @raw_schema = nil
  @event_types = nil
end

.schemerObject

The JSONSchemer instance for the full AG-UI schema bundle.



66
67
68
# File 'lib/ag_ui/protocol/json_schema.rb', line 66

def schemer
  @schemer ||= JSONSchemer.schema(raw_schema)
end