Class: SqlChatbot::ChatbotController

Inherits:
ActionController::Base
  • Object
show all
Includes:
ActionController::Live
Defined in:
app/controllers/sql_chatbot/chatbot_controller.rb

Instance Method Summary collapse

Instance Method Details

#askObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/sql_chatbot/chatbot_controller.rb', line 32

def ask
  return render_unauthorized unless authorized?
  ensure_initialized!

  question = params[:question]
  return render json: { error: "question is required" }, status: 400 if question.blank?

  response.headers["Content-Type"] = "text/event-stream"
  response.headers["Cache-Control"] = "no-cache"
  response.headers["Connection"] = "keep-alive"

  SqlChatbot.orchestrator.handle_question(
    question: question,
    page_context: params[:pageContext],
    history: params[:history],
  ).each do |event|
    response.stream.write("data: #{event.to_json}\n\n")
  end
rescue => e
  unless response.stream.closed?
    response.stream.write("data: #{({ type: "error", message: e.message }).to_json}\n\n")
  end
ensure
  response.stream.close
end

#create_sessionObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/sql_chatbot/chatbot_controller.rb', line 84

def create_session
  origin = request.headers["Origin"]

  # Validate origin
  allowed_origins = SqlChatbot.config&.allowed_origins
  if origin && !Auth::Cors.origin_allowed?(origin, allowed_origins)
    return render json: { error: "Origin not allowed" }, status: 403
  end

  # Check auth
  unless authorized?
    return render_unauthorized
  end

  config = SqlChatbot.config
  token = Auth::Jwt.generate_token(
    secret: config.resolved_token_secret,
    origin: origin,
    lifetime_seconds: config.token_lifetime
  )

  render json: { token: token, expires_in: config.token_lifetime }
end

#healthObject



21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/sql_chatbot/chatbot_controller.rb', line 21

def health
  ensure_initialized!
  render json: {
    status: "ok",
    tables: SqlChatbot.schema_service.table_count,
    codeFiles: SqlChatbot.code_indexer.file_count,
  }
rescue => e
  render json: { status: "error", message: e.message }, status: 500
end

#preflightObject



108
109
110
# File 'app/controllers/sql_chatbot/chatbot_controller.rb', line 108

def preflight
  head :no_content
end

#receive_manifestObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/sql_chatbot/chatbot_controller.rb', line 68

def receive_manifest
  return render_unauthorized unless authorized?
  ensure_initialized!

  manifest = params[:manifest]
  if manifest.present?
    manifest_data = manifest.respond_to?(:to_unsafe_h) ? manifest.to_unsafe_h : manifest.to_h
    SqlChatbot.orchestrator.set_manifest(manifest_data)
    render json: { status: "received", routeCount: manifest["routes"]&.length || 0 }
  else
    render json: { error: "manifest is required" }, status: 400
  end
rescue => e
  render json: { status: "error", message: e.message }, status: 500
end

#refreshObject



58
59
60
61
62
63
64
65
66
# File 'app/controllers/sql_chatbot/chatbot_controller.rb', line 58

def refresh
  return render_unauthorized unless authorized?
  ensure_initialized!
  SqlChatbot.schema_service.discover
  SqlChatbot.code_indexer.index(SqlChatbot.config.code_paths)
  render json: { status: "refreshed" }
rescue => e
  render json: { status: "error", message: e.message }, status: 500
end

#widgetObject



9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/sql_chatbot/chatbot_controller.rb', line 9

def widget
  if SqlChatbot.config&.secret
    cookies[:chatbot_token] = {
      value: SqlChatbot.config.secret,
      httponly: true,
      same_site: :strict,
    }
  end
  widget_path = File.join(SqlChatbot::Engine.root, "vendor", "assets", "widget.js")
  render body: File.read(widget_path), content_type: "application/javascript"
end