Module: Kward::ConfigFiles
- Defined in:
- lib/kward/config_files.rb
Overview
Resolves Kward configuration, cache, memory, prompt, skill, and plugin paths, and reads/writes the JSON config file used by the CLI and RPC server.
This module is the configuration boundary, not a runtime settings cache.
Most methods read the filesystem each time so CLI commands and RPC reloads can
observe edits made outside the process. Callers that need caching should own
invalidation explicitly, as Client#reload_config does for provider state.
Keep path decisions here. Higher-level code should ask ConfigFiles for
config, prompt, skill, plugin, cache, memory, and session locations instead of
reconstructing ~/.kward paths independently.
Defined Under Namespace
Classes: ConfigError, PromptTemplate, Skill
Constant Summary collapse
- MAX_SKILL_FILE_BYTES =
100_000- MAX_PROMPT_FILE_BYTES =
32 * 1024
- DEFAULT_OVERLAY_SETTINGS =
{ "alignment" => "center", "width" => "maximum" }.freeze
- DEFAULT_PERSONAS =
{ "characters" => [ { "key" => "kward", "label" => "Kward", "instruction" => "Your name is Kward, the grim Andruid - robotic keeper of the Forest of Code, protecting the nature of good engineering principles. Speak like an old druid, be suspicious of everyone, but with good intent." } ], "default" => "kward" }.freeze
- OVERLAY_ALIGNMENTS =
%w[left center right].freeze
- OVERLAY_WIDTHS =
%w[capped maximum].freeze
Class Method Summary collapse
-
.active_persona_label(workspace_root:, model: nil, config: read_config) ⇒ Object
Returns the label of the persona selected by default/workspace/model rules.
- .add_persona_entry(entries, layer, value, name: nil) ⇒ Object
-
.agents_prompt ⇒ String?
Reads global principle instructions from the config directory.
- .cache_dir ⇒ Object
- .canonical_workspace_root(path) ⇒ Object
- .character_entries(raw) ⇒ Object
- .character_entry(entry) ⇒ Object
- .code_search_cache_dir ⇒ Object
-
.composer_busy_help?(config = read_config) ⇒ Boolean
Returns whether the composer should show busy-state keyboard help.
-
.composer_tab_keybindings(config = read_config) ⇒ Object
Returns the configured tab keybinding family, or auto when unset/invalid.
- .config_agents_path ⇒ Object
-
.config_dir ⇒ String
Directory that contains Kward's user config and adjacent prompt/skill data.
-
.config_path ⇒ String
Expanded JSON config file path.
- .config_principles_path ⇒ Object
-
.config_value(config, *keys) ⇒ Object
Returns the first present non-empty string value among several config keys.
- .crew_character_labels(personas) ⇒ Object
- .crew_characters(personas) ⇒ Object
- .default_config ⇒ Object
-
.delete_config_key(key, path = config_path) ⇒ Object
Removes a top-level config key when it exists.
-
.diff_view(config = read_config) ⇒ Object
Returns the integrated diff viewer display mode.
-
.editor_auto_close_pairs?(config = read_config) ⇒ Boolean
Returns whether the built-in TUI editor should auto-close typed pairs.
-
.editor_auto_indent?(config = read_config) ⇒ Boolean
Returns whether the built-in TUI editor should auto-indent new lines.
-
.editor_bar_cursor?(config = read_config) ⇒ Boolean
Returns whether editable built-in TUI editor buffers should use a bar cursor.
-
.editor_line_numbers(config = read_config) ⇒ Object
Returns the built-in TUI editor line-number display mode.
-
.editor_mode(config = read_config) ⇒ Object
Returns the built-in TUI editor keymap mode.
-
.editor_soft_wrap?(config = read_config) ⇒ Boolean
Returns whether the built-in TUI editor should soft-wrap long lines.
- .ekwsh_config_path ⇒ Object
-
.enforce_workspace_agents_file?(config = read_config) ⇒ Boolean
Returns whether workspace AGENTS.md contents should be injected directly instead of a compact read-when-relevant instruction.
-
.ensure_default_config!(path = config_path) ⇒ Object
Performs ensure default config for configuration file and path handling.
- .extract_character_instruction(definition) ⇒ Object
- .extract_character_label(definition) ⇒ Object
- .inside_directory?(path, base) ⇒ Boolean
- .lifecycle_hooks_config(workspace_root = Dir.pwd) ⇒ Object
-
.mcp_servers(config = read_config) ⇒ Object
Returns configured MCP stdio servers, or an empty config when absent.
- .memory_core_path ⇒ Object
-
.memory_dir ⇒ String
Directory containing structured memory files.
- .memory_events_path ⇒ Object
- .memory_soft_path ⇒ Object
- .merge_hook_maps(left, right) ⇒ Object
- .merge_hooks_config(config, workspace_config) ⇒ Object
- .named_character_values(personas) ⇒ Object
- .normalize_ekwsh_aliases(values) ⇒ Object
- .normalize_ekwsh_config(data) ⇒ Object
- .normalize_ekwsh_env(values) ⇒ Object
- .normalize_ekwsh_shell(value) ⇒ Object
- .normalize_positive_integer(value, default) ⇒ Object
- .openrouter_models_cache_path ⇒ Object
-
.overlay_settings(config = read_config) ⇒ Hash
Returns validated overlay settings with defaults for missing or invalid values.
- .persona_entries(workspace_root:, model: nil, reasoning_effort: nil, now: Time.now, config: read_config, include_reasoning: true) ⇒ Object
- .persona_label_for_key(value, labels) ⇒ Object
-
.persona_prompt(workspace_root, model: nil, reasoning_effort: nil, now: Time.now, config: read_config) ⇒ String?
Builds persona prompt text from default, workspace, model, reasoning, time-of-day, weekday, and suffix config entries.
-
.plugin_dir ⇒ String
Trusted user plugin directory.
-
.plugin_paths ⇒ Array<String>
Finds trusted top-level plugin files.
- .presence(value) ⇒ Object
- .project_browser_state_path ⇒ Object
-
.project_skills_trusted?(config = read_config) ⇒ Boolean
Returns whether project-level Agent Skills should be loaded from the workspace.
- .prompt_history_path(cwd, config_dir: self.config_dir, kind: "prompt") ⇒ Object
- .prompt_template_registry ⇒ Object
-
.prompt_templates(reserved_commands: []) ⇒ Array<PromptTemplate>
Lists prompt templates exposed as slash commands.
-
.read_config(path = config_path) ⇒ Hash
Reads the JSON config file.
- .read_ekwsh_config(path = ekwsh_config_path) ⇒ Object
- .read_prompt_file(path, label) ⇒ Object
-
.read_skill_file(name, relative_path = nil, workspace_root: Dir.pwd) ⇒ String
Reads a skill file by skill name and optional relative path.
- .read_trusted_workspace_hooks ⇒ Object
- .read_trusted_workspace_hooks_config(workspace_root = Dir.pwd) ⇒ Object
- .resolved_persona_text(value, characters: {}) ⇒ Object
-
.session_auto_resume_enabled?(config = read_config) ⇒ Boolean
Returns whether new frontends should resume the last active session automatically.
-
.skills(workspace_root: Dir.pwd) ⇒ Array<Skill>
Lists configured skills discovered under the config directory.
- .skills_registry(workspace_root: Dir.pwd) ⇒ Object
- .skip_config=(value) ⇒ Object
- .skip_config? ⇒ Boolean
- .time_of_day_bucket(now) ⇒ Object
- .trust_workspace_hooks!(workspace_root = Dir.pwd) ⇒ Object
- .trusted_workspace_hooks_path ⇒ Object
- .untrust_workspace_hooks!(workspace_root = Dir.pwd) ⇒ Object
- .update_check_cache_path ⇒ Object
-
.update_config(values, path = config_path) ⇒ Object
Merges top-level config values and writes the updated config privately.
-
.update_nested_config(section, values, path = config_path) ⇒ Object
Merges values into a one-level nested config section and writes privately.
-
.update_overlay_settings(values) ⇒ Object
Validates and persists terminal overlay settings.
-
.web_search_config(config = read_config) ⇒ Object
Returns the nested web-search config object, or an empty config when absent.
- .weekday_name(now) ⇒ Object
- .workspace_agents_file?(workspace_root) ⇒ Boolean
- .workspace_agents_path(workspace_root) ⇒ Object
- .workspace_agents_prompt(workspace_root) ⇒ Object
-
.workspace_guardrails_enabled?(config = read_config) ⇒ Boolean
Returns whether file tools must stay inside the active workspace root.
- .workspace_hooks_digest(workspace_root = Dir.pwd) ⇒ Object
- .workspace_hooks_path(root = Dir.pwd) ⇒ Object
- .workspace_hooks_trusted?(workspace_root = Dir.pwd) ⇒ Boolean
-
.write_config(config, path = config_path) ⇒ Object
Writes config JSON using private file permissions.
Class Method Details
.active_persona_label(workspace_root:, model: nil, config: read_config) ⇒ Object
Returns the label of the persona selected by default/workspace/model rules.
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 |
# File 'lib/kward/config_files.rb', line 571 def active_persona_label(workspace_root:, model: nil, config: read_config) personas = config["personas"] return nil unless personas.is_a?(Hash) labels = crew_character_labels(personas) active_label = persona_label_for_key(personas["default"], labels) unless personas["default"].nil? workspaces = personas["workspaces"] if workspaces.is_a?(Hash) root = canonical_workspace_root(workspace_root) workspaces.each do |path, key| next unless canonical_workspace_root(path) == root active_label = persona_label_for_key(key, labels) break end end models = personas["models"] if models.is_a?(Hash) && !model.to_s.empty? && models.key?(model.to_s) active_label = persona_label_for_key(models[model.to_s], labels) end active_label end |
.add_persona_entry(entries, layer, value, name: nil) ⇒ Object
681 682 683 684 685 686 |
# File 'lib/kward/config_files.rb', line 681 def add_persona_entry(entries, layer, value, name: nil) text = presence(value) return unless text entries << { layer: layer.to_s, name: name.to_s, prompt: text } end |
.agents_prompt ⇒ String?
Reads global principle instructions from the config directory.
PRINCIPLES.md is preferred. AGENTS.md remains a backwards-compatible
alias for existing installations.
533 534 535 536 537 538 |
# File 'lib/kward/config_files.rb', line 533 def agents_prompt path = config_principles_path return read_prompt_file(path, "Kward principles file") if File.exist?(path) read_prompt_file(config_agents_path, "Kward AGENTS.md alias") end |
.cache_dir ⇒ Object
90 91 92 |
# File 'lib/kward/config_files.rb', line 90 def cache_dir File.join(config_dir, "cache") end |
.canonical_workspace_root(path) ⇒ Object
676 677 678 679 |
# File 'lib/kward/config_files.rb', line 676 def canonical_workspace_root(path) = File.(path.to_s.empty? ? Dir.pwd : path.to_s) File.directory?() ? File.realpath() : end |
.character_entries(raw) ⇒ Object
728 729 730 731 732 733 734 735 736 737 |
# File 'lib/kward/config_files.rb', line 728 def character_entries(raw) case raw when Hash raw.map { |key, definition| [key, definition] } when Array raw.filter_map { |entry| character_entry(entry) } else [] end end |
.character_entry(entry) ⇒ Object
739 740 741 742 743 744 745 746 747 748 |
# File 'lib/kward/config_files.rb', line 739 def character_entry(entry) return nil unless entry.is_a?(Hash) if entry.length == 1 && entry.keys.first.is_a?(String) [entry.keys.first, entry.values.first] else key = entry["key"] || entry[:key] || entry["id"] || entry[:id] || entry["name"] || entry[:name] key.to_s.empty? ? nil : [key, entry] end end |
.code_search_cache_dir ⇒ Object
159 160 161 |
# File 'lib/kward/config_files.rb', line 159 def code_search_cache_dir File.join(cache_dir, "code_search") end |
.composer_busy_help?(config = read_config) ⇒ Boolean
Returns whether the composer should show busy-state keyboard help.
413 414 415 416 |
# File 'lib/kward/config_files.rb', line 413 def composer_busy_help?(config = read_config) composer = config["composer"].is_a?(Hash) ? config["composer"] : {} composer["busy_help"] != false end |
.composer_tab_keybindings(config = read_config) ⇒ Object
Returns the configured tab keybinding family, or auto when unset/invalid.
419 420 421 422 423 |
# File 'lib/kward/config_files.rb', line 419 def composer_tab_keybindings(config = read_config) composer = config["composer"].is_a?(Hash) ? config["composer"] : {} value = composer["tab_keybindings"].to_s.downcase %w[auto ctrl alt].include?(value) ? value : "auto" end |
.config_agents_path ⇒ Object
544 545 546 |
# File 'lib/kward/config_files.rb', line 544 def config_agents_path File.join(config_dir, "AGENTS.md") end |
.config_dir ⇒ String
Directory that contains Kward's user config and adjacent prompt/skill
data. Defaults to ~/.kward, or the directory of KWARD_CONFIG_PATH.
78 79 80 81 82 83 |
# File 'lib/kward/config_files.rb', line 78 def config_dir config_path = ENV["KWARD_CONFIG_PATH"] return File.(File.dirname(config_path)) if config_path && !config_path.empty? File.("~/.kward") end |
.config_path ⇒ String
Returns expanded JSON config file path.
86 87 88 |
# File 'lib/kward/config_files.rb', line 86 def config_path File.(ENV["KWARD_CONFIG_PATH"] || File.join(config_dir, "config.json")) end |
.config_principles_path ⇒ Object
540 541 542 |
# File 'lib/kward/config_files.rb', line 540 def config_principles_path File.join(config_dir, "PRINCIPLES.md") end |
.config_value(config, *keys) ⇒ Object
Returns the first present non-empty string value among several config keys.
389 390 391 392 393 394 395 |
# File 'lib/kward/config_files.rb', line 389 def config_value(config, *keys) keys.each do |key| text = presence(config[key]) return text if text end nil end |
.crew_character_labels(personas) ⇒ Object
694 695 696 697 698 |
# File 'lib/kward/config_files.rb', line 694 def crew_character_labels(personas) named_character_values(personas) do |_key, definition| extract_character_label(definition) end end |
.crew_characters(personas) ⇒ Object
688 689 690 691 692 |
# File 'lib/kward/config_files.rb', line 688 def crew_characters(personas) named_character_values(personas) do |_key, definition| extract_character_instruction(definition) end end |
.default_config ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/kward/config_files.rb', line 106 def default_config { "personas" => JSON.parse(JSON.generate(DEFAULT_PERSONAS)), "memory" => { "enabled" => false, "auto_summary" => false }, "composer" => { "busy_help" => true, "tab_keybindings" => "auto" }, "editor" => { "mode" => "modern", "auto_indent" => true, "auto_close_pairs" => true, "soft_wrap" => true, "bar_cursor" => true, "line_numbers" => "absolute", "diff_view" => "auto" }, "overlay" => DEFAULT_OVERLAY_SETTINGS.dup, "web_search" => { "enabled" => true, "provider" => "auto", "allow_model_providers" => false }, "updates" => { "check" => true }, "sessions" => { "auto_resume" => false }, "skills" => { "trust_project" => false }, "enforce_workspace_agents_file" => false, "mcpServers" => {}, "tools" => { "workspace_guardrails" => true } } end |
.delete_config_key(key, path = config_path) ⇒ Object
Removes a top-level config key when it exists.
380 381 382 383 384 385 386 |
# File 'lib/kward/config_files.rb', line 380 def delete_config_key(key, path = config_path) config = read_config(path) existed = config.key?(key.to_s) config.delete(key.to_s) write_config(config, path) if existed existed end |
.diff_view(config = read_config) ⇒ Object
Returns the integrated diff viewer display mode.
462 463 464 465 |
# File 'lib/kward/config_files.rb', line 462 def diff_view(config = read_config) editor = config["editor"].is_a?(Hash) ? config["editor"] : {} DiffViewMode.normalize(editor["diff_view"]) end |
.editor_auto_close_pairs?(config = read_config) ⇒ Boolean
Returns whether the built-in TUI editor should auto-close typed pairs.
438 439 440 441 |
# File 'lib/kward/config_files.rb', line 438 def editor_auto_close_pairs?(config = read_config) editor = config["editor"].is_a?(Hash) ? config["editor"] : {} editor["auto_close_pairs"] != false end |
.editor_auto_indent?(config = read_config) ⇒ Boolean
Returns whether the built-in TUI editor should auto-indent new lines.
432 433 434 435 |
# File 'lib/kward/config_files.rb', line 432 def editor_auto_indent?(config = read_config) editor = config["editor"].is_a?(Hash) ? config["editor"] : {} editor["auto_indent"] != false end |
.editor_bar_cursor?(config = read_config) ⇒ Boolean
Returns whether editable built-in TUI editor buffers should use a bar cursor.
450 451 452 453 |
# File 'lib/kward/config_files.rb', line 450 def (config = read_config) editor = config["editor"].is_a?(Hash) ? config["editor"] : {} editor["bar_cursor"] != false end |
.editor_line_numbers(config = read_config) ⇒ Object
Returns the built-in TUI editor line-number display mode.
456 457 458 459 |
# File 'lib/kward/config_files.rb', line 456 def editor_line_numbers(config = read_config) editor = config["editor"].is_a?(Hash) ? config["editor"] : {} EditorMode.normalize_line_numbers(editor["line_numbers"]) end |
.editor_mode(config = read_config) ⇒ Object
Returns the built-in TUI editor keymap mode.
426 427 428 429 |
# File 'lib/kward/config_files.rb', line 426 def editor_mode(config = read_config) editor = config["editor"].is_a?(Hash) ? config["editor"] : {} EditorMode.normalize(editor["mode"]) end |
.editor_soft_wrap?(config = read_config) ⇒ Boolean
Returns whether the built-in TUI editor should soft-wrap long lines.
444 445 446 447 |
# File 'lib/kward/config_files.rb', line 444 def editor_soft_wrap?(config = read_config) editor = config["editor"].is_a?(Hash) ? config["editor"] : {} editor["soft_wrap"] != false end |
.ekwsh_config_path ⇒ Object
94 95 96 |
# File 'lib/kward/config_files.rb', line 94 def ekwsh_config_path File.join(config_dir, "ekwsh.yml") end |
.enforce_workspace_agents_file?(config = read_config) ⇒ Boolean
Returns whether workspace AGENTS.md contents should be injected directly instead of a compact read-when-relevant instruction.
487 488 489 |
# File 'lib/kward/config_files.rb', line 487 def enforce_workspace_agents_file?(config = read_config) config["enforce_workspace_agents_file"] == true end |
.ensure_default_config!(path = config_path) ⇒ Object
Performs ensure default config for configuration file and path handling.
150 151 152 153 154 155 156 157 |
# File 'lib/kward/config_files.rb', line 150 def ensure_default_config!(path = config_path) path = File.(path) return false if skip_config? && path == config_path return false if File.exist?(path) write_config(default_config, path) true end |
.extract_character_instruction(definition) ⇒ Object
756 757 758 759 760 761 762 763 764 765 |
# File 'lib/kward/config_files.rb', line 756 def extract_character_instruction(definition) return nil if definition.nil? if definition.is_a?(Hash) value = definition["instruction"] || definition[:instruction] return presence(value) end presence(definition) end |
.extract_character_label(definition) ⇒ Object
750 751 752 753 754 |
# File 'lib/kward/config_files.rb', line 750 def extract_character_label(definition) return nil unless definition.is_a?(Hash) presence(definition["label"] || definition[:label]) end |
.inside_directory?(path, base) ⇒ Boolean
845 846 847 |
# File 'lib/kward/config_files.rb', line 845 def inside_directory?(path, base) PathGuard.inside?(path, base) end |
.lifecycle_hooks_config(workspace_root = Dir.pwd) ⇒ Object
229 230 231 232 233 234 235 |
# File 'lib/kward/config_files.rb', line 229 def lifecycle_hooks_config(workspace_root = Dir.pwd) config = read_config workspace_config = read_trusted_workspace_hooks_config(workspace_root) return config if workspace_config.empty? merge_hooks_config(config, workspace_config) end |
.mcp_servers(config = read_config) ⇒ Object
Returns configured MCP stdio servers, or an empty config when absent.
498 499 500 501 |
# File 'lib/kward/config_files.rb', line 498 def mcp_servers(config = read_config) value = config["mcpServers"] || config.dig("mcp", "servers") value.is_a?(Hash) ? value : {} end |
.memory_core_path ⇒ Object
187 188 189 |
# File 'lib/kward/config_files.rb', line 187 def memory_core_path File.join(memory_dir, "core.json") end |
.memory_dir ⇒ String
Returns directory containing structured memory files.
183 184 185 |
# File 'lib/kward/config_files.rb', line 183 def memory_dir File.join(config_dir, "memory") end |
.memory_events_path ⇒ Object
195 196 197 |
# File 'lib/kward/config_files.rb', line 195 def memory_events_path File.join(memory_dir, "events.jsonl") end |
.memory_soft_path ⇒ Object
191 192 193 |
# File 'lib/kward/config_files.rb', line 191 def memory_soft_path File.join(memory_dir, "soft.jsonl") end |
.merge_hook_maps(left, right) ⇒ Object
285 286 287 288 289 290 291 292 293 |
# File 'lib/kward/config_files.rb', line 285 def merge_hook_maps(left, right) left = left.is_a?(Hash) ? left : {} right = right.is_a?(Hash) ? right : {} merged = JSON.parse(JSON.generate(left)) right.each do |event, entries| merged[event.to_s] = Array(merged[event.to_s]) + Array(entries) end merged end |
.merge_hooks_config(config, workspace_config) ⇒ Object
279 280 281 282 283 |
# File 'lib/kward/config_files.rb', line 279 def merge_hooks_config(config, workspace_config) merged = JSON.parse(JSON.generate(config || {})) merged["hooks"] = merge_hook_maps(merged["hooks"], workspace_config["hooks"] || workspace_config[:hooks]) merged end |
.named_character_values(personas) ⇒ Object
719 720 721 722 723 724 725 726 |
# File 'lib/kward/config_files.rb', line 719 def named_character_values(personas) character_entries(personas["characters"] || personas["crew"]).each_with_object({}) do |(key, definition), mapping| value = yield(key, definition) next if value.to_s.empty? mapping[key.to_s] = value end end |
.normalize_ekwsh_aliases(values) ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/kward/config_files.rb', line 345 def normalize_ekwsh_aliases(values) return {} unless values.is_a?(Hash) values.each_with_object({}) do |(name, command), result| name = name.to_s command = command.to_s.strip next unless Ekwsh.valid_alias_name?(name) next if command.empty? result[name] = command end end |
.normalize_ekwsh_config(data) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/kward/config_files.rb', line 305 def normalize_ekwsh_config(data) data = data.transform_keys(&:to_s) if data.is_a?(Hash) settings = data.is_a?(Hash) ? data : {} { shell: normalize_ekwsh_shell(settings["shell"]), timeout_seconds: normalize_positive_integer(settings["timeout_seconds"], Ekwsh::DEFAULT_TIMEOUT_SECONDS), max_output_bytes: normalize_positive_integer(settings["max_output_bytes"], Ekwsh::DEFAULT_MAX_OUTPUT_BYTES), history_limit: normalize_positive_integer(settings["history_limit"], Ekwsh::DEFAULT_HISTORY_LIMIT), env: normalize_ekwsh_env(settings["env"]), aliases: normalize_ekwsh_aliases(settings["aliases"]) } end |
.normalize_ekwsh_env(values) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/kward/config_files.rb', line 333 def normalize_ekwsh_env(values) return {} unless values.is_a?(Hash) values.each_with_object({}) do |(key, value), result| key = key.to_s next unless key.match?(/\A[A-Za-z_][A-Za-z0-9_]*\z/) next if value.nil? result[key] = value.to_s end end |
.normalize_ekwsh_shell(value) ⇒ Object
318 319 320 321 322 323 324 |
# File 'lib/kward/config_files.rb', line 318 def normalize_ekwsh_shell(value) shell = value.to_s.strip return Ekwsh::DEFAULT_SHELL if shell.empty? return shell if shell.start_with?("/") && File.executable?(shell) Ekwsh::DEFAULT_SHELL end |
.normalize_positive_integer(value, default) ⇒ Object
326 327 328 329 330 331 |
# File 'lib/kward/config_files.rb', line 326 def normalize_positive_integer(value, default) integer = Integer(value) integer.positive? ? integer : default rescue ArgumentError, TypeError default end |
.openrouter_models_cache_path ⇒ Object
163 164 165 |
# File 'lib/kward/config_files.rb', line 163 def openrouter_models_cache_path File.join(cache_dir, "openrouter_models.json") end |
.overlay_settings(config = read_config) ⇒ Hash
Returns validated overlay settings with defaults for missing or invalid values.
402 403 404 405 406 407 408 409 410 |
# File 'lib/kward/config_files.rb', line 402 def (config = read_config) = config["overlay"].is_a?(Hash) ? config["overlay"] : {} settings = DEFAULT_OVERLAY_SETTINGS.dup alignment = ["alignment"].to_s width = ["width"].to_s settings["alignment"] = alignment if OVERLAY_ALIGNMENTS.include?(alignment) settings["width"] = width if OVERLAY_WIDTHS.include?(width) settings end |
.persona_entries(workspace_root:, model: nil, reasoning_effort: nil, now: Time.now, config: read_config, include_reasoning: true) ⇒ Object
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 |
# File 'lib/kward/config_files.rb', line 597 def persona_entries(workspace_root:, model: nil, reasoning_effort: nil, now: Time.now, config: read_config, include_reasoning: true) personas = config["personas"] return [] unless personas.is_a?(Hash) characters = crew_characters(personas) entries = [] active_persona = { layer: "default", value: personas["default"], name: nil } workspaces = personas["workspaces"] if workspaces.is_a?(Hash) root = canonical_workspace_root(workspace_root) workspaces.each do |path, key| if canonical_workspace_root(path) == root active_persona = { layer: "workspace", value: key, name: path } break end end end models = personas["models"] if models.is_a?(Hash) && !model.to_s.empty? && models.key?(model.to_s) active_persona = { layer: "model", value: models[model.to_s], name: model.to_s } end add_persona_entry( entries, active_persona.fetch(:layer), resolved_persona_text(active_persona.fetch(:value), characters: characters), name: active_persona[:name] ) modifiers = personas["persona_modifiers"] if modifiers.is_a?(Hash) if include_reasoning reasoning = modifiers["reasoning"] add_persona_entry(entries, "reasoning", reasoning[reasoning_effort.to_s]) if reasoning.is_a?(Hash) && !reasoning_effort.to_s.empty? end time_of_day = modifiers["time_of_day"] bucket = time_of_day_bucket(now) add_persona_entry(entries, "time_of_day", time_of_day[bucket], name: bucket) if time_of_day.is_a?(Hash) weekday = modifiers["weekday"] day = weekday_name(now) add_persona_entry(entries, "weekday", weekday[day], name: day) if weekday.is_a?(Hash) add_persona_entry(entries, "suffix", modifiers["suffix"]) end entries end |
.persona_label_for_key(value, labels) ⇒ Object
712 713 714 715 716 717 |
# File 'lib/kward/config_files.rb', line 712 def persona_label_for_key(value, labels) key = value.to_s.strip return nil if key.empty? presence(labels[key]) end |
.persona_prompt(workspace_root, model: nil, reasoning_effort: nil, now: Time.now, config: read_config) ⇒ String?
Builds persona prompt text from default, workspace, model, reasoning, time-of-day, weekday, and suffix config entries.
Persona resolution is intentionally data-driven so users can edit config without plugin code. Keep new persona selectors additive and deterministic; prompt construction depends on stable ordering.
561 562 563 564 565 566 567 568 |
# File 'lib/kward/config_files.rb', line 561 def persona_prompt(workspace_root, model: nil, reasoning_effort: nil, now: Time.now, config: read_config) text = persona_entries(workspace_root: workspace_root, model: model, reasoning_effort: reasoning_effort, now: now, config: config).map do |entry| entry[:prompt] end.join("\n\n") return nil if text.empty? text end |
.plugin_dir ⇒ String
Returns trusted user plugin directory.
788 789 790 |
# File 'lib/kward/config_files.rb', line 788 def plugin_dir File.("~/.kward/plugins") end |
.plugin_paths ⇒ Array<String>
Finds trusted top-level plugin files.
Plugins are intentionally loaded only from ~/.kward/plugins, not from a
workspace or custom KWARD_CONFIG_PATH directory.
798 799 800 801 802 803 804 805 806 |
# File 'lib/kward/config_files.rb', line 798 def plugin_paths plugins_root = plugin_dir return [] unless Dir.exist?(plugins_root) Dir.glob(File.join(plugins_root, "*.rb")).sort rescue StandardError => e warn "Warning: skipping Kward plugins in #{plugins_root}: #{e.}" [] end |
.presence(value) ⇒ Object
849 850 851 852 |
# File 'lib/kward/config_files.rb', line 849 def presence(value) text = value.to_s text.empty? ? nil : text end |
.project_browser_state_path ⇒ Object
171 172 173 |
# File 'lib/kward/config_files.rb', line 171 def project_browser_state_path File.join(cache_dir, "project_browser_state.json") end |
.project_skills_trusted?(config = read_config) ⇒ Boolean
Returns whether project-level Agent Skills should be loaded from the workspace.
474 475 476 477 |
# File 'lib/kward/config_files.rb', line 474 def project_skills_trusted?(config = read_config) skills = config["skills"].is_a?(Hash) ? config["skills"] : {} skills["trust_project"] == true end |
.prompt_history_path(cwd, config_dir: self.config_dir, kind: "prompt") ⇒ Object
175 176 177 178 179 180 |
# File 'lib/kward/config_files.rb', line 175 def prompt_history_path(cwd, config_dir: self.config_dir, kind: "prompt") key = Digest::SHA256.hexdigest(canonical_workspace_root(cwd))[0, 24] return File.join(config_dir, "history", "#{key}.jsonl") if kind.to_s == "prompt" File.join(config_dir, "history", kind.to_s, "#{key}.jsonl") end |
.prompt_template_registry ⇒ Object
837 838 839 840 841 842 843 |
# File 'lib/kward/config_files.rb', line 837 def prompt_template_registry Prompts::Templates.new( config_dir: config_dir, template_class: PromptTemplate, markdown_parser: ->(path) { Frontmatter.markdown_parts(path) } ) end |
.prompt_templates(reserved_commands: []) ⇒ Array<PromptTemplate>
Lists prompt templates exposed as slash commands.
812 813 814 |
# File 'lib/kward/config_files.rb', line 812 def prompt_templates(reserved_commands: []) prompt_template_registry.prompt_templates(reserved_commands: reserved_commands) end |
.read_config(path = config_path) ⇒ Hash
Reads the JSON config file.
Missing files are treated as an empty config. Invalid JSON raises a user-facing error that includes the file path. This method does not merge defaults; callers should apply feature-specific defaults at the point where behavior is decided.
208 209 210 211 212 213 214 215 216 |
# File 'lib/kward/config_files.rb', line 208 def read_config(path = config_path) path = File.(path) return {} if skip_config? && path == config_path return {} unless File.exist?(path) JSON.parse(File.read(path)) rescue JSON::ParserError => e raise ConfigError.new(path: path, format: "JSON", detail: e.) end |
.read_ekwsh_config(path = ekwsh_config_path) ⇒ Object
295 296 297 298 299 300 301 302 303 |
# File 'lib/kward/config_files.rb', line 295 def read_ekwsh_config(path = ekwsh_config_path) path = File.(path) return normalize_ekwsh_config(nil) unless File.exist?(path) data = YAML.safe_load(File.read(path), permitted_classes: [], aliases: false) normalize_ekwsh_config(data) rescue Psych::SyntaxError => e raise "Invalid ekwsh YAML config: #{path}: #{e.}" end |
.read_prompt_file(path, label) ⇒ Object
661 662 663 664 665 666 667 668 669 670 671 672 673 674 |
# File 'lib/kward/config_files.rb', line 661 def read_prompt_file(path, label) return nil unless File.exist?(path) size = File.size(path) if size > MAX_PROMPT_FILE_BYTES warn "Warning: skipping #{label} #{path}: file too large (#{size} bytes; limit is #{MAX_PROMPT_FILE_BYTES} bytes)" return nil end File.read(path) rescue StandardError => e warn "Warning: skipping #{label} #{path}: #{e.}" nil end |
.read_skill_file(name, relative_path = nil, workspace_root: Dir.pwd) ⇒ String
Reads a skill file by skill name and optional relative path.
821 822 823 |
# File 'lib/kward/config_files.rb', line 821 def read_skill_file(name, relative_path = nil, workspace_root: Dir.pwd) skills_registry(workspace_root: workspace_root).read_skill_file(name, relative_path) end |
.read_trusted_workspace_hooks ⇒ Object
272 273 274 275 276 277 |
# File 'lib/kward/config_files.rb', line 272 def read_trusted_workspace_hooks config = read_config(trusted_workspace_hooks_path) config.is_a?(Hash) ? config : {} rescue ConfigError {} end |
.read_trusted_workspace_hooks_config(workspace_root = Dir.pwd) ⇒ Object
237 238 239 240 241 242 |
# File 'lib/kward/config_files.rb', line 237 def read_trusted_workspace_hooks_config(workspace_root = Dir.pwd) path = workspace_hooks_path(workspace_root) return {} unless workspace_hooks_trusted?(workspace_root) read_config(path) end |
.resolved_persona_text(value, characters: {}) ⇒ Object
700 701 702 703 704 705 706 707 708 709 710 |
# File 'lib/kward/config_files.rb', line 700 def resolved_persona_text(value, characters: {}) return nil if value.nil? key = value.to_s.strip return nil if key.empty? text = characters[key.to_s] return text unless text.to_s.empty? value end |
.session_auto_resume_enabled?(config = read_config) ⇒ Boolean
Returns whether new frontends should resume the last active session automatically.
480 481 482 483 |
# File 'lib/kward/config_files.rb', line 480 def session_auto_resume_enabled?(config = read_config) sessions = config["sessions"].is_a?(Hash) ? config["sessions"] : {} sessions["auto_resume"] == true end |
.skills(workspace_root: Dir.pwd) ⇒ Array<Skill>
Lists configured skills discovered under the config directory.
783 784 785 |
# File 'lib/kward/config_files.rb', line 783 def skills(workspace_root: Dir.pwd) skills_registry(workspace_root: workspace_root).skills end |
.skills_registry(workspace_root: Dir.pwd) ⇒ Object
825 826 827 828 829 830 831 832 833 834 835 |
# File 'lib/kward/config_files.rb', line 825 def skills_registry(workspace_root: Dir.pwd) Skills::Registry.new( config_dir: config_dir, workspace_root: workspace_root, project_skills_trusted: project_skills_trusted?, skill_class: Skill, max_file_bytes: MAX_SKILL_FILE_BYTES, markdown_parser: ->(path) { Frontmatter.markdown_parts(path, lenient: true) }, inside_directory: method(:inside_directory?) ) end |
.skip_config=(value) ⇒ Object
66 67 68 |
# File 'lib/kward/config_files.rb', line 66 def skip_config=(value) @skip_config = value end |
.skip_config? ⇒ Boolean
70 71 72 |
# File 'lib/kward/config_files.rb', line 70 def skip_config? @skip_config == true end |
.time_of_day_bucket(now) ⇒ Object
767 768 769 770 771 772 773 774 |
# File 'lib/kward/config_files.rb', line 767 def time_of_day_bucket(now) hour = now.hour return "morning" if hour >= 5 && hour < 11 return "before_lunch" if hour == 11 return "late_evening" if hour >= 21 || hour < 5 nil end |
.trust_workspace_hooks!(workspace_root = Dir.pwd) ⇒ Object
252 253 254 255 256 257 258 259 |
# File 'lib/kward/config_files.rb', line 252 def trust_workspace_hooks!(workspace_root = Dir.pwd) path = workspace_hooks_path(workspace_root) raise "No workspace hook config found: #{path}" unless File.file?(path) trusted = read_trusted_workspace_hooks trusted[File.(path)] = workspace_hooks_digest(workspace_root) PrivateFile.write_json(trusted_workspace_hooks_path, trusted) end |
.trusted_workspace_hooks_path ⇒ Object
102 103 104 |
# File 'lib/kward/config_files.rb', line 102 def trusted_workspace_hooks_path File.join(config_dir, "trusted_workspace_hooks.json") end |
.untrust_workspace_hooks!(workspace_root = Dir.pwd) ⇒ Object
261 262 263 264 265 266 |
# File 'lib/kward/config_files.rb', line 261 def untrust_workspace_hooks!(workspace_root = Dir.pwd) path = File.(workspace_hooks_path(workspace_root)) trusted = read_trusted_workspace_hooks trusted.delete(path) PrivateFile.write_json(trusted_workspace_hooks_path, trusted) end |
.update_check_cache_path ⇒ Object
167 168 169 |
# File 'lib/kward/config_files.rb', line 167 def update_check_cache_path File.join(cache_dir, "update_check.json") end |
.update_config(values, path = config_path) ⇒ Object
Merges top-level config values and writes the updated config privately.
359 360 361 362 363 364 365 366 |
# File 'lib/kward/config_files.rb', line 359 def update_config(values, path = config_path) raise "Config values must be an object" unless values.is_a?(Hash) config = read_config(path) values.each { |key, value| config[key.to_s] = value } write_config(config, path) config end |
.update_nested_config(section, values, path = config_path) ⇒ Object
Merges values into a one-level nested config section and writes privately.
369 370 371 372 373 374 375 376 377 |
# File 'lib/kward/config_files.rb', line 369 def update_nested_config(section, values, path = config_path) raise "Config values must be an object" unless values.is_a?(Hash) config = read_config(path) current = config[section.to_s].is_a?(Hash) ? config[section.to_s].dup : {} config[section.to_s] = current.merge(values.transform_keys(&:to_s)) write_config(config, path) config end |
.update_overlay_settings(values) ⇒ Object
Validates and persists terminal overlay settings.
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 |
# File 'lib/kward/config_files.rb', line 504 def (values) raise "Overlay settings must be an object" unless values.is_a?(Hash) config = read_config = (config) values.each do |key, value| key = key.to_s value = value.to_s case key when "alignment" raise "Overlay alignment must be left, center, or right" unless OVERLAY_ALIGNMENTS.include?(value) when "width" raise "Overlay width must be capped or maximum" unless OVERLAY_WIDTHS.include?(value) else raise "Unknown overlay setting: #{key}" end [key] = value end config["overlay"] = write_config(config) (config) end |
.web_search_config(config = read_config) ⇒ Object
Returns the nested web-search config object, or an empty config when absent.
492 493 494 495 |
# File 'lib/kward/config_files.rb', line 492 def web_search_config(config = read_config) value = config["web_search"] value.is_a?(Hash) ? value : {} end |
.weekday_name(now) ⇒ Object
776 777 778 |
# File 'lib/kward/config_files.rb', line 776 def weekday_name(now) %w[sunday monday tuesday wednesday thursday friday saturday][now.wday] end |
.workspace_agents_file?(workspace_root) ⇒ Boolean
653 654 655 |
# File 'lib/kward/config_files.rb', line 653 def workspace_agents_file?(workspace_root) File.exist?(workspace_agents_path(workspace_root)) end |
.workspace_agents_path(workspace_root) ⇒ Object
649 650 651 |
# File 'lib/kward/config_files.rb', line 649 def workspace_agents_path(workspace_root) File.join(canonical_workspace_root(workspace_root), "AGENTS.md") end |
.workspace_agents_prompt(workspace_root) ⇒ Object
657 658 659 |
# File 'lib/kward/config_files.rb', line 657 def workspace_agents_prompt(workspace_root) read_prompt_file(workspace_agents_path(workspace_root), "workspace AGENTS.md") end |
.workspace_guardrails_enabled?(config = read_config) ⇒ Boolean
Returns whether file tools must stay inside the active workspace root.
468 469 470 471 |
# File 'lib/kward/config_files.rb', line 468 def workspace_guardrails_enabled?(config = read_config) tools = config["tools"].is_a?(Hash) ? config["tools"] : {} tools["workspace_guardrails"] != false end |
.workspace_hooks_digest(workspace_root = Dir.pwd) ⇒ Object
268 269 270 |
# File 'lib/kward/config_files.rb', line 268 def workspace_hooks_digest(workspace_root = Dir.pwd) Digest::SHA256.file(workspace_hooks_path(workspace_root)).hexdigest end |
.workspace_hooks_path(root = Dir.pwd) ⇒ Object
98 99 100 |
# File 'lib/kward/config_files.rb', line 98 def workspace_hooks_path(root = Dir.pwd) File.join(File.(root), ".kward", "hooks.json") end |
.workspace_hooks_trusted?(workspace_root = Dir.pwd) ⇒ Boolean
244 245 246 247 248 249 250 |
# File 'lib/kward/config_files.rb', line 244 def workspace_hooks_trusted?(workspace_root = Dir.pwd) path = workspace_hooks_path(workspace_root) return false unless File.file?(path) trusted = read_trusted_workspace_hooks trusted[File.(path)] == workspace_hooks_digest(workspace_root) end |
.write_config(config, path = config_path) ⇒ Object
Writes config JSON using private file permissions.
222 223 224 225 226 227 |
# File 'lib/kward/config_files.rb', line 222 def write_config(config, path = config_path) path = File.(path) raise "Cannot write Kward config while --skip-config is active: #{path}" if skip_config? && path == config_path PrivateFile.write_json(path, config) end |