Skip to content

A platform for community discussion. Free, open, simple.

11.6 KB
Raw Blame
# frozen_string_literal: true
module DiscourseAi::Completions
  class OpenAiResponsesMessageProcessor
    PROVIDER_KEY = :open_ai_responses

    attr_reader :prompt_tokens,
                :completion_tokens,
                :cache_read_tokens,
                :cache_write_tokens,
                :stop_reason,
                :reasoning_tokens

    def initialize(partial_tool_calls: false, output_thinking: false)
      @tool = nil # currently streaming ToolCall
      @tool_arguments = +""
      @prompt_tokens = nil
      @completion_tokens = nil
      @cache_read_tokens = nil
      @cache_write_tokens = nil
      @partial_tool_calls = partial_tool_calls
      @streaming_parser = nil # JsonStreamingTracker, if used
      @has_new_data = false
      @output_thinking = output_thinking
      @reasoning_contexts = {}
      @pending_reasonings = []
      @has_first_summary_part = false
      @response_output_items = []
      @emitted_response_output_items = false
    end

    def output_limit_reached
      @stop_reason == "max_output_tokens"
    end

    # @param json [Hash] full JSON response from responses.create / retrieve
    # @return [Array<String,ToolCall>] pieces in the order they were produced
    def process_message(json)
      json = normalize_provider_payload(json)
      result = []

      pending_reasonings = []

      output = json[:output] || []
      @response_output_items = output.deep_dup

      output.each do |item|
        type = item[:type]

        case type
        when "reasoning"
          if @output_thinking
            thinking = build_thinking_from_reasoning(item)
            pending_reasonings << thinking
            result << thinking
          end
        when "function_call"
          result << build_tool_call_from_item(item)
        when "web_search_call"
          if @output_thinking
            result << build_native_tool_thinking(item, include_provider_info: true)
          end
        when "message"
          text = extract_text(item)
          attach_message_id_to_reasonings(pending_reasonings, item[:id])
          result << text if text
        end
      end

      update_stop_reason(json[:response] || json)
      update_usage(json)
      result
    end

    # @param json [Hash] a single streamed event, already parsed from ND-JSON
    # @return [String, ToolCall, nil] only when a complete chunk is ready
    def process_streamed_message(json)
      json = normalize_provider_payload(json)
      rval = nil
      event_type = json[:type]

      case event_type
      when "response.output_text.delta"
        delta = json[:delta]
        rval = delta if !delta.empty?
      when "response.reasoning_summary_part.added"
        rval = build_partial_reasoning_delta(json, prefix: "\n\n") if @has_first_summary_part
      when "response.reasoning_summary_text.delta"
        rval = build_partial_reasoning_delta(json)
      when "response.reasoning_summary_part.done"
        @has_first_summary_part = true
      when "response.output_item.added"
        item = json[:item]
        if item
          if item[:type] == "function_call"
            handle_tool_stream(:start, item) { |finished| rval = finished }
          elsif item[:type] == "reasoning" && @output_thinking
            register_reasoning_context(item)
          end
        end
      when "response.function_call_arguments.delta"
        delta = json[:delta]
        handle_tool_stream(:progress, delta) { |finished| rval = finished } if delta
      when "response.output_item.done"
        item = json[:item]
        if item
          track_response_output_item(item)
          if item[:type] == "function_call"
            handle_tool_stream(:done, item) { |finished| rval = finished }
          elsif item[:type] == "reasoning" && @output_thinking
            return finalize_reasoning_context(item)
          elsif item[:type] == "web_search_call" && @output_thinking
            return build_native_tool_thinking(item)
          elsif item[:type] == "message"
            attach_message_id_to_pending_stream_reasonings(item[:id])
          end
        end
      when "response.completed"
        track_completed_response(json[:response])
      end

      update_stop_reason(json[:response] || json)
      update_usage(json)
      rval
    end

    # Called by JsonStreamingTracker when partial JSON arguments are parsed
    def notify_progress(key, value)
      if @tool
        @tool.partial = true
        @tool.parameters[key.to_sym] = value
        @has_new_data = true
      end
    end

    def current_tool_progress
      if @has_new_data
        @has_new_data = false
        @tool
      end
    end

    def finish
      rval = []
      if @tool
        process_arguments
        rval << @tool
        @tool = nil
      end
      if @output_thinking && (thinking = build_response_output_items_thinking)
        rval << thinking
      end
      rval
    end

    private

    def extract_text(message_item)
      (message_item[:content] || [])
        .filter { |content| content[:type] == "output_text" }
        .map { |content| content[:text] }
        .join
    end

    def build_thinking_from_reasoning(item)
      provider_payload = {
        reasoning_id: item[:id],
        encrypted_content: item[:encrypted_content],
      }.compact

      summary_text = (item.dig(:summary) || []).map { |s| s[:text].to_s }.join("\n\n")

      # worth considering if we return multiple Thinking objects for each summary part?
      Thinking.new(
        message: summary_text,
        partial: false,
        provider_info: {
          PROVIDER_KEY => provider_payload,
        },
      )
    end

    def build_tool_call_from_item(item)
      call_id = item[:call_id]
      item_id = item[:id]
      name = item[:name]
      arguments = item[:arguments] || ""
      params = ToolArgumentsParser.parse(arguments)

      ToolCall.new(
        id: call_id,
        name: name,
        parameters: params,
        provider_data: {
          PROVIDER_KEY => { id: item_id, call_id: call_id }.compact,
        },
      )
    end

    def build_native_tool_thinking(item, include_provider_info: false)
      provider_info = include_provider_info ? response_output_items_provider_info : {}

      Thinking.new(message: native_tool_summary(item), partial: false, provider_info: provider_info)
    end

    def normalize_provider_payload(payload)
      payload.respond_to?(:deep_symbolize_keys) ? payload.deep_symbolize_keys : payload
    end

    def build_response_output_items_thinking
      return if @response_output_items.blank? || @emitted_response_output_items
      return if !@response_output_items.any? { |item| native_tool_item?(item) }

      @emitted_response_output_items = true
      Thinking.new(message: nil, partial: false, provider_info: response_output_items_provider_info)
    end

    def response_output_items_provider_info
      { PROVIDER_KEY => { output_items: @response_output_items } }
    end

    def track_response_output_item(item)
      @response_output_items << item.deep_dup
    end

    def track_completed_response(response)
      output = response&.dig(:output)
      @response_output_items = output.deep_dup if output.present?
    end

    def native_tool_item?(item)
      item[:type] == "web_search_call"
    end

    def native_tool_summary(item)
      action = item[:action] || {}

      case action[:type]
      when "search"
        queries = Array(action[:queries]).presence || Array(action[:query])
        queries.present? ? "Web search: #{queries.join(", ")}" : "Web search"
      when "open_page"
        target = action.values_at(:url, :page_url, :title).compact.first
        target.present? ? "Web fetch: #{target}" : "Web fetch"
      when "find_in_page"
        target = action.values_at(:pattern, :query).compact.first
        target.present? ? "Web page search: #{target}" : "Web page search"
      else
        "Used native web tool"
      end
    end

    def handle_tool_stream(event_type, json)
      if event_type == :start
        start_tool_stream(json)
      elsif event_type == :progress
        @streaming_parser << json if @streaming_parser
        yield current_tool_progress
      elsif event_type == :done
        @tool_arguments << json[:arguments].to_s
        process_arguments
        finished = @tool
        @tool = nil
        yield finished
      end
    end

    def start_tool_stream(data)
      call_id = data[:call_id]
      item_id = data[:id]
      name = data[:name]

      @tool_arguments = +""
      @tool =
        ToolCall.new(
          id: call_id,
          name: name,
          provider_data: {
            PROVIDER_KEY => { id: item_id, call_id: call_id }.compact,
          },
        )
      @streaming_parser = JsonStreamingTracker.new(self) if @partial_tool_calls
    end

    # Parse accumulated @tool_arguments once we have a complete JSON blob
    def process_arguments
      return if @tool_arguments.to_s.empty?
      parsed = ToolArgumentsParser.parse(@tool_arguments)
      @tool.parameters = parsed
      @tool.partial = false
      @tool_arguments = nil
    rescue JSON::ParserError
      # leave arguments empty; caller can decide how to handle
    end

    def update_stop_reason(response)
      if response[:status] == "incomplete"
        @stop_reason = response.dig(:incomplete_details, :reason)
      elsif response[:status] == "completed"
        @stop_reason = "completed"
      end
    end

    def update_usage(json)
      usage = json.dig(:response, :usage) || json[:usage]
      return if !usage

      @reasoning_tokens = usage.dig(:output_tokens_details, :reasoning_tokens) if usage[
        :output_tokens_details
      ]&.key?(:reasoning_tokens)
      token_details = usage[:input_tokens_details] || {}
      cached_tokens = token_details[:cached_tokens].to_i
      cache_write_tokens = token_details[:cache_write_tokens].to_i

      @prompt_tokens = usage[:input_tokens] - cached_tokens - cache_write_tokens
      @completion_tokens = usage[:output_tokens]
      @cache_read_tokens = cached_tokens
      @cache_write_tokens = cache_write_tokens
    end

    def build_partial_reasoning_delta(json, prefix: nil)
      return unless @output_thinking

      delta = json[:delta]
      context = @reasoning_contexts[json[:item_id]]
      if context
        delta = prefix + delta.to_s if prefix
        context[:summary] << delta.to_s
      end

      Thinking.new(message: delta, partial: true)
    end

    def register_reasoning_context(item)
      @reasoning_contexts[item[:id]] ||= { summary: +"", encrypted_content: nil, thinking: nil }
    end

    def finalize_reasoning_context(item)
      context = @reasoning_contexts.delete(item[:id]) || { summary: +"", thinking: nil }
      summary = context[:summary].presence || item.dig(:summary, 0, :text).to_s || +""

      thinking =
        Thinking.new(
          message: summary,
          partial: false,
          provider_info: {
            PROVIDER_KEY => {
              reasoning_id: item[:id],
              encrypted_content: item[:encrypted_content],
            }.compact,
          },
        )

      context[:thinking] = thinking
      @pending_reasonings << context
      thinking
    end

    def attach_message_id_to_reasonings(queue, message_id)
      return if queue.empty? || message_id.blank?

      queue.each { |thinking| assign_message_id(thinking, message_id) }

      queue.clear
    end

    def attach_message_id_to_pending_stream_reasonings(message_id)
      return if @pending_reasonings.empty? || message_id.blank?

      while (context = @pending_reasonings.first)
        break unless context[:thinking]
        assign_message_id(context[:thinking], message_id)
        @pending_reasonings.shift
      end
    end

    def assign_message_id(thinking, message_id)
      data = thinking.provider_info[PROVIDER_KEY] ||= {}
      data[:next_message_id] = message_id
    end
  end
end