Class: Llm::Agent::ChatController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/llm/agent/chat_controller.rb

Instance Method Summary collapse

Instance Method Details

#stepObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
57
58
59
60
61
# File 'app/controllers/llm/agent/chat_controller.rb', line 7

def step
  raw = params[:messages]
  if raw.nil?
    return render json: { error: "BadRequest", message: "messages is required (array of {role, content})" }, status: :bad_request
  end

  # Coerce into an array of message hashes Rails/OpenAI will accept.
  messages =
    Array.wrap(raw).map do |m|
      # If the client sent a JSON string, parse it.
      m = JSON.parse(m) rescue m

      # If it's ActionController::Parameters, unfurl to a plain hash.
      m = m.to_unsafe_h if m.respond_to?(:to_unsafe_h)
      m = m.to_h        if m.respond_to?(:to_h) && !m.is_a?(Hash)

      # Keep only role/content, as OpenAI expects
      {
        "role"    => m["role"] || m[:role],
        "content" => m["content"] || m[:content]
      }
    end

  thread_id = params[:thread_id] || "http-#{request.request_id}"
  ctx = {
    tenant_id: params[:tenant_id] || "demo-org",
    actor_id:  params[:actor_id]  || "demo-user",
    thread_id: thread_id
  }

  adapter = ::Llm::Agent::Rails::Adapters::OpenAIAdapter.new(
    api_key: ENV.fetch("OPENAI_API_KEY"),
    model:    ::Llm::Agent::Rails.config[:model],
    temperature: ::Llm::Agent::Rails.config[:temperature]
  )

  orch = ::Llm::Agent::Rails::Orchestrator.new(
    adapter:  adapter,
    registry: ::Llm::Agent::Rails.config[:registry],
    store:    ::Llm::Agent::Rails.config[:store]
  )

  outcome = orch.step(
    thread_id: ctx[:thread_id],
    tenant_id: ctx[:tenant_id],
    actor_id:  ctx[:actor_id],
    messages:  messages
  )

  render json: outcome
rescue ActionController::ParameterMissing => e
  render json: { error: e.message }, status: :bad_request
rescue => e
  render json: { error: e.class.name, message: e.message }, status: :internal_server_error
end