Skip to content
Kward Search API index

Class: Kward::RPC::ConfigManager

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/rpc/config_manager.rb

Overview

RPC configuration manager for reading and updating user config.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path: OpenAIOAuth.default_config_path, api_key_store: nil) ⇒ ConfigManager

Returns a new instance of ConfigManager.



15
16
17
18
# File 'lib/kward/rpc/config_manager.rb', line 15

def initialize(config_path: OpenAIOAuth.default_config_path, api_key_store: nil)
  @config_path = File.expand_path(config_path)
  @api_key_store = api_key_store || APIKeyStore.new(path: File.join(File.dirname(@config_path), "api_keys.json"), config_path: @config_path)
end

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



20
21
22
# File 'lib/kward/rpc/config_manager.rb', line 20

def config_path
  @config_path
end

Instance Method Details

#api_key_status(provider_id) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kward/rpc/config_manager.rb', line 53

def api_key_status(provider_id)
  provider = ProviderCatalog.fetch(provider_id)
  configured = @api_key_store.configured?(provider.id)
  stored = @api_key_store.stored?(provider.id)
  {
    configured: configured,
    stored: stored,
    source: configured ? (environment_api_key?(provider) ? "environment" : "stored") : nil,
    canLogout: stored
  }.compact
end

#configure_azure_openai(values) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/kward/rpc/config_manager.rb', line 69

def configure_azure_openai(values)
  setup = AzureOpenAIConfig.new(
    endpoint: values["endpoint"] || values[:endpoint],
    deployment: values["deployment"] || values[:deployment],
    api_version: values["apiVersion"] || values[:apiVersion] || values["api_version"] || values[:api_version]
  )
  update(setup.to_config)
end

#delete_api_key(provider_id) ⇒ Object



65
66
67
# File 'lib/kward/rpc/config_manager.rb', line 65

def delete_api_key(provider_id)
  @api_key_store.delete(provider_id)
end

#delete_key(key) ⇒ Object



78
79
80
# File 'lib/kward/rpc/config_manager.rb', line 78

def delete_key(key)
  ConfigFiles.delete_config_key(key, @config_path)
end

#read(redacted: true) ⇒ Object



22
23
24
25
# File 'lib/kward/rpc/config_manager.rb', line 22

def read(redacted: true)
  config = load_config
  redacted ? Redactor.redact(config) : config
end

#session_auto_resume_enabled?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/kward/rpc/config_manager.rb', line 86

def session_auto_resume_enabled?
  ConfigFiles.session_auto_resume_enabled?(read(redacted: false))
end

#set_api_key(provider_id, api_key) ⇒ Object



46
47
48
49
50
51
# File 'lib/kward/rpc/config_manager.rb', line 46

def set_api_key(provider_id, api_key)
  provider = ProviderCatalog.fetch(provider_id)
  raise "#{provider.name} does not accept an API key" unless provider.api_key?

  @api_key_store.store(provider.id, api_key)
end

#set_model(model, provider: nil) ⇒ Object



32
33
34
35
36
37
# File 'lib/kward/rpc/config_manager.rb', line 32

def set_model(model, provider: nil)
  model = model.to_s.strip
  raise "Model must be a non-empty string" if model.empty?

  update(ModelInfo.config_values_for_selection(provider, model))
end

#set_reasoning_effort(effort, provider: nil) ⇒ Object



39
40
41
42
43
44
# File 'lib/kward/rpc/config_manager.rb', line 39

def set_reasoning_effort(effort, provider: nil)
  effort = effort.to_s.strip
  raise "Reasoning effort must be a non-empty string" if effort.empty?

  update(ModelInfo.reasoning_config_key_for_provider(provider) => effort)
end

#update(values) ⇒ Object



27
28
29
30
# File 'lib/kward/rpc/config_manager.rb', line 27

def update(values)
  reject_provider_api_keys!(values)
  Redactor.redact(ConfigFiles.update_config(values, @config_path))
end

#workspace_guardrails_enabled?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/kward/rpc/config_manager.rb', line 82

def workspace_guardrails_enabled?
  ConfigFiles.workspace_guardrails_enabled?(read(redacted: false))
end