Module: Kward::ConfigFiles::Core
Instance Method Summary collapse
-
#cache_dir ⇒ String
Directory containing reusable local caches.
- #code_search_cache_dir ⇒ 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_value(config, *keys) ⇒ Object
Returns the first present non-empty string value among several config keys.
-
#default_config ⇒ Hash
Returns a fresh default config suitable for first-run persistence.
-
#delete_config_key(key, path = config_path) ⇒ Object
Removes a top-level config key when it exists.
- #editor_explorer_state_path(cwd) ⇒ Object
-
#ensure_default_config!(path = config_path) ⇒ Object
Performs ensure default config for configuration file and path handling.
- #ignore_local_instructions=(value) ⇒ Object
- #ignore_local_instructions? ⇒ Boolean
-
#kwshrc_path ⇒ String
Embedded-shell rc config path.
-
#kwshrc_paths ⇒ Array<String>
Embedded-shell rc config paths in load order.
- #lifecycle_hooks_config(workspace_root = Dir.pwd) ⇒ Object
- #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
- #model_catalog_cache_path(provider_id) ⇒ Object
- #openrouter_models_cache_path ⇒ Object
- #project_browser_state_path ⇒ Object
- #prompt_history_path(cwd, config_dir: self.config_dir, kind: "prompt") ⇒ Object
-
#read_config(path = config_path) ⇒ Hash
Reads the JSON config file.
- #read_kwsh_config ⇒ Object
- #read_kwshrc_config(paths = kwshrc_paths, env: ENV.to_h) ⇒ Object
- #read_trusted_workspace_hooks ⇒ Object
- #read_trusted_workspace_hooks_config(workspace_root = Dir.pwd) ⇒ Object
- #skip_config=(value) ⇒ Object
- #skip_config? ⇒ Boolean
- #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.
- #warning_sink ⇒ Object
- #warning_sink=(sink) ⇒ Object
- #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.
Instance Method Details
#cache_dir ⇒ String
Returns directory containing reusable local caches.
86 87 88 |
# File 'lib/kward/config/core.rb', line 86 def cache_dir File.join(config_dir, "cache") end |
#code_search_cache_dir ⇒ Object
187 188 189 |
# File 'lib/kward/config/core.rb', line 187 def code_search_cache_dir File.join(cache_dir, "code_search") 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.
76 77 78 |
# File 'lib/kward/config/core.rb', line 76 def config_dir ConfigPaths.config_dir end |
#config_path ⇒ String
Returns expanded JSON config file path.
81 82 83 |
# File 'lib/kward/config/core.rb', line 81 def config_path File.(ENV["KWARD_CONFIG_PATH"] || File.join(config_dir, "config.json")) end |
#config_value(config, *keys) ⇒ Object
Returns the first present non-empty string value among several config keys.
390 391 392 393 394 395 396 |
# File 'lib/kward/config/core.rb', line 390 def config_value(config, *keys) keys.each do |key| text = presence(config[key]) return text if text end nil end |
#default_config ⇒ Hash
Returns a fresh default config suitable for first-run persistence.
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/kward/config/core.rb', line 111 def default_config { "personas" => JSON.parse(JSON.generate(DEFAULT_PERSONAS)), "memory" => { "enabled" => false, "auto_summary" => false }, "transcript" => { "turn_stats" => "compact" }, "composer" => { "busy_help" => true, "tab_keybindings" => "auto" }, "editor" => { "mode" => "modern", "agent" => {}, "runners" => {}, "auto_indent" => true, "auto_close_pairs" => true, "soft_wrap" => true, "bar_cursor" => true, "line_numbers" => "absolute", "diff_view" => "auto" }, "overlay" => DEFAULT_OVERLAY_SETTINGS.dup, "project_browser" => { "icons" => "off" }, "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, "system_prompt" => { "include_principles" => true }, "mcpServers" => {}, "plugins" => {}, "transports" => {}, "tools" => { "workspace_guardrails" => true }, "permissions" => { "enabled" => false, "mode" => "ask" }, "sandbox" => { "mode" => "off", "network" => "deny", "writable_roots" => [], "protect_git_metadata" => true } } end |
#delete_config_key(key, path = config_path) ⇒ Object
Removes a top-level config key when it exists.
381 382 383 384 385 386 387 |
# File 'lib/kward/config/core.rb', line 381 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 |
#editor_explorer_state_path(cwd) ⇒ Object
210 211 212 213 |
# File 'lib/kward/config/core.rb', line 210 def editor_explorer_state_path(cwd) key = Digest::SHA256.hexdigest(canonical_workspace_root(cwd))[0, 24] File.join(cache_dir, "editor_explorer", "#{key}.json") end |
#ensure_default_config!(path = config_path) ⇒ Object
Performs ensure default config for configuration file and path handling.
178 179 180 181 182 183 184 185 |
# File 'lib/kward/config/core.rb', line 178 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 |
#ignore_local_instructions=(value) ⇒ Object
52 53 54 |
# File 'lib/kward/config/core.rb', line 52 def ignore_local_instructions=(value) @ignore_local_instructions = value end |
#ignore_local_instructions? ⇒ Boolean
56 57 58 |
# File 'lib/kward/config/core.rb', line 56 def ignore_local_instructions? @ignore_local_instructions == true end |
#kwshrc_path ⇒ String
Returns embedded-shell rc config path.
91 92 93 |
# File 'lib/kward/config/core.rb', line 91 def kwshrc_path File.join(config_dir, "kwshrc") end |
#kwshrc_paths ⇒ Array<String>
Returns embedded-shell rc config paths in load order.
96 97 98 |
# File 'lib/kward/config/core.rb', line 96 def kwshrc_paths Kwshrc.paths(kwshrc_path) end |
#lifecycle_hooks_config(workspace_root = Dir.pwd) ⇒ Object
272 273 274 275 276 277 278 |
# File 'lib/kward/config/core.rb', line 272 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 |
#memory_core_path ⇒ Object
227 228 229 |
# File 'lib/kward/config/core.rb', line 227 def memory_core_path File.join(memory_dir, "core.json") end |
#memory_dir ⇒ String
Returns directory containing structured memory files.
223 224 225 |
# File 'lib/kward/config/core.rb', line 223 def memory_dir File.join(config_dir, "memory") end |
#memory_events_path ⇒ Object
235 236 237 |
# File 'lib/kward/config/core.rb', line 235 def memory_events_path File.join(memory_dir, "events.jsonl") end |
#memory_soft_path ⇒ Object
231 232 233 |
# File 'lib/kward/config/core.rb', line 231 def memory_soft_path File.join(memory_dir, "soft.jsonl") end |
#merge_hook_maps(left, right) ⇒ Object
328 329 330 331 332 333 334 335 336 |
# File 'lib/kward/config/core.rb', line 328 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
322 323 324 325 326 |
# File 'lib/kward/config/core.rb', line 322 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 |
#model_catalog_cache_path(provider_id) ⇒ Object
195 196 197 198 199 200 |
# File 'lib/kward/config/core.rb', line 195 def model_catalog_cache_path(provider_id) provider = provider_id.to_s.gsub(/[^a-z0-9_-]/i, "_") raise ArgumentError, "Provider id must be a non-empty string" if provider.empty? File.join(cache_dir, "models", "#{provider}.json") end |
#openrouter_models_cache_path ⇒ Object
191 192 193 |
# File 'lib/kward/config/core.rb', line 191 def openrouter_models_cache_path File.join(cache_dir, "openrouter_models.json") end |
#project_browser_state_path ⇒ Object
206 207 208 |
# File 'lib/kward/config/core.rb', line 206 def project_browser_state_path File.join(cache_dir, "project_browser_state.json") end |
#prompt_history_path(cwd, config_dir: self.config_dir, kind: "prompt") ⇒ Object
215 216 217 218 219 220 |
# File 'lib/kward/config/core.rb', line 215 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 |
#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.
248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/kward/config/core.rb', line 248 def read_config(path = config_path) path = File.(path) return {} if skip_config? && path == config_path return {} unless File.exist?(path) config = JSON.parse(File.read(path)) return config if config.is_a?(Hash) raise ConfigError.new(path: path, format: "JSON", detail: "top-level value must be an object") rescue JSON::ParserError => e raise ConfigError.new(path: path, format: "JSON", detail: e.) end |
#read_kwsh_config ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/kward/config/core.rb', line 338 def read_kwsh_config config = { shell: Kwsh::DEFAULT_SHELL, timeout_seconds: Kwsh::DEFAULT_TIMEOUT_SECONDS, max_output_bytes: Kwsh::DEFAULT_MAX_OUTPUT_BYTES, history_limit: Kwsh::DEFAULT_HISTORY_LIMIT, env: {}, aliases: {} } rc_config = read_kwshrc_config config[:env].merge!(rc_config[:env]) config[:aliases].merge!(rc_config[:aliases]) config end |
#read_kwshrc_config(paths = kwshrc_paths, env: ENV.to_h) ⇒ Object
353 354 355 356 357 |
# File 'lib/kward/config/core.rb', line 353 def read_kwshrc_config(paths = kwshrc_paths, env: ENV.to_h) Kwshrc.read(paths, env: env) rescue Kwshrc::ParseError => e raise "Invalid kwshrc config: #{e.}" end |
#read_trusted_workspace_hooks ⇒ Object
315 316 317 318 319 320 |
# File 'lib/kward/config/core.rb', line 315 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
280 281 282 283 284 285 |
# File 'lib/kward/config/core.rb', line 280 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 |
#skip_config=(value) ⇒ Object
48 49 50 |
# File 'lib/kward/config/core.rb', line 48 def skip_config=(value) @skip_config = value end |
#skip_config? ⇒ Boolean
68 69 70 |
# File 'lib/kward/config/core.rb', line 68 def skip_config? @skip_config == true end |
#trust_workspace_hooks!(workspace_root = Dir.pwd) ⇒ Object
295 296 297 298 299 300 301 302 |
# File 'lib/kward/config/core.rb', line 295 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
104 105 106 |
# File 'lib/kward/config/core.rb', line 104 def trusted_workspace_hooks_path File.join(config_dir, "trusted_workspace_hooks.json") end |
#untrust_workspace_hooks!(workspace_root = Dir.pwd) ⇒ Object
304 305 306 307 308 309 |
# File 'lib/kward/config/core.rb', line 304 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
202 203 204 |
# File 'lib/kward/config/core.rb', line 202 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.
360 361 362 363 364 365 366 367 |
# File 'lib/kward/config/core.rb', line 360 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.
370 371 372 373 374 375 376 377 378 |
# File 'lib/kward/config/core.rb', line 370 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 |
#warning_sink ⇒ Object
64 65 66 |
# File 'lib/kward/config/core.rb', line 64 def warning_sink @warning_sink end |
#warning_sink=(sink) ⇒ Object
60 61 62 |
# File 'lib/kward/config/core.rb', line 60 def warning_sink=(sink) @warning_sink = sink end |
#workspace_hooks_digest(workspace_root = Dir.pwd) ⇒ Object
311 312 313 |
# File 'lib/kward/config/core.rb', line 311 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
100 101 102 |
# File 'lib/kward/config/core.rb', line 100 def workspace_hooks_path(root = Dir.pwd) File.join(File.(root), ".kward", "hooks.json") end |
#workspace_hooks_trusted?(workspace_root = Dir.pwd) ⇒ Boolean
287 288 289 290 291 292 293 |
# File 'lib/kward/config/core.rb', line 287 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.
265 266 267 268 269 270 |
# File 'lib/kward/config/core.rb', line 265 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 |