aia 2.0.0.0.pre.beta2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. checksums.yaml +4 -4
  2. data/.version +1 -1
  3. data/CHANGELOG.md +146 -228
  4. data/README.md +41 -18
  5. data/bin/aia +8 -3
  6. data/lib/aia/aia_completion.bash +2 -3
  7. data/lib/aia/aia_completion.fish +6 -5
  8. data/lib/aia/aia_completion.zsh +2 -3
  9. data/lib/aia/chat_loop.rb +16 -14
  10. data/lib/aia/config/cli_parser.rb +123 -91
  11. data/lib/aia/config/defaults.yml +12 -39
  12. data/lib/aia/config/mcp_parser.rb +30 -36
  13. data/lib/aia/config/model_spec.rb +8 -14
  14. data/lib/aia/config/validator.rb +102 -103
  15. data/lib/aia/config/value_caster.rb +76 -0
  16. data/lib/aia/config.rb +70 -53
  17. data/lib/aia/crew.rb +29 -19
  18. data/lib/aia/debate_handler.rb +27 -10
  19. data/lib/aia/directives/configuration_directives.rb +55 -14
  20. data/lib/aia/directives/context_directives.rb +24 -12
  21. data/lib/aia/directives/execution_directives.rb +18 -23
  22. data/lib/aia/directives/model_directives.rb +41 -104
  23. data/lib/aia/directives/utility_directives.rb +16 -17
  24. data/lib/aia/directives/web_and_file_directives.rb +21 -14
  25. data/lib/aia/fact_asserter.rb +3 -3
  26. data/lib/aia/history_transfer.rb +64 -24
  27. data/lib/aia/logger.rb +10 -45
  28. data/lib/aia/mcp_config_normalizer.rb +27 -14
  29. data/lib/aia/mcp_connection_manager.rb +181 -184
  30. data/lib/aia/mcp_discovery.rb +43 -15
  31. data/lib/aia/mcp_grouper.rb +2 -1
  32. data/lib/aia/mcp_server_config.rb +20 -2
  33. data/lib/aia/mcp_utility.rb +24 -19
  34. data/lib/aia/mention_router.rb +15 -2
  35. data/lib/aia/modality_query.rb +36 -0
  36. data/lib/aia/model_alias_registry.rb +11 -11
  37. data/lib/aia/model_registry.rb +75 -0
  38. data/lib/aia/network_builder.rb +8 -7
  39. data/lib/aia/patches/ruby_llm_streaming_error.rb +21 -4
  40. data/lib/aia/patches/ruby_llm_tool_param_compat.rb +18 -0
  41. data/lib/aia/pipeline_orchestrator.rb +71 -38
  42. data/lib/aia/prompt_decomposer.rb +1 -1
  43. data/lib/aia/prompt_handler.rb +56 -29
  44. data/lib/aia/provider_registry.rb +105 -0
  45. data/lib/aia/robot_builder.rb +1 -1
  46. data/lib/aia/robot_factory.rb +67 -63
  47. data/lib/aia/robot_sync.rb +79 -0
  48. data/lib/aia/role_catalog.rb +89 -0
  49. data/lib/aia/session.rb +25 -11
  50. data/lib/aia/skill_utils.rb +60 -19
  51. data/lib/aia/spawn_handler.rb +16 -4
  52. data/lib/aia/spawn_spec_parser.rb +6 -7
  53. data/lib/aia/special_mode_handler.rb +28 -25
  54. data/lib/aia/speech.rb +70 -34
  55. data/lib/aia/startup_coordinator.rb +17 -92
  56. data/lib/aia/streaming_runner.rb +10 -2
  57. data/lib/aia/system_prompt_assembler.rb +12 -11
  58. data/lib/aia/task_board.rb +60 -0
  59. data/lib/aia/tool_approver.rb +205 -0
  60. data/lib/aia/tool_filter.rb +1 -10
  61. data/lib/aia/tool_filter_strategy.rb +12 -1
  62. data/lib/aia/tool_introspection.rb +9 -1
  63. data/lib/aia/tool_loader.rb +67 -33
  64. data/lib/aia/tool_utility.rb +6 -5
  65. data/lib/aia/tools/delegate_to_foreman_tool.rb +6 -4
  66. data/lib/aia/tools/recruit_robot_tool.rb +12 -12
  67. data/lib/aia/tools/reskill_robot_tool.rb +7 -7
  68. data/lib/aia/tools/task_board_tool.rb +16 -16
  69. data/lib/aia/trakflow_bridge.rb +14 -8
  70. data/lib/aia/turn_state.rb +31 -8
  71. data/lib/aia/ui_presenter.rb +16 -0
  72. data/lib/aia/utility.rb +6 -24
  73. data/lib/aia/variable_input_collector.rb +0 -3
  74. data/lib/aia/verification_network.rb +9 -10
  75. data/lib/aia.rb +33 -8
  76. metadata +19 -160
  77. data/.better-commits.json +0 -150
  78. data/.config/tocer/configuration.yml +0 -6
  79. data/.envrc +0 -19
  80. data/.loki +0 -11
  81. data/.reek.yml +0 -160
  82. data/.rubocop.yml +0 -116
  83. data/.rubocop_strict.yml +0 -15
  84. data/COMMITS.md +0 -196
  85. data/IMPLEMENTATION_PLAN.md +0 -506
  86. data/Rakefile +0 -47
  87. data/_typos.toml +0 -10
  88. data/aia.gemspec +0 -92
  89. data/architecture_review.md +0 -314
  90. data/config/aia.yml +0 -13
  91. data/docs/AGENTS.md +0 -40
  92. data/docs/advanced-prompting.md +0 -814
  93. data/docs/assets/images/aia.png +0 -0
  94. data/docs/cli-reference.md +0 -1241
  95. data/docs/configuration.md +0 -747
  96. data/docs/contributing.md +0 -386
  97. data/docs/directives-reference.md +0 -1145
  98. data/docs/examples/index.md +0 -277
  99. data/docs/examples/mcp/index.md +0 -475
  100. data/docs/examples/prompts/analysis/index.md +0 -78
  101. data/docs/examples/prompts/automation/index.md +0 -109
  102. data/docs/examples/prompts/development/index.md +0 -125
  103. data/docs/examples/prompts/index.md +0 -333
  104. data/docs/examples/prompts/learning/index.md +0 -127
  105. data/docs/examples/prompts/writing/index.md +0 -62
  106. data/docs/examples/tools/index.md +0 -282
  107. data/docs/faq.md +0 -578
  108. data/docs/guides/available-models.md +0 -366
  109. data/docs/guides/basic-usage.md +0 -478
  110. data/docs/guides/chat.md +0 -680
  111. data/docs/guides/crew.md +0 -239
  112. data/docs/guides/executable-prompts.md +0 -331
  113. data/docs/guides/first-prompt.md +0 -454
  114. data/docs/guides/getting-started.md +0 -455
  115. data/docs/guides/image-generation.md +0 -507
  116. data/docs/guides/index.md +0 -48
  117. data/docs/guides/local-models.md +0 -306
  118. data/docs/guides/migrate-prompts.md +0 -335
  119. data/docs/guides/models.md +0 -1017
  120. data/docs/guides/shared-tools.md +0 -254
  121. data/docs/guides/tools.md +0 -831
  122. data/docs/index.md +0 -259
  123. data/docs/installation.md +0 -274
  124. data/docs/mcp-integration.md +0 -603
  125. data/docs/prompt_management.md +0 -665
  126. data/docs/security.md +0 -674
  127. data/docs/special_projects_guide.md +0 -386
  128. data/docs/tools-and-mcp-examples.md +0 -1211
  129. data/docs/workflows-and-pipelines.md +0 -636
  130. data/examples/.gitignore +0 -2
  131. data/examples/00_setup_aia.sh +0 -111
  132. data/examples/01_basic_usage.sh +0 -23
  133. data/examples/02_frontmatter.sh +0 -33
  134. data/examples/03_shell_integration.sh +0 -55
  135. data/examples/04_erb_templating.sh +0 -56
  136. data/examples/05_shell_then_erb.sh +0 -38
  137. data/examples/06_prompt_chaining.sh +0 -59
  138. data/examples/07_pipeline.sh +0 -46
  139. data/examples/08_context_files.sh +0 -43
  140. data/examples/09_roles.sh +0 -67
  141. data/examples/10_stdin_piping.sh +0 -49
  142. data/examples/11_multi_model.sh +0 -65
  143. data/examples/12_token_usage.sh +0 -52
  144. data/examples/13_cost_tracking.sh +0 -68
  145. data/examples/14_output_file.sh +0 -91
  146. data/examples/15_parameters.sh +0 -68
  147. data/examples/16_directives.sh +0 -113
  148. data/examples/17_require_and_conditionals.sh +0 -68
  149. data/examples/18_tools.sh +0 -61
  150. data/examples/19_local_tools.sh +0 -34
  151. data/examples/20_mcp_servers.sh +0 -102
  152. data/examples/21_executable_prompts.sh +0 -78
  153. data/examples/22_chat_mode.sh +0 -217
  154. data/examples/23_verify.sh +0 -139
  155. data/examples/24_decompose.sh +0 -139
  156. data/examples/25_spawn.sh +0 -139
  157. data/examples/26_debate.sh +0 -97
  158. data/examples/27_mention_routing.sh +0 -157
  159. data/examples/28_model_switching.sh +0 -106
  160. data/examples/29_agent_harness.sh +0 -177
  161. data/examples/README.md +0 -321
  162. data/examples/advanced_multi_robot_capabilities_without_examples.md +0 -106
  163. data/examples/aia_config.yml +0 -22
  164. data/examples/aia_config_orchestrator.yml +0 -45
  165. data/examples/aia_config_with_mcp.yml +0 -21
  166. data/examples/common.sh +0 -39
  167. data/examples/context/project_goals.md +0 -6
  168. data/examples/context/tech_stack.md +0 -7
  169. data/examples/directives/timestamp_directive.rb +0 -22
  170. data/examples/mcp/filesystem.json +0 -9
  171. data/examples/prompts_dir/analyze.md +0 -2
  172. data/examples/prompts_dir/brainstorm.md +0 -2
  173. data/examples/prompts_dir/conditional.md +0 -17
  174. data/examples/prompts_dir/creative.md +0 -4
  175. data/examples/prompts_dir/erbinfo.md +0 -5
  176. data/examples/prompts_dir/erbinfo_raw.md +0 -8
  177. data/examples/prompts_dir/evaluate.md +0 -2
  178. data/examples/prompts_dir/explain_joke.md +0 -1
  179. data/examples/prompts_dir/explain_recursion.md +0 -1
  180. data/examples/prompts_dir/fun_fact +0 -3
  181. data/examples/prompts_dir/greeting.md +0 -8
  182. data/examples/prompts_dir/greeting_required.md +0 -7
  183. data/examples/prompts_dir/hello.md +0 -1
  184. data/examples/prompts_dir/includes/ruby_facts.md +0 -4
  185. data/examples/prompts_dir/limerick.md +0 -1
  186. data/examples/prompts_dir/list_with_mcp.md +0 -3
  187. data/examples/prompts_dir/pick_best.md +0 -2
  188. data/examples/prompts_dir/project_summary +0 -12
  189. data/examples/prompts_dir/quiz_with_include.md +0 -6
  190. data/examples/prompts_dir/require_demo.md +0 -5
  191. data/examples/prompts_dir/roles/formal.md +0 -3
  192. data/examples/prompts_dir/roles/orchestrator.md +0 -21
  193. data/examples/prompts_dir/roles/pirate.md +0 -3
  194. data/examples/prompts_dir/shell_then_erb.md +0 -6
  195. data/examples/prompts_dir/sort_compare.md +0 -2
  196. data/examples/prompts_dir/summarize_context.md +0 -2
  197. data/examples/prompts_dir/sysinfo.md +0 -5
  198. data/examples/prompts_dir/sysinfo_raw.md +0 -8
  199. data/examples/prompts_dir/tagline.md +0 -1
  200. data/examples/prompts_dir/tell_joke.md +0 -1
  201. data/examples/prompts_dir/tell_joke_with_next.md +0 -4
  202. data/examples/prompts_dir/use_local_tool.md +0 -8
  203. data/examples/prompts_dir/use_tools.md +0 -8
  204. data/examples/prompts_dir/weather.md +0 -2
  205. data/examples/prompts_dir/whats_new.md +0 -2
  206. data/examples/prompts_dir/with_timestamp.md +0 -5
  207. data/examples/requirements/sinatra_taskflow_app.md +0 -139
  208. data/examples/rules/01_classify_ruby.rb +0 -16
  209. data/examples/rules/02_prefer_claude_for_code.rb +0 -19
  210. data/examples/rules/03_gate_prompt_length.rb +0 -19
  211. data/examples/rules/04_tool_selection.rb +0 -41
  212. data/examples/rules/README.md +0 -30
  213. data/examples/run_all.sh +0 -294
  214. data/examples/tools/word_count_tool.rb +0 -30
  215. data/lib/AGENTS.md +0 -57
  216. data/lib/aia/model_switch_handler.rb +0 -100
  217. data/mcp_servers/README.md +0 -110
  218. data/mcp_servers/apple-mcp.json +0 -8
  219. data/mcp_servers/filesystem.json +0 -9
  220. data/mcp_servers/mcp_server_chart.json +0 -11
  221. data/mcp_servers/playwright_one.json +0 -8
  222. data/mcp_servers/playwright_server_definition.json +0 -9
  223. data/mcp_servers/playwright_two.json +0 -8
  224. data/mcp_servers/tavily_mcp_server.json +0 -8
  225. data/mkdocs.yml +0 -169
