2
3
4
5
6
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
|
# File 'app/controllers/messages_controller.rb', line 2
def create
card = Board.first!.cards.find_by!(number: params[:card_id])
text = params.require(:message)[:text]
parked_run = card.runs.where(status: "needs_input").order(:id).last
live_run = card.runs.where(status: "running").order(:id).last
pending_permission = live_run && live_run.permission_requests.pending.order(:id).first
if params.dig(:message, :note) == "1"
card.log!("user_message", actor: "user", text: text, note: true)
elsif pending_permission
card.log!("user_message", actor: "user", run: live_run, text: text)
if text.strip.match?(/\A(y|yes|ok|okay|allow|approve|👍)\z/i)
pending_permission.resolve!("allowed")
else
pending_permission.resolve!("denied", message: text)
end
elsif parked_run
kind = parked_run.phase == "plan" ? "user_message" : "answer"
card.log!(kind, actor: "user", run: parked_run, text: text)
ResumeRunJob.perform_later(parked_run.id, text)
elsif card.column.execution? && card.working? && live_run
notes = Array(live_run.briefing["steering"]) << text
live_run.update!(briefing: live_run.briefing.merge("steering" => notes))
card.log!("user_message", actor: "user", run: live_run, text: text)
card.log!("status_change", run: live_run,
text: "🧭 Note queued for the agent — delivers at its next check-in")
elsif card.column.review? && %w[in_review approved].include?(card.status)
card.log!("user_message", actor: "user", text: text)
card.update!(status: "changes_requested")
card.log!("status_change", actor: "user", text: "Changes requested — drag the card back to a work column when ready")
else
card.log!("user_message", actor: "user", text: text)
AssistantReplyJob.perform_later(card) if card.column.planning? && card.column.ai?
end
redirect_to card_path(card)
end
|