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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/controllers/rails_console_ai/agents_controller.rb', line 117

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/rails_console_ai/agents_controller.rb', line 45

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



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

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



135
136
137
138
139
140
141
142
# File 'app/controllers/rails_console_ai/agents_controller.rb', line 135

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_body  = @to ? @to.body : @agent.body
  @to_tools = @to ? Array(@to.tools) : Array(@agent.tools)
end

#editObject



88
89
90
# File 'app/controllers/rails_console_ai/agents_controller.rb', line 88

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

#importObject

POST /agents/import — parse a pasted .md blob and re-render ‘new` with fields prefilled.



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 'app/controllers/rails_console_ai/agents_controller.rb', line 61

def import
  content = params[:content].to_s
  if content.strip.empty?
    redirect_to new_agent_path, alert: 'Nothing to parse — paste the .md content into the box first.'
    return
  end

  parsed = AgentLoader.parse(content)
  if parsed.nil? || parsed['name'].to_s.strip.empty?
    redirect_to new_agent_path,
                alert: 'Could not parse. Expected YAML frontmatter (between `---` lines) with at least a `name` field, followed by the agent body.'
    return
  end

  @agent = Agent.new(
    name: parsed['name'],
    description: parsed['description'],
    body: parsed['body'],
    max_rounds: parsed['max_rounds'],
    model: parsed['model']
  )
  @agent.tools = Array(parsed['tools'])

  flash.now[:notice] = "Parsed \"#{parsed['name']}\" from pasted content. Review the fields below and click Create agent to save to the DB."
  render :new
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
# 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.name        = builtin['name']
      @agent.description = builtin['description']
      @agent.body        = builtin['body']
      @agent.max_rounds  = builtin['max_rounds']
      @agent.model       = builtin['model']
      @agent.tools       = Array(builtin['tools'])
    end
  end
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



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

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