Class: Fosm::Admin::AgentsController

Inherits:
BaseController show all
Defined in:
app/controllers/fosm/admin/agents_controller.rb

Instance Method Summary collapse

Methods inherited from Fosm::ApplicationController

use_host_routes!

Instance Method Details

#agent_invokeObject



68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/fosm/admin/agents_controller.rb', line 68

def agent_invoke
  tool   = params[:tool].to_s
  id     = params[:record_id].presence
  filter = params[:filter].presence

  result = invoke_tool(tool, id, filter)
  render json: result
rescue => e
  render json: { error: e.message }, status: :unprocessable_entity
end

#chatObject

── Chat ──────────────────────────────────────────────────────────────────



18
19
20
# File 'app/controllers/fosm/admin/agents_controller.rb', line 18

def chat
  @history = chat_history
end

#chat_resetObject



62
63
64
65
66
# File 'app/controllers/fosm/admin/agents_controller.rb', line 62

def chat_reset
  ::Rails.cache.delete(chat_history_key)
  ::Rails.cache.delete(agent_cache_key)
  render json: { ok: true }
end

#chat_sendObject



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
# File 'app/controllers/fosm/admin/agents_controller.rb', line 22

def chat_send
  message = params[:message].to_s.strip
  return render json: { error: "Message cannot be blank" }, status: :bad_request if message.blank?

  agent = fetch_or_build_agent

  # reset: true starts fresh each turn (avoids trailing-whitespace issues with
  # multi-turn context). Visual history is maintained in the session.
  result = agent.run(message, reset: true, return_full_result: true)

  # Persist agent instance so next message continues the same conversation
  store_agent(agent)

  steps = Array(result.steps).map { |s|
    h = s.to_h
    # Ensure tool_calls are plain hashes for JSON serialization
    if h[:tool_calls]
      h[:tool_calls] = h[:tool_calls].map { |tc|
        { name: tc.function.name, args: tc.function.arguments }
      } rescue h[:tool_calls].map(&:to_s)
    end
    h
  }

  output = result.output.to_s

  # Store chat history in Rails.cache (not session cookie) to avoid
  # ActionDispatch::Cookies::CookieOverflow — agent responses can be large.
  history = chat_history
  history << { role: "user",  content: message }
  history << { role: "agent", content: output, timing: result.timing.round(2) }
  save_chat_history(history.last(10))

  render json: { output: output, steps: steps,
                 token_usage: result.token_usage.to_h,
                 timing: result.timing.round(2) }
rescue => e
  render json: { error: "#{e.class}: #{e.message}" }, status: :unprocessable_entity
end

#showObject



6
7
8
9
10
11
12
13
14
# File 'app/controllers/fosm/admin/agents_controller.rb', line 6

def show
  @tools = derive_tool_definitions
  @system_prompt = derive_system_prompt
  @agent_class = begin
    "Fosm::#{@model_class.name.demodulize}Agent".constantize
  rescue NameError
    nil
  end
end