@@ -8,12 +8,6 @@ module AIA
8
8
  class PromptHandler
9
9
  include AIA::SkillUtils
10
10
 
11
- # Struct for path-based role content (bypasses PM parsing)
12
- RoleContent = Struct.new(:content) do
13
- def to_s = content
14
- def metadata = nil
15
- end
16
-
17
11
  # Root-level YAML keys that are shorthands for deeper config paths
18
12
  SHORTHAND_KEYS = %w[model temperature top_p next pipeline shell erb].freeze
19
13
 
@@ -30,15 +24,35 @@ module AIA
30
24
 
31
25
  def initialize
32
26
  @prompts_dir = AIA.config.prompts.dir
33
- @roles_dir = AIA.config.prompts.roles_dir
34
27
 
28
+ flags = AIA.config.flags
35
29
  PM.configure do |c|
36
30
  c.prompts_dir = @prompts_dir
31
+ # flags.shell / flags.erb are the defaults; a file's front matter can still override them
32
+ c.shell = self.class.flag_enabled?(flags, :shell)
33
+ c.erb = self.class.flag_enabled?(flags, :erb)
37
34
  end
38
35
 
39
36
  register_pm_directives
40
37
  end
41
38
 
39
+ # A shell/erb flag is on unless it is explicitly false.
40
+ #
41
+ # @param flags [#shell, #erb, nil]
42
+ # @param name [Symbol] :shell or :erb
43
+ # @return [Boolean]
44
+ def self.flag_enabled?(flags, name)
45
+ return true unless flags.respond_to?(name)
46
+
47
+ flags.public_send(name) != false
48
+ end
49
+
50
+ # A pipeline from front matter: a list, or a comma-separated String
51
+ # ("a, b"), as a list of prompt IDs.
52
+ def self.pipeline_list(value)
53
+ Array(value).flat_map { it.to_s.split(',') }.map(&:strip).reject(&:empty?)
54
+ end
55
+
42
56
  def fetch_prompt(prompt_id)
