Module: Askcii
- Defined in:
- lib/askcii.rb,
lib/askcii/cli.rb,
lib/askcii/errors.rb,
lib/askcii/version.rb,
lib/askcii/application.rb,
lib/askcii/models/chat.rb,
lib/askcii/chat_factory.rb,
lib/askcii/chat_session.rb,
lib/askcii/models/config.rb,
lib/askcii/models/message.rb,
lib/askcii/provider_config.rb,
lib/askcii/config_validator.rb,
lib/askcii/commands/base_command.rb,
lib/askcii/commands/chat_command.rb,
lib/askcii/commands/help_command.rb,
lib/askcii/configuration_manager.rb,
lib/askcii/commands/history_command.rb,
lib/askcii/commands/configure_command.rb,
lib/askcii/commands/clear_history_command.rb,
lib/askcii/commands/last_response_command.rb,
lib/askcii/commands/list_sessions_command.rb
Defined Under Namespace
Modules: Commands Classes: Application, CLI, Chat, ChatFactory, ChatSession, Config, ConfigValidator, ConfigurationError, ConfigurationManager, Error, Message, ProviderConfig, SessionError, ValidationError
Constant Summary collapse
- VERSION =
'0.4.0'
Class Method Summary collapse
- .configure_llm(selected_config = nil) ⇒ Object
- .database ⇒ Object
-
.db_path ⇒ Object
Get the path to the database file.
-
.load_legacy_config ⇒ Hash?
Loads legacy configuration from database or environment variables.
- .require_application ⇒ Object
- .require_models ⇒ Object
-
.setup_database ⇒ Object
Initialize the database.
Class Method Details
.configure_llm(selected_config = nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/askcii.rb', line 57 def self.configure_llm(selected_config = nil) # Validate configuration before attempting to use it if selected_config ConfigValidator.validate!(selected_config) else # Try legacy configuration with proper error handling selected_config = load_legacy_config ConfigValidator.validate!(selected_config) if selected_config end raise ConfigurationError, "No configuration available. Run 'askcii -c' to configure." unless selected_config RubyLLM.configure do |config| config.log_file = '/dev/null' provider = selected_config[:provider] || selected_config['provider'] api_key = selected_config[:api_key] || selected_config['api_key'] api_endpoint = selected_config[:api_endpoint] || selected_config['api_endpoint'] # Set the appropriate API key based on provider case provider.to_s.downcase when 'openai' config.openai_api_key = api_key config.openai_api_base = api_endpoint when 'anthropic' config.anthropic_api_key = api_key when 'gemini' config.gemini_api_key = api_key when 'deepseek' config.deepseek_api_key = api_key when 'openrouter' config.openrouter_api_key = api_key when 'ollama' # Ollama doesn't need an API key, just endpoint config.ollama_api_base = api_endpoint else raise ConfigurationError, "Unknown provider: #{provider}" end end end |
.database ⇒ Object
13 14 15 |
# File 'lib/askcii.rb', line 13 def self.database @@database ||= Sequel.amalgalite(db_path) end |
.db_path ⇒ Object
Get the path to the database file
18 19 20 21 22 |
# File 'lib/askcii.rb', line 18 def self.db_path db_dir = File.join(ENV['HOME'], '.local', 'share', 'askcii') FileUtils.mkdir_p(db_dir) unless Dir.exist?(db_dir) File.join(db_dir, 'askcii.db') end |
.load_legacy_config ⇒ Hash?
Loads legacy configuration from database or environment variables
100 101 102 103 104 105 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 |
# File 'lib/askcii.rb', line 100 def self.load_legacy_config # Try loading from Config model (requires model to be loaded) return nil unless defined?(Askcii::Config) begin api_key = Askcii::Config.api_key api_endpoint = Askcii::Config.api_endpoint model_id = Askcii::Config.model_id # If we got values from the database, use them if api_key || api_endpoint || model_id return { provider: 'openai', # Legacy default api_key: api_key, api_endpoint: api_endpoint || ProviderConfig.default_endpoint('openai'), model_id: model_id || 'gpt-3.5-turbo' } end rescue Sequel::DatabaseError, NoMethodError => e # Database or model not available, fall through to env vars end # Fall back to environment variables if ENV['ASKCII_API_KEY'] { provider: 'openai', # Legacy default api_key: ENV['ASKCII_API_KEY'], api_endpoint: ENV['ASKCII_API_ENDPOINT'] || ProviderConfig.default_endpoint('openai'), model_id: ENV['ASKCII_MODEL_ID'] || 'gpt-3.5-turbo' } else nil end end |
.require_application ⇒ Object
141 142 143 |
# File 'lib/askcii.rb', line 141 def self.require_application require_relative './askcii/application' end |
.require_models ⇒ Object
135 136 137 138 139 |
# File 'lib/askcii.rb', line 135 def self.require_models require_relative './askcii/models/chat' require_relative './askcii/models/message' require_relative './askcii/models/config' end |
.setup_database ⇒ Object
Initialize the database
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/askcii.rb', line 25 def self.setup_database unless database.table_exists?(:chats) database.create_table :chats do primary_key :id String :model_id, null: true String :context, null: true Datetime :created_at, null: false, default: Sequel::CURRENT_TIMESTAMP end end unless database.table_exists?(:messages) database.create_table :messages do primary_key :id foreign_key :chat_id, :chats, null: false String :role, null: true Text :content, null: true String :model_id, null: true Integer :input_tokens, null: true Integer :output_tokens, null: true Datetime :created_at, null: false, default: Sequel::CURRENT_TIMESTAMP end end return if database.table_exists?(:configs) database.create_table :configs do primary_key :id String :key, null: false, unique: true Text :value, null: true end end |