Module: Brainiac::Plugins::Fizzy::Planning

Defined in:
lib/brainiac/plugins/fizzy/planning.rb

Overview

Planning mode — generates plans from agent sessions and creates Fizzy steps.

Constant Summary collapse

PLANS_DIR =
File.join(
  ENV.fetch("BRAINIAC_DIR", File.join(Dir.home, ".brainiac")),
  "plans"
)

Class Method Summary collapse

Class Method Details

.finalize_if_needed(prompt_file, agent_name, project_config) ⇒ Object

Called from :agent_completed hook after session finishes. Checks if a plan file was produced and creates Fizzy steps from it.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/brainiac/plugins/fizzy/planning.rb', line 16

def finalize_if_needed(prompt_file, agent_name, project_config)
  return unless prompt_file && File.exist?(prompt_file)

  prompt_content = File.read(prompt_file)
  card_id_match = prompt_content.match(/CARD_ID.*?(\d+|discord-[\w-]+)/)
  return unless card_id_match

  card_id = card_id_match[1]
  plan_file = File.join(PLANS_DIR, "card-#{card_id}-plan.md")
  return unless File.exist?(plan_file)

  LOG.info "[Fizzy:Planning] Plan file detected for card #{card_id}, finalizing..." if defined?(LOG)
  finalize_plan(card_id: card_id, agent_name: agent_name, project_config: project_config)
end

.finalize_plan(card_id:, agent_name:, project_config:) ⇒ Object



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
# File 'lib/brainiac/plugins/fizzy/planning.rb', line 31

def finalize_plan(card_id:, agent_name:, project_config:)
  plan_file = File.join(PLANS_DIR, "card-#{card_id}-plan.md")
  return { success: false, error: "No plan file" } unless File.exist?(plan_file)

  plan_content = File.read(plan_file)
  tasks = extract_tasks(plan_content)
  return { success: false, error: "No tasks found in plan" } if tasks.empty?

  card_number = card_id.match?(/^\d+$/) ? card_id.to_i : nil
  return { success: false, error: "No card number" } unless card_number

  repo_path = project_config["repo_path"]
  env = Helpers.fizzy_env_for(agent_name || AI_AGENT_NAME)

  tasks.each do |task_title|
    run_cmd("fizzy", "step", "create", "--card", card_number.to_s, "--content", task_title,
            chdir: repo_path, env: env)
  end

  LOG.info "[Fizzy:Planning] Created #{tasks.size} steps for card ##{card_number}" if defined?(LOG)
  { success: true, tasks: tasks }
rescue StandardError => e
  LOG.error "[Fizzy:Planning] Failed to finalize plan: #{e.message}" if defined?(LOG)
  { success: false, error: e.message }
end