43
57
  if prompt_id == '__FUZZY_SEARCH__'
44
58
  return fuzzy_search_prompt('')
@@ -48,10 +62,10 @@ module AIA
48
62
  return fetch_executable_prompt
49
63
  end
50
64
 
51
- prompt_file_path = File.join(@prompts_dir, "#{prompt_id}#{AIA.config.prompts.extname}")
65
+ prompt_file_path = prompt_path(prompt_id)
52
66
 
53
67
  parsed = if File.exist?(prompt_file_path)
54
- PM.parse(prompt_id)
68
+ parse_prompt_file(prompt_id)
55
69
  else
56
70
  $stderr.puts "Warning: Invalid prompt ID or file not found: #{prompt_id}"
57
71
  logger.warn("Invalid prompt ID or file not found: #{prompt_id}")
@@ -74,9 +88,7 @@ module AIA
74
88
  role_file_path = File.join(@prompts_dir, "#{role_id}#{prompts_cfg.extname}")
75
89
 
76
90
  parsed = if File.exist?(role_file_path)
77
- # PM.parse(role_id) cannot resolve subdirectory IDs like "roles/jersey_mike"
78
- # and returns the ID string as content. Parse from raw file content instead.
79
- PM.parse_string(File.read(role_file_path))
91
+ parse_prompt_file(role_id)
80
92
  else
