Class: RailsConsoleAi::AgentsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/rails_console_ai/agents_controller.rb

Instance Method Summary collapse

Instance Method Details

#approveObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/rails_console_ai/agents_controller.rb', line 92

def approve
  redirect_to agents_path, alert: read_only_message and return unless @agent.is_a?(RailsConsoleAi::Agent)

  approver = params[:approved_by].presence || 'web'

  if @agent.approved?
    redirect_to agent_path(@agent), notice: 'Agent is already approved.'
    return
  end

  begin
    @agent.approve!(approved_by: approver)
    redirect_to agent_path(@agent), notice: "Approved by #{approver}. The AI can now invoke this agent via delegate_task."
  rescue ArgumentError, ActiveRecord::RecordInvalid => e
    redirect_to agent_path(@agent), alert: "Could not approve: #{e.message}"
  end
end

#createObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/rails_console_ai/agents_controller.rb', line 48

def create
  @agent = Agent.new
  begin
    @agent.update_with_version!(
      agent_params,
      edited_by: edited_by_param,
      change_note: params[:change_note].presence
    )
    redirect_to agent_path(@agent), notice: 'Agent created.'
  rescue ActiveRecord::RecordInvalid => e
    flash.now[:alert] = e.message
    render :new
  end
end

#destroyObject



83
84
85
86
87
88
89
90
# File 'app/controllers/rails_console_ai/agents_controller.rb', line 83

def destroy
  if @agent.is_a?(RailsConsoleAi::Agent)
    @agent.destroy
    redirect_to agents_path, notice: 'Agent deleted. Past versions remain in history.'
  else
    redirect_to agents_path, alert: read_only_message
  end
end

#diffObject



110
111
112
113
114
115
116
# File 'app/controllers/rails_console_ai/agents_controller.rb', line 110

def diff
  @agent = Agent.find(params[:agent_id])
  @from = @agent.versions.find(params[:from])
  @to   = params[:to].present? ? @agent.versions.find(params[:to]) : nil
  @to_label   = @to ? "Version ##{@to.id}" : 'Current'
  @to_content = @to ? @to.content : @agent.content
end

#editObject



63
64
65
# File 'app/controllers/rails_console_ai/agents_controller.rb', line 63

def edit
  redirect_to agents_path, alert: read_only_message and return unless @agent.is_a?(RailsConsoleAi::Agent)
end

#indexObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/rails_console_ai/agents_controller.rb', line 7

def index
  @agents = AgentLoader.new.load_all_agents
  @q = params[:q].to_s.strip
  unless @q.empty?
    needle = @q.downcase
    @agents = @agents.select { |a|
      [a['name'], a['description'], Array(a['tools']).join(' ')].compact.join(' ').downcase.include?(needle)
    }
  end

  @sort = params[:sort].to_s
  if @sort == 'used'
    @agents = @agents.sort_by { |a| [-(a['use_count'].to_i), a['name'].to_s.downcase] }
  end
end

#newObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/rails_console_ai/agents_controller.rb', line 27

def new
  # Allow prefilling from a built-in (the "Create override" action on the show page).
  @agent = Agent.new
  if params[:from_builtin].present?
    builtin = AgentLoader.new.load_all_agents.find { |a|
      a['source'] == :builtin && a['name'].to_s.downcase == params[:from_builtin].to_s.downcase
    }
    if builtin
      @agent.content = AgentLoader.dump(
        name: builtin['name'],
        description: builtin['description'],
        body: builtin['body'],
        max_rounds: builtin['max_rounds'],
        model: builtin['model'],
        tools: Array(builtin['tools'])
      )
    end
  end
  @agent.content ||= new_agent_template
end

#showObject



23
24
25
# File 'app/controllers/rails_console_ai/agents_controller.rb', line 23

def show
  @versions = @agent.versions if @agent.is_a?(RailsConsoleAi::Agent)
end

#updateObject



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

def update
  redirect_to agents_path, alert: read_only_message and return unless @agent.is_a?(RailsConsoleAi::Agent)

  begin
    @agent.update_with_version!(
      agent_params,
      edited_by: edited_by_param,
      change_note: params[:change_note].presence
    )
    redirect_to agent_path(@agent), notice: 'Agent updated.'
  rescue ActiveRecord::RecordInvalid => e
    flash.now[:alert] = e.message
    render :edit
  end
end