module OpenUI::Rails::Message
Ruby implementation of the upstream OpenUI message protocol (buildMessage / parseMessage from ‘@openuidev/react-lang` and `@openuidev/lang-core`).
A stored message is the response text wrapped in marker lines that start with ‘]]>openui:`. The browser Renderer reads the whole stored message and ignores the context; the host app is responsible for restoring form state from the context as initialState. Markers must never reach the model: strip them with {.strip} (i.e. `parseMessage(content)`).
This is *message framing*, not an OpenUI Lang parser.
Constants
- CONTENT_MARKER
- CONTEXT_MARKER
- END_MARKER
- MARKERS
- MARKER_PREFIX
- Parsed
-
Result of {.parse}.
finishedmaps to the upstreamendfield.
Public Class Methods
# File lib/openui/rails/message.rb, line 44 def build(content:, context: nil, attributes: nil, finished: false, **options) finished = options.fetch(:end, finished) if options.key?(:end) raise ArgumentError, "unknown options: #{options.keys.inspect}" unless (options.keys - [ :end ]).empty? lines = [ content_marker(attributes), content.to_s ] unless context.nil? lines << CONTEXT_MARKER lines << (context.is_a?(String) ? context : JSON.generate(context)) end lines << END_MARKER if finished lines.join("\n") end
Wrap content (and optional context/attributes) in the marker format.
@param content [String] the response text (OpenUI Lang or prose) @param context [String, Object, nil] stored context; strings verbatim,
anything else JSON-encoded, nil writes no context section
@param attributes [Hash, nil] written after ‘?`, URLSearchParams-encoded @param finished [Boolean] add the end line (accepts the upstream
`end:` keyword too)
@return [String]
# File lib/openui/rails/message.rb, line 62 def parse(stored, streaming: false) # Match lang-core 1.0.1 framing, including retries, CRLF boundaries # and incomplete streaming markers. This is not a DSL parser. text = stored.to_s.gsub(/([^\n])(?=\]\]>openui:content(?:\?|\r?\n|\z))/, "\\1\n") if streaming && (cut = /[^\n](\]\]>[a-z:]*)\z/.match(text)) && CONTENT_MARKER.start_with?(cut[1]) text = text[0..cut.begin(0)] end lines = text.split("\n", -1) last = lines.last held = streaming && last && !last.empty? && (last.start_with?(MARKER_PREFIX) || MARKER_PREFIX.start_with?(last)) lines.pop if held sections = [ { kind: nil, attributes: {}, lines: [] } ] end_at = -1 lines.each_with_index do |line, index| match = /\A\]\]>openui:([a-z]+)(?:\?(.*))?\z/.match(line.delete_suffix("\r")) unless match sections.last[:lines] << line next end close_section(sections.last) if match[1] == "end" end_at = sections.size - 1 break if index == lines.size - 2 && lines.last == "" else sections << { kind: match[1], attributes: decode_attributes(match[2]), lines: [] } end end close_section(sections.last) if held winner = sections.rindex { |section| section[:kind] == "content" } || 0 context_index = sections.rindex { |section| section[:kind] == "context" } context = parse_context(sections[context_index][:lines].join("\n")) if context_index && context_index > winner Parsed.new(content: sections[winner][:lines].join("\n"), context: context, attributes: sections[winner][:attributes], finished: end_at >= winner) end
Parse a stored message into its parts.
@param stored [String] @param streaming [Boolean] hold back a trailing partial marker line @return [Parsed]
Source
# File lib/openui/rails/message.rb, line 98 def strip(stored) parse(stored).content end
Strip the markers for model history; returns only the content body.