Class: SqlChatbot::Services::Orchestrator

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_chatbot/services/orchestrator.rb

Constant Summary collapse

VALID_TYPES =
%w[data data_with_code code navigation guidance greeting unsafe].freeze

Instance Method Summary collapse

Constructor Details

#initialize(llm_client:, schema_service:, code_indexer:, route_introspector_data: nil) ⇒ Orchestrator

Returns a new instance of Orchestrator.



20
21
22
23
24
25
26
# File 'lib/sql_chatbot/services/orchestrator.rb', line 20

def initialize(llm_client:, schema_service:, code_indexer:, route_introspector_data: nil)
  @llm = llm_client
  @schema = schema_service
  @code_indexer = code_indexer
  @route_introspector_data = route_introspector_data
  @manifest = nil
end

Instance Method Details

#handle_question(question:, page_context: nil, history: []) ⇒ Object

Returns an Enumerator that yields SSE event hashes. Events: classifying, classified, sql, executing, token, done, error



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sql_chatbot/services/orchestrator.rb', line 43

def handle_question(question:, page_context: nil, history: [])
  Enumerator.new do |yielder|
    begin
      # --- Step 1: Classify ---
      yielder.yield({ type: "classifying" })

      table_names_str = @schema.table_names
      classify_messages = Prompts::Classify.build_messages(
        question: question,
        schema_summary: table_names_str,
        page_context: page_context,
        history: history
      )

      raw = @llm.call(classify_messages, json_mode: true)
      classification = parse_classification(raw)

      yielder.yield({
        type: "classified",
        questionType: classification[:type],
        confidence: classification[:confidence]
      })

      # --- Step 2: Route by question type ---
      case classification[:type]
      when "data", "data_with_code"
        handle_data_with_code(yielder, question, classification, page_context, history)
      when "code"
        handle_code(yielder, question, classification, history)
      when "navigation", "guidance"
        handle_navigation(yielder, question, classification[:type], page_context, history)
      when "greeting"
        handle_greeting(yielder, question, history)
      when "unsafe"
        handle_unsafe(yielder)
      end

      yielder.yield({ type: "done" })
    rescue => e
      log_error(e)
      yielder.yield({ type: "error", message: friendly_error_message(e) })
    end
  end
end

#route_listObject



37
38
39
# File 'lib/sql_chatbot/services/orchestrator.rb', line 37

def route_list
  build_route_list
end

#set_manifest(manifest) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/sql_chatbot/services/orchestrator.rb', line 28

def set_manifest(manifest)
  version = manifest["version"] || manifest[:version]
  unless version == 1
    warn "[SqlChatbot] Unsupported manifest version: #{version}"
    return
  end
  @manifest = manifest
end