81
93
  $stderr.puts "Warning: Invalid role ID or file not found: #{role_id}"
82
94
  logger.warn("Invalid role ID or file not found: #{role_id}")
@@ -133,7 +145,7 @@ module AIA
133
145
 
134
146
  def fetch_executable_prompt
135
147
  content = AIA.config.executable_prompt_content
136
- parsed = PM.parse(content)
148
+ parsed = PM.parse_string(content)
137
149
  apply_metadata_config(parsed) if parsed
138
150
  parsed
139
151
  end
@@ -177,9 +189,9 @@ module AIA
177
189
  def apply_root_shorthands(meta_hash)
178
190
  cfg = AIA.config
179
191
 
180
- # model → cfg.models (replace with single-model array)
192
+ # model → cfg.models (replace; a String or list becomes ModelSpecs)
181
193
  model_val = shorthand(meta_hash, :model)
182
- cfg.models = [model_val] if model_val
194
+ cfg.models = Config::TO_MODEL_SPECS.call(Array(model_val)) if model_val
183
195
 
184
196
  temp_val = shorthand(meta_hash, :temperature)
185
197
  cfg.llm.temperature = temp_val if temp_val
@@ -189,10 +201,10 @@ module AIA
189
201
 
190
202
  # next / pipeline → cfg.pipeline (replace)
191
203
  next_val = shorthand(meta_hash, :next)
192
- apply_pipeline_shorthand(cfg, [next_val], "next: #{next_val}") if next_val
204
+ apply_pipeline_shorthand(cfg, next_val, "next: #{next_val}") if next_val
193
205
 
194
206
  pipeline_val = shorthand(meta_hash, :pipeline)
195
- apply_pipeline_shorthand(cfg, Array(pipeline_val), "pipeline") if pipeline_val
207
+ apply_pipeline_shorthand(cfg, pipeline_val, "pipeline") if pipeline_val
196
208
 
197
209
  # shell / erb → cfg.flags (and PM's shell/erb via metadata)
198
210
  shell_val = shorthand(meta_hash, :shell)
@@ -212,7 +224,7 @@ module AIA
212
224
  if cfg.pipeline.any?
213
225
  logger.info "Prompt metadata '#{label}' overrides remaining pipeline #{cfg.pipeline.inspect}"
214
226
  end
215
- cfg.pipeline = new_pipeline
227
+ cfg.pipeline = self.class.pipeline_list(new_pipeline)
216
228
  end
217
229
 
218
230
  def logger
@@ -230,6 +242,10 @@ module AIA
230
242
  if sub_config
231
243
  deep_merge_into_config(sub_config, value)
232
244
  end
245
+ elsif key == :pipeline
246
+ target.pipeline = self.class.pipeline_list(value)
247
+ elsif key == :models
248
+ target.models = Config::TO_MODEL_SPECS.call(Array(value))
233
249
  elsif target.respond_to?(:"#{key}=")
234
250
  target.send(:"#{key}=", value)
235
251
  end
@@ -286,8 +302,7 @@ module AIA
286
302
  end
287
303
 
288
304
  def fetch_role_from_path(role_id)
289
- expanded = File.expand_path(role_id)
290
- expanded += '.md' if File.extname(expanded).empty?
305
+ expanded = prompt_path(role_id)
291
306
 
292
307
  unless File.exist?(expanded)
293
308
  $stderr.puts "Warning: Role file not found at path: #{expanded}"
@@ -295,7 +310,9 @@ module AIA
295
310
  return handle_missing_role(role_id)
296
311
  end
297
312
 
298
- RoleContent.new(File.read(expanded))
313
+ parsed = PM.parse(Pathname.new(expanded))
314
+ apply_metadata_config(parsed)
315
+ parsed
299
316
  end
300
317
 
301
318
  def fuzzy_search_prompt(prompt_id)
