Class: RailsConsoleAi::SkillsController

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

Instance Method Summary collapse

Instance Method Details

#approveObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 107

def approve
  redirect_to skills_path, alert: file_skill_message and return unless @skill.is_a?(RailsConsoleAi::Skill)

  approver = params[:approved_by].presence ||
             (request.respond_to?(:remote_user) && request.remote_user.presence) ||
             'web'

  if @skill.approved?
    redirect_to skill_path(@skill), notice: 'Skill is already approved.'
    return
  end

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

#createObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 62

def create
  @skill = Skill.new
  attrs = skill_params
  begin
    @skill.update_with_version!(
      attrs,
      edited_by: edited_by_param,
      change_note: params[:change_note].presence
    )
    redirect_to skill_path(@skill), notice: 'Skill created.'
  rescue ActiveRecord::RecordInvalid => e
    flash.now[:alert] = e.message
    render :new
  end
end

#destroyObject



98
99
100
101
102
103
104
105
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 98

def destroy
  if @skill.is_a?(RailsConsoleAi::Skill)
    @skill.destroy
    redirect_to skills_path, notice: 'Skill deleted. Past versions remain in history.'
  else
    redirect_to skills_path, alert: file_skill_message
  end
end

#diffObject

GET /skills/diff?skill_id=&from=&to=



128
129
130
131
132
133
134
135
136
137
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 128

def diff
  @skill = Skill.find(params[:skill_id])
  @from = @skill.versions.find(params[:from])
  @to   = params[:to].present? ? @skill.versions.find(params[:to]) : nil
  # If `to` is omitted, diff against the current skill.
  @to_label = @to ? "Version ##{@to.id}" : 'Current'
  @to_body  = @to ? @to.body : @skill.body
  @to_tags  = @to ? Array(@to.tags) : Array(@skill.tags)
  @to_bypass = @to ? Array(@to.bypass_guards_for_methods) : Array(@skill.bypass_guards_for_methods)
end

#editObject



78
79
80
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 78

def edit
  redirect_to skills_path, alert: file_skill_message and return unless @skill.is_a?(RailsConsoleAi::Skill)
end

#importObject

POST /skills/import — accepts a pasted .md blob in params, parses YAML frontmatter + body, and re-renders ‘new` with the fields pre-populated. The user reviews + clicks Create skill to actually persist (normal proposed- status + version-row flow applies).



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/rails_console_ai/skills_controller.rb', line 36

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

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

  @skill = Skill.new(
    name: parsed['name'],
    description: parsed['description'],
    body: parsed['body']
  )
  @skill.tags = Array(parsed['tags'])
  @skill.bypass_guards_for_methods = Array(parsed['bypass_guards_for_methods'])

  flash.now[:notice] = "Parsed \"#{parsed['name']}\" from pasted content. Review the fields below and click Create skill to save to the DB."
  render :new
end

#indexObject



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

def index
  @skills = SkillLoader.new.load_all_skills
  @q = params[:q].to_s.strip
  unless @q.empty?
    needle = @q.downcase
    @skills = @skills.select { |s|
      [s['name'], s['description'], Array(s['tags']).join(' ')].compact.join(' ').downcase.include?(needle)
    }
  end

  @sort = params[:sort].to_s
  if @sort == 'used'
    # Most-used first; file/builtin records (no counter) sink to the bottom alphabetically.
    @skills = @skills.sort_by { |s| [-(s['use_count'].to_i), s['name'].to_s.downcase] }
  end
end

#newObject



28
29
30
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 28

def new
  @skill = Skill.new
end

#showObject



24
25
26
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 24

def show
  @versions = @skill.versions if @skill.is_a?(RailsConsoleAi::Skill)
end

#updateObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 82

def update
  redirect_to skills_path, alert: file_skill_message and return unless @skill.is_a?(RailsConsoleAi::Skill)

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