Class: Aidp::Skills::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/skills/router.rb

Overview

Routes file paths and tasks to appropriate skills based on routing rules

The Router reads routing configuration from aidp.yml and matches:

  • File paths against glob patterns (path_rules)

  • Task descriptions against keywords (task_rules)

  • Combined path + task rules (combined_rules)

Examples:

Basic usage

router = Router.new(project_dir: Dir.pwd)
skill_id = router.route_by_path("app/controllers/users_controller.rb")
# => "rails_expert"

Task-based routing

router = Router.new(project_dir: Dir.pwd)
skill_id = router.route_by_task("Add API endpoint")
# => "backend_developer"

Combined routing

router = Router.new(project_dir: Dir.pwd)
skill_id = router.route(path: "lib/cli.rb", task: "Add command")
# => "cli_expert"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_dir:) ⇒ Router

Initialize router with project directory

Parameters:

  • project_dir (String)

    Path to project directory



34
35
36
37
# File 'lib/aidp/skills/router.rb', line 34

def initialize(project_dir:)
  @project_dir = project_dir
  @config = load_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



29
30
31
# File 'lib/aidp/skills/router.rb', line 29

def config
  @config
end

#project_dirObject (readonly)

Returns the value of attribute project_dir.



29
30
31
# File 'lib/aidp/skills/router.rb', line 29

def project_dir
  @project_dir
end

Instance Method Details

#default_skillString?

Get default skill

Returns:

  • (String, nil)

    Default skill ID or nil



112
113
114
# File 'lib/aidp/skills/router.rb', line 112

def default_skill
  config.dig("skills", "routing", "default")
end

#route(path: nil, task: nil) ⇒ String?

Route based on both path and task (highest priority)

Parameters:

  • path (String, nil) (defaults to: nil)

    File path to route

  • task (String, nil) (defaults to: nil)

    Task description to route

Returns:

  • (String, nil)

    Matched skill ID or nil



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/aidp/skills/router.rb', line 44

def route(path: nil, task: nil)
  # Priority 1: Combined rules (path AND task)
  if path && task
    combined_match = match_combined_rules(path, task)
    return combined_match if combined_match
  end

  # Priority 2: Path rules
  path_match = route_by_path(path) if path
  return path_match if path_match

  # Priority 3: Task rules
  task_match = route_by_task(task) if task
  return task_match if task_match

  # Priority 4: Default skill
  config.dig("skills", "routing", "default")
end

#route_by_path(path) ⇒ String?

Route based on file path

Parameters:

  • path (String)

    File path to match against patterns

Returns:

  • (String, nil)

    Matched skill ID or nil



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/aidp/skills/router.rb', line 67

def route_by_path(path)
  return nil unless path

  path_rules = config.dig("skills", "routing", "path_rules") || {}

  path_rules.each do |skill_id, patterns|
    patterns = [patterns] unless patterns.is_a?(Array)
    patterns.each do |pattern|
      return skill_id if File.fnmatch?(pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB)
    end
  end

  nil
end

#route_by_task(task) ⇒ String?

Route based on task description

Parameters:

  • task (String)

    Task description to match against keywords

Returns:

  • (String, nil)

    Matched skill ID or nil



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/aidp/skills/router.rb', line 86

def route_by_task(task)
  return nil unless task

  task_rules = config.dig("skills", "routing", "task_rules") || {}
  task_lower = task.downcase

  task_rules.each do |skill_id, keywords|
    keywords = [keywords] unless keywords.is_a?(Array)
    keywords.each do |keyword|
      return skill_id if task_lower.include?(keyword.downcase)
    end
  end

  nil
end

#routing_enabled?Boolean

Check if routing is enabled

Returns:

  • (Boolean)

    true if routing is configured



105
106
107
# File 'lib/aidp/skills/router.rb', line 105

def routing_enabled?
  config.dig("skills", "routing", "enabled") == true
end

#rulesHash

Get all routing rules

Returns:

  • (Hash)

    Hash containing path_rules, task_rules, and combined_rules



119
120
121
122
123
124
125
# File 'lib/aidp/skills/router.rb', line 119

def rules
  {
    path_rules: config.dig("skills", "routing", "path_rules") || {},
    task_rules: config.dig("skills", "routing", "task_rules") || {},
    combined_rules: config.dig("skills", "routing", "combined_rules") || {}
  }
end