@@ -305,7 +322,7 @@ module AIA
305
322
  raise "Error: Could not find prompt with ID: #{prompt_id} even with fuzzy search"
306
323
  end
307
324
 
308
- PM.parse(new_prompt_id)
325
+ parse_prompt_file(new_prompt_id)
309
326
  end
310
327
 
311
328
  def handle_missing_role(role_id)
@@ -329,7 +346,19 @@ module AIA
329
346
  raise "Error: Could not find role with ID: #{role_id} even with fuzzy search"
330
347
  end
331
348
 
332
- PM.parse(new_role_id)
349
+ parse_prompt_file(new_role_id)
350
+ end
351
+
352
+ # Full path of the prompt file for +prompt_id+ (relative to the prompts dir).
353
+ def prompt_path(prompt_id)
354
+ SkillUtils.prompt_file_path(prompt_id, dir: @prompts_dir, extname: AIA.config.prompts.extname)
355
+ end
356
+
357
+ # Parse the prompt file for +prompt_id+. PM.parse only treats a single
358
+ # \w+ word as a file name, so IDs such as "code-review" or "dev/review"
359
+ # would be parsed as literal text; a Pathname is always read as a file.
360
+ def parse_prompt_file(prompt_id)
361
+ PM.parse(Pathname.new(prompt_path(prompt_id)))
333
362
  end
334
363
 
335
364
  def search_prompt_id_with_fzf(initial_query)
@@ -347,11 +376,11 @@ module AIA
347
376
 
348
377
  def search_role_id_with_fzf(initial_query)
349
378
  prompts_cfg = AIA.config.prompts
350
- role_files = Dir.glob(File.join(@roles_dir, "*#{prompts_cfg.extname}"))
351
- .map { |file| File.basename(file, prompts_cfg.extname) }
379
+ prefix = prompts_cfg.roles_prefix
380
+ role_files = RoleCatalog.names(@prompts_dir, prefix, extname: prompts_cfg.extname)
352
381
  fzf = AIA::Fzf.new(
353
382
  list: role_files,
354
- directory: @prompts_dir,
383
+ directory: File.join(@prompts_dir, prefix.to_s),
355
384
  query: initial_query,
356
385
  subject: 'Role IDs',
357
386
  prompt: 'Select a role ID:'
@@ -363,9 +392,7 @@ module AIA
363
392
  raise "No role ID selected"
364
393
  end
365
394
 
366
- unless role.start_with?(prompts_cfg.roles_prefix)
367
- role = prompts_cfg.roles_prefix + '/' + role
368
- end
395
+ role = "#{prefix}/#{role}" unless role.start_with?(prefix)
369
396
 
370
397
  role
371
398
  end
@@ -0,0 +1,105 @@
1
+ # lib/aia/provider_registry.rb
2
+ #
3
+ # Generic gateway to RubyLLM's provider registry (ruby_llm >= 2.0).
4
+ # AIA hardcodes no provider names: a model prefix counts as a provider
5
+ # exactly when RubyLLM has it registered — built-in providers, provider gems
6
+ # the user loaded (e.g. `aia --require ruby_llm-providers-xyzzy`), or a gem
7
+ # following the conventional `ruby_llm/providers/<slug>` require path that
8
+ # can be loaded on demand the first time its slug is seen.
9
+
10
+ module AIA
11
+ module ProviderRegistry
12
+ module_function
13
+
14
+ # All registered provider slugs, as strings.
15
+ def slugs
16
+ RubyLLM::Provider.providers.keys.map(&:to_s)
17
+ end
18
+
19
+ # Registered providers as {slug (String) => provider class}.
20
+ def providers
21
+ RubyLLM::Provider.providers.transform_keys(&:to_s)
22
+ end
23
+
24
+ # True when RubyLLM already has this slug registered.
25
+ def registered?(slug)
26
+ return false unless slug
27
+
28
+ RubyLLM::Provider.providers.key?(slug.to_sym)
29
+ end
30
+
31
+ # True when the slug names a registered provider, loading the
32
+ # conventional provider gem on demand the first time it is seen.
33
+ def available?(slug)
34
+ registered?(slug) || load(slug)
35
+ end
36
+
37
+ # Attempt to require the conventional gem path for a provider slug.
38
+ # Returns true when the provider ends up registered. Failed lookups are
39
+ # cached so repeated non-provider prefixes (model ids also use "/")
40
+ # don't pay the require cost every time.
41
+ def load(slug)
42
+ slug = slug.to_s
43
+ return false unless slug.match?(/\A[a-z0-9_]+\z/)
44
+
45
+ @load_attempts ||= {}
46
+ return @load_attempts[slug] if @load_attempts.key?(slug)
47
+
48
+ @load_attempts[slug] =
49
+ begin
50
+ require "ruby_llm/providers/#{slug}"
51
+ registered?(slug)
52
+ rescue LoadError
53
+ false
54
+ end
55
+ end
56
+
57
+ # A provider instance ready for API calls (model listing etc.).
58
+ # Raises RubyLLM's own errors for unknown or unconfigured providers.
59
+ def instantiate(slug)
60
+ RubyLLM::Provider.resolve!(slug.to_sym).new(RubyLLM.config)
61
+ end
62
+
63
+ # Slugs of conventionally named provider gems visible to the bundle
64
+ # (ruby_llm-providers-<slug>), whether or not they are loaded yet.
65
+ def installed_gem_slugs
66
+ Gem::Specification.filter_map do |spec|
67
+ spec.name.delete_prefix('ruby_llm-providers-') if spec.name.start_with?('ruby_llm-providers-')
68
+ end.uniq
69
+ end
70
+
71
+ # Register every installed conventional provider gem, so a full listing
72
+ # reflects what is available, not just what has been referenced so far.
73
+ def load_installed_gems
74
+ installed_gem_slugs.each { |slug| load(slug) }
75
+ end
76
+
77
+ # Endpoints for local servers that RubyLLM leaves unset, so they work
78
+ # without an environment variable when running on their usual port.
79
+ DEFAULT_ENDPOINTS = { 'ollama' => 'http://localhost:11434/v1' }.freeze
80
+
81
+ # Generic env-var endpoint overrides: for every registered provider,
82
+ # <SLUG>_API_BASE sets RubyLLM.config.<slug>_api_base when the provider
83
+ # defines that option (RubyLLM registers the accessor with the provider).
84
+ # A provider in DEFAULT_ENDPOINTS with no endpoint set gets its default.
85
+ def apply_env_endpoints
86
+ RubyLLM.configure do |c|
87
+ slugs.each do |slug|
88
+ setter = :"#{slug}_api_base="
89
+ next unless c.respond_to?(setter)
90
+
91
+ value = ENV.fetch("#{slug.upcase}_API_BASE", nil) || default_endpoint(c, slug)
92
+ c.public_send(setter, value) if value
93
+ end
94
+ end
95
+ end
96
+
97
+ # The default endpoint for +slug+ when RubyLLM has none configured.
98
+ def default_endpoint(ruby_llm_config, slug)
99
+ getter = :"#{slug}_api_base"
100
+ return nil if ruby_llm_config.respond_to?(getter) && ruby_llm_config.public_send(getter)
101
+
102
+ DEFAULT_ENDPOINTS[slug.to_s]
103
+ end
104
+ end
105
+ end
@@ -27,7 +27,7 @@ module AIA
27
27
  name: robot_name,
28
28
  system_prompt: system_prompt,
29
29
  local_tools: ToolLoader.filtered_tools(config),
30
- mcp_servers: RobotFactory.mcp_server_configs(config),
30
+ mcp: RobotFactory.mcp_servers(config),
31
31
  on_content: nil,
32
32
  config: RobotFactory.build_run_config(config)
33
33
  )
