2025-05-06 10:09:39 -03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Completions
|
|
|
|
class StructuredOutput
|
|
|
|
def initialize(json_schema_properties)
|
|
|
|
@property_names = json_schema_properties.keys.map(&:to_sym)
|
|
|
|
@property_cursors =
|
|
|
|
json_schema_properties.reduce({}) do |m, (k, prop)|
|
|
|
|
m[k.to_sym] = 0 if prop[:type] == "string"
|
|
|
|
m
|
|
|
|
end
|
|
|
|
|
|
|
|
@tracked = {}
|
|
|
|
|
2025-05-15 11:32:10 -03:00
|
|
|
@raw_response = +""
|
|
|
|
@raw_cursor = 0
|
|
|
|
|
2025-05-06 10:09:39 -03:00
|
|
|
@partial_json_tracker = JsonStreamingTracker.new(self)
|
2025-07-04 14:47:11 +10:00
|
|
|
|
|
|
|
@type_map = {}
|
|
|
|
json_schema_properties.each { |name, prop| @type_map[name.to_sym] = prop[:type].to_sym }
|
|
|
|
|
|
|
|
@done = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
# we may want to also normalize the JSON here for the broken case
|
|
|
|
@raw_response
|
2025-05-06 10:09:39 -03:00
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :last_chunk_buffer
|
|
|
|
|
|
|
|
def <<(raw)
|
2025-07-04 14:47:11 +10:00
|
|
|
raise "Cannot append to a completed StructuredOutput" if @done
|
2025-05-15 11:32:10 -03:00
|
|
|
@raw_response << raw
|
2025-05-06 10:09:39 -03:00
|
|
|
@partial_json_tracker << raw
|
|
|
|
end
|
|
|
|
|
2025-07-04 14:47:11 +10:00
|
|
|
def finish
|
|
|
|
@done = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def broken?
|
|
|
|
@partial_json_tracker.broken?
|
|
|
|
end
|
|
|
|
|
2025-05-15 11:32:10 -03:00
|
|
|
def read_buffered_property(prop_name)
|
|
|
|
if @partial_json_tracker.broken?
|
2025-07-04 14:47:11 +10:00
|
|
|
if @done
|
|
|
|
return nil if @type_map[prop_name.to_sym].nil?
|
|
|
|
return(
|
|
|
|
DiscourseAi::Utils::BestEffortJsonParser.extract_key(
|
|
|
|
@raw_response,
|
|
|
|
@type_map[prop_name.to_sym],
|
|
|
|
prop_name,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
2025-05-15 11:32:10 -03:00
|
|
|
end
|
2025-05-06 10:09:39 -03:00
|
|
|
|
2025-05-15 11:32:10 -03:00
|
|
|
# Maybe we haven't read that part of the JSON yet.
|
2025-06-27 10:35:47 -03:00
|
|
|
return nil if @tracked[prop_name].nil?
|
2025-05-15 11:32:10 -03:00
|
|
|
|
|
|
|
# This means this property is a string and we want to return unread chunks.
|
|
|
|
if @property_cursors[prop_name].present?
|
|
|
|
unread = @tracked[prop_name][@property_cursors[prop_name]..]
|
|
|
|
@property_cursors[prop_name] = @tracked[prop_name].length
|
|
|
|
unread
|
|
|
|
else
|
2025-06-02 14:29:20 -03:00
|
|
|
# Ints and bools, and arrays are always returned as is.
|
2025-05-15 11:32:10 -03:00
|
|
|
@tracked[prop_name]
|
2025-05-06 10:09:39 -03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_progress(key, value)
|
|
|
|
key_sym = key.to_sym
|
|
|
|
return if !@property_names.include?(key_sym)
|
|
|
|
|
|
|
|
@tracked[key_sym] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|