@@ -74,24 +74,6 @@ module AIA
74
74
  new_robot
75
75
  end
76
76
 
77
- # --- Forwarding wrappers for backward compatibility ---
78
-
79
- def clear_tool_cache!
80
- ToolLoader.clear_cache!
81
- end
82
-
83
- def filtered_tools(config)
84
- ToolLoader.filtered_tools(config)
85
- end
86
-
87
- def resolve_system_prompt(config, model_spec = nil)
88
- SystemPromptAssembler.resolve_system_prompt(config, model_spec)
89
- end
90
-
91
- def build_identity_prompt(robot_name, spec, roster)
92
- SystemPromptAssembler.build_identity_prompt(robot_name, spec, roster)
93
- end
94
-
95
77
  # Build a concurrent MCP network where independent server groups
96
78
  # run in parallel with a synthesizer to merge results.
97
79
  #
@@ -118,7 +100,7 @@ module AIA
118
100
  cached = ToolLoader.cached_tools
119
101
  if cached
120
102
  config.loaded_tools = cached
121
- config.tool_names = cached.map { |t| t.respond_to?(:name) ? t.name : t.class.name }.join(', ')
103
+ config.tool_names = cached.map { |t| ToolIntrospection.tool_name(t) }.join(', ')
122
104
  else
123
105
  ToolLoader.load_tools(config)
124
106
  end
@@ -143,8 +125,21 @@ module AIA
143
125
  # - max_tokens remains a RobotLab::RunConfig field; AIA normalizes it at
144
126
  # the RubyLLM/OpenAI provider boundary for models that need
145
127
  # max_completion_tokens
146
- # :reek:TooManyStatements -- one guarded param per LLM tuning knob, with provider-specific max_tokens handling
147
128
  def build_run_config(config)
129
+ params = llm_params(config)
130
+ params.merge!(ToolApprover.options(config))
131
+
132
+ RobotLab::RunConfig.new(**params)
133
+ end
134
+
135
+ # The LLM tuning parameters (temperature, max_tokens, top_p, penalties)
136
+ # for the current config, with unsupported or default-only values left out.
137
+ # Shared by build_run_config and RobotSync, which retunes a running robot.
138
+ #
139
+ # @param config [AIA::Config]
140
+ # @return [Hash{Symbol => Numeric}]
141
+ # :reek:TooManyStatements -- one guarded param per LLM tuning knob, with provider-specific max_tokens handling
142
+ def llm_params(config)
148
143
  model_spec = config.models.first
149
144
  llm = config.llm
150
145
  params = {}
@@ -171,7 +166,7 @@ module AIA
171
166
  pp = llm.presence_penalty
172
167
  params[:presence_penalty] = pp if pp && pp != 0.0
173
168
 
174
- RobotLab::RunConfig.new(**params)
169
+ params
175
170
  end
176
171
 
177
172
  # Returns false for models that explicitly declare temperature: false in the
@@ -184,17 +179,26 @@ module AIA
184
179
  true
185
180
  end
186
181
 
187
- # Normalize all configured MCP server configs for robot_lab.
188
- # Filtering/selection is MCPDiscovery's responsibility — this just normalizes shape.
189
- def mcp_server_configs(config)
182
+ # The MCP servers to hand a robot as RobotLab.build(mcp:).
183
+ #
184
+ # Once MCPConnectionManager.start has run, these are the live
185
+ # RubyLLM::MCP instances it connected, shared by every robot so each
186
+ # server is started once; servers that failed to connect are left out.
187
+ # Before that (or without a manager) they are normalized config Hashes,
188
+ # which robot_lab connects lazily on the robot's first run.
189
+ #
190
+ # @param config [AIA::Config]
191
+ # @param group [Array<Hash>, nil] a subset of server configs (concurrent MCP
192
+ # networks); defaults to MCPDiscovery's selection (--mcp-use/--mcp-skip)
193
+ # @return [Array<RubyLLM::MCP, Hash>]
194
+ def mcp_servers(config, group = nil)
190
195
  return [] if config.flags.no_mcp
191
- Array(config.mcp_servers).map { |s| MCPConfigNormalizer.normalize(s) }
192
- end
193
196
 
194
- # Normalize a single MCP server config to robot_lab's nested transport format.
195
- # Delegates to MCPConfigNormalizer.
196
- def normalize_mcp_config(server)
197
- MCPConfigNormalizer.normalize(server)
197
+ configs = group || MCPDiscovery.new.discover(config)
198
+ manager = AIA.mcp_manager
199
+ return manager.servers_for(configs) if manager&.connected?
200
+
201
+ configs.map { |s| MCPConfigNormalizer.normalize(s) }
198
202
  end
199
203
 
200
204
  # Initialize shared memory for a network with session context.
@@ -230,24 +234,39 @@ module AIA
230
234
 
231
235
  # Build a RobotLab::Robot for a model spec, applying provider resolution.
232
236
  # Centralizes the "set provider if the spec has one, then build" tail that
233
- # every robot/network builder needs, so local-provider routing (ollama/lms)
234
- # can't be forgotten at a new build site.
237
+ # every robot/network builder needs, so provider routing can't be
238
+ # forgotten at a new build site.
235
239
  #
236
240
  # @param spec [AIA::Config::ModelSpec] the model spec (supplies model: and provider)
237
241
  # @param opts [Hash] forwarded to RobotLab.build (name:, system_prompt:,
238
- # local_tools:, mcp_servers:, config:, on_content:, ...). Do NOT pass model:.
242
+ # local_tools:, mcp:, config:, on_content:, ...). Do NOT pass model:.
239
243
  # @return [RobotLab::Robot]
240
244
  def build_robot(spec, **opts)
241
245
  opts[:provider] = resolve_provider(spec) if spec.provider
242
246
  RobotLab.build(model: spec.name, **opts)
243
247
  end
244
248
 
245
- # Map AIA provider aliases to RubyLLM provider slugs.
249
+ # Resolve a spec's provider to a RubyLLM provider slug. Slugs pass
250
+ # through unchanged — RubyLLM's registry is the single source of truth —
251
+ # but the method stays as the seam where optional provider gems get
252
+ # loaded on demand and where a future alias mapping would live.
246
253
  def resolve_provider(model_spec)
247
- case model_spec.provider
248
- when 'lms' then 'openai'
249
- else model_spec.provider
250
- end
254
+ provider = model_spec.provider
255
+ ensure_provider_loaded(provider)
256
+ provider
257
+ end
258
+
259
+ # Make sure a provider slug is registered with RubyLLM, loading its
260
+ # conventional gem on demand. An unknown provider only warns here —
261
+ # RubyLLM raises its own clear error later when the slug is truly
262
+ # unresolvable.
263
+ def ensure_provider_loaded(provider)
264
+ return if provider.nil? || ProviderRegistry.available?(provider)
265
+
266
+ AIA.debug_warn(
267
+ "provider '#{provider}' is not registered with RubyLLM and no " \
268
+ "ruby_llm/providers/#{provider} library could be loaded"
269
+ )
251
270
  end
252
271
 
253
272
  private
@@ -256,7 +275,6 @@ module AIA
256
275
  def configure_robot_lab(config)
257
276
  # Configure loggers before any API calls
258
277
  AIA::LoggerManager.configure_llm_logger
259
- AIA::LoggerManager.configure_mcp_logger
260
278
 
261
279
  # Route RobotLab's internal logging through AIA's MCP logger
262
280
  # so entries go to the configured log files instead of STDOUT.
@@ -276,6 +294,9 @@ module AIA
276
294
  # Configure local provider API endpoints from environment variables
277
295
  configure_local_providers(config)
278
296
 
297
+ # Use AIA's models.json, refreshing it every registry.refresh days
298
+ ModelRegistry.prepare(config)
299
+
279
300
  # Wire audio model settings into RubyLLM config
280
301
  configure_audio(config)
281
302
  end
@@ -291,31 +312,14 @@ module AIA
291
312
  RubyLLM.configure { |c| c.default_transcription_model = transcription_model }
292
313
  end
293
314
 
294
- # Set API base URLs for local providers (Ollama, LM Studio) so
295
- # RubyLLM can reach them. Reads from standard env vars.
315
+ # Make every provider named by the configured models available, then
316
+ # apply generic env-var endpoint overrides: <SLUG>_API_BASE sets
317
+ # RubyLLM.config.<slug>_api_base for every registered provider that
318
+ # defines the option. Providers ship their own endpoint defaults, so
319
+ # nothing is set unless the user overrides it.
296
320
  def configure_local_providers(config)
297
- providers_used = config.models.map(&:provider).compact.uniq
298
-
299
- RubyLLM.configure do |c|
300
- # Always configure Ollama's base so switching to an `ollama/` model
301
- # mid-session works even when the session started on a cloud model.
302
- # It only affects the Ollama provider, so it's harmless when unused.
303
- # RubyLLM's Ollama provider subclasses OpenAI and appends
304
- # `chat/completions`, so the base must include the `/v1` suffix.
305
- c.ollama_api_base = ENV.fetch('OLLAMA_API_BASE', 'http://localhost:11434/v1')
306
-
307
- # LM Studio exposes an OpenAI-compatible API and reuses the OpenAI
308
- # provider, so only override the OpenAI base when an `lms/` model is
309
- # actually in use — otherwise it would hijack real OpenAI requests.
310
- # The OpenAI provider POSTs the relative `chat/completions`, so (like
311
- # the real `https://api.openai.com/v1` default and the Ollama base) the
312
- # LM Studio base must include the `/v1` suffix — otherwise requests hit
313
- # `:1234/chat/completions`, LM Studio 404s, and ruby_llm's streaming
314
- # error handler masks it as `delete_prefix for nil`.
315
- if providers_used.include?('lms')
316
- c.openai_api_base = ENV.fetch('LMS_API_BASE', 'http://localhost:1234/v1')
317
- end
318
- end
321
+ config.models.map(&:provider).compact.uniq.each { |provider| ensure_provider_loaded(provider) }
322
+ ProviderRegistry.apply_env_endpoints
319
323
  end
320
324
 
321
325
  # Decide between consensus and parallel multi-model.
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ # lib/aia/robot_sync.rb
4
+ #
5
+ # The robot (or crew) is built once from the configuration at startup, but a
6
+ # prompt's front matter (model:, temperature:, top_p:, config: llm: ...) and
7
+ # chat directives (/temperature, /top_p, /config llm.*) change the
8
+ # configuration later. RobotSync runs before each prompt and chat turn and
9
+ # brings the running client in line with the configuration:
10
+ #
11
+ # * the model list changed -> rebuild the client (as /model does), carrying
12
+ # the conversation over per model_switch_history
13
+ # * only LLM settings changed -> retune every robot in place, keeping history
14
+ #
15
+ # A client replaced elsewhere (e.g. by /model) becomes the new baseline.
16
+
17
+ module AIA
18
+ class RobotSync
19
+ # @param config [AIA::Config] the live configuration
20
+ # @param client [RobotLab::Robot, RobotLab::Network, nil] the client as built
21
+ # @param factory [#rebuild, #llm_params] builds and parameterizes clients
22
+ def initialize(config, client, factory: RobotFactory)
23
+ @config = config
24
+ @factory = factory
25
+ record(client)
26
+ end
27
+
28
+ # @param client [RobotLab::Robot, RobotLab::Network] the current client
29
+ # @return [RobotLab::Robot, RobotLab::Network] the client to use from now on
30
+ def call(client)
31
+ return record(client) unless client.equal?(@client)
32
+
33
+ if model_signature != @models
34
+ rebuild
35
+ elsif @factory.llm_params(@config) != @params
36
+ retune(client)
37
+ else
38
+ client
39
+ end
40
+ end
41
+
42
+ # The models (name, role) the client should be running.
43
+ def model_signature
44
+ Array(@config.models).map { [it.name, it.role] }
45
+ end
46
+
47
+ # Robot#update keywords for +params+ (from RobotFactory.llm_params).
48
+ # top_p and the penalties go to the provider as request options; they are
49
+ # always sent (possibly empty) so a value that was cleared is dropped.
50
+ def self.update_options(params)
51
+ options = {}
52
+ options[:temperature] = params[:temperature] if params.key?(:temperature)
53
+ options[:max_output_tokens] = params[:max_tokens] if params.key?(:max_tokens)
54
+ options[:provider_options] = params.slice(:top_p, :frequency_penalty, :presence_penalty)
55
+ options
56
+ end
57
+
58
+ private
59
+
60
+ def rebuild
61
+ client = @factory.rebuild(@config, history_mode: HistoryTransfer.mode_for(@config))
62
+ AIA.client = client
63
+ record(client)
64
+ end
65
+
66
+ def retune(client)
67
+ options = self.class.update_options(@factory.llm_params(@config))
68
+ HistoryTransfer.recipients(client).each { |robot| robot.update(**options) if robot.respond_to?(:update) }
69
+ record(client)
70
+ end
71
+
72
+ def record(client)
73
+ @client = client
74
+ @models = model_signature
75
+ @params = @factory.llm_params(@config)
76
+ client
77
+ end
78
+ end
79
+ end