Class: OllamaChat::Database::Models::Session

Inherits:
Object
  • Object
show all
Includes:
Duplicatable, RowLocking
Defined in:
lib/ollama_chat/database/models/session.rb

Overview

Represents a persistent chat session in the database, managing the configuration, state, and message history for an individual chat interaction.

This model utilizes Sequel plugins to handle timestamps, object touching, and the JSON serialization of complex attributes such as tools_default_enabled and model_options.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Duplicatable

#duplicate

Methods included from RowLocking

#lock, #lock?, #locked?, #unlock

Class Method Details

.with_defaults(chat, name: nil) ⇒ OllamaChat::Database::Models::Session

Factory method that initializes a new Session model instance with a set of default values derived from the active chat configuration.

Parameters:

  • chat (OllamaChat::Chat) —

    the active chat instance used to extract defaults

  • name (String, nil) (defaults to: nil) —

    an optional session name (defaults to a random name)

Returns:



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/ollama_chat/database/models/session.rb', line 192

def self.with_defaults(chat, name: nil)
  name ||= chat.new_random_session_name
  tools_default_enabled =
    chat.config.tools.functions.to_h.
    each_with_object({}) { |(name, f), h| h[name.to_s] = f[:default] }
  current_model = chat.initial_model
  model_options = chat.get_stored_model_options(current_model)
  attributes = {
    name:                     ,
    current_model:            ,
    current_collection:       chat.initial_collection,
    default_persona_name:     chat.config.persona&.default_name,
    current_system_prompt:    chat.initial_system_prompt,
    tools_enabled:            chat.config.tools.enabled,
    tools_default_enabled:    ,
    think_mode:               chat.config.think.mode,
    think_loud_enabled:       chat.config.think.loud,
    embedding_enabled:        chat.config.embedding.enabled,
    document_policy:          chat.config.document_policy,
    runtime_info_enabled:     chat.config.runtime_info.enabled,
    think_strip_enabled:      chat.config.think.strip,
    markdown_enabled:         chat.config.markdown,
    stream_enabled:           chat.config.stream,
    location_enabled:         chat.config.location.enabled,
    voice_enabled:            chat.config.voice.enabled,
    model_options:            ,
    current_voice:            chat.config.voice.default,
    context_format:           chat.config.context.format,
    context_manifest_enabled: chat.config.context.manifest,
    working_directory:        Dir.pwd,
    messages:                 '',
    trigger:                  nil,
  }
  new(attributes)
end

Instance Method Details

#age(now: Time.now) ⇒ Tins::Duration

Calculates the age of the session based on the last update timestamp.

Parameters:

  • now (Time) (defaults to: Time.now) —

    the reference time for the age calculation (defaults to Time.now)

Returns:

  • (Tins::Duration) —

    the duration since the session was last updated



51
52
53
# File 'lib/ollama_chat/database/models/session.rb', line 51

def age(now: Time.now)
  Tins::Duration.new(updated_at ? now - updated_at : 0)
end

#context_format=(value) ⇒ String

Returns The format used for project context generation (e.g., 'JSON', 'TOON').

Returns:

  • (String) —

    The format used for project context generation (e.g., 'JSON', 'TOON').



# File 'lib/ollama_chat/database/models/session.rb', line 90

#context_manifest_enabled=(value) ⇒ Boolean

Returns Whether context files use manifest mode (metadata only, no content).

Returns:

  • (Boolean) —

    Whether context files use manifest mode (metadata only, no content).



# File 'lib/ollama_chat/database/models/session.rb', line 90

#count_messages ⇒ Integer

Counts the number of messages in the session's JSONL history.

Returns:

  • (Integer) —

    the number of messages stored in the session



65
66
67
# File 'lib/ollama_chat/database/models/session.rb', line 65

def count_messages
  messages.to_s.count(?\n)
end

#created_at=(value) ⇒ Time?

Returns The timestamp when the session was created.

Returns:

  • (Time, nil) —

    The timestamp when the session was created.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#current_collection=(value) ⇒ String?

Returns The name of the currently active document collection.

Returns:

  • (String, nil) —

    The name of the currently active document collection.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#current_model=(value) ⇒ String?

Returns The name of the model currently active in this session.

Returns:

  • (String, nil) —

    The name of the model currently active in this session.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#current_system_prompt=(value) ⇒ String?

Returns The text of the current system prompt.

Returns:

  • (String, nil) —

    The text of the current system prompt.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#current_voice=(value) ⇒ String

Returns The name of the voice currently in use.

Returns:

  • (String) —

    The name of the voice currently in use.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#default_persona_name=(value) ⇒ String?

Returns The identifier of the persona set as default for this session.

Returns:

  • (String, nil) —

    The identifier of the persona set as default for this session.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#document_policy=(value) ⇒ String

Returns The policy for handling document references (must be one of DOCUMENT_POLICY_STATES).

Returns:

  • (String) —

    The policy for handling document references (must be one of DOCUMENT_POLICY_STATES).



# File 'lib/ollama_chat/database/models/session.rb', line 90

#embedding_enabled=(value) ⇒ Boolean

Returns Whether embedding/RAG capabilities are enabled.

Returns:

  • (Boolean) —

    Whether embedding/RAG capabilities are enabled.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#estimate_tokens ⇒ OllamaChat::TokenEstimator::Estimate

Estimates the token and byte size of the session's message history.

Returns:



58
59
60
# File 'lib/ollama_chat/database/models/session.rb', line 58

def estimate_tokens
  OllamaChat::TokenEstimator.estimate(messages.to_s)
end

#history=(value) ⇒ String

Returns A text field storing the history for the session.

Returns:

  • (String) —

    A text field storing the history for the session.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#id=(value) ⇒ Integer

Returns The primary key for the session.

Returns:

  • (Integer) —

    The primary key for the session.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#links=(value) ⇒ String

Returns A text field storing links associated with the session.

Returns:

  • (String) —

    A text field storing links associated with the session.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#location_enabled=(value) ⇒ Boolean

Returns Whether location-based information is enabled.

Returns:

  • (Boolean) —

    Whether location-based information is enabled.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#locked_by_pid=(value) ⇒ Integer?

Returns The process ID that currently holds a lock on this session.

Returns:

  • (Integer, nil) —

    The process ID that currently holds a lock on this session.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#markdown_enabled=(value) ⇒ Boolean

Returns Whether Markdown rendering is enabled for the session.

Returns:

  • (Boolean) —

    Whether Markdown rendering is enabled for the session.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#messages=(value) ⇒ String

Returns The full conversation history, stored in JSONL format.

Returns:

  • (String) —

    The full conversation history, stored in JSONL format.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#model_options=(value) ⇒ String?

Returns A JSON-serialized string containing model-specific options.

Returns:

  • (String, nil) —

    A JSON-serialized string containing model-specific options.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#name=(value) ⇒ String

Returns The unique name of the session.

Returns:

  • (String) —

    The unique name of the session.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#runtime_info_enabled=(value) ⇒ Boolean

Returns Whether runtime information is displayed during the session.

Returns:

  • (Boolean) —

    Whether runtime information is displayed during the session.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#set_trigger_enabled(value) ⇒ Object

Sets the enabled flag of the first configured trigger entry and persists the change.

No-op when no trigger is configured.

Parameters:

  • value (Boolean) —

    the new enabled state



83
84
85
86
87
88
# File 'lib/ollama_chat/database/models/session.rb', line 83

def set_trigger_enabled(value)
  if hash = trigger&.values&.first
    hash['enabled'] = value
    save
  end
end

#stream_enabled=(value) ⇒ Boolean

Returns Whether the response should be streamed.

Returns:

  • (Boolean) —

    Whether the response should be streamed.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#think_loud_enabled=(value) ⇒ Boolean

Returns Whether thinking annotations are displayed in the output.

Returns:

  • (Boolean) —

    Whether thinking annotations are displayed in the output.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#think_mode=(value) ⇒ String

Returns The current thinking mode (must be one of Chat#think_mode_states).

Returns:

  • (String) —

    The current thinking mode (must be one of Chat#think_mode_states).



# File 'lib/ollama_chat/database/models/session.rb', line 90

#think_strip_enabled=(value) ⇒ Boolean

Returns Whether thinking content should be stripped from the final output.

Returns:

  • (Boolean) —

    Whether thinking content should be stripped from the final output.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#tools_default_enabled=(value) ⇒ String?

Returns A JSON-serialized string containing default settings for tools.

Returns:

  • (String, nil) —

    A JSON-serialized string containing default settings for tools.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#tools_enabled=(value) ⇒ Boolean

Returns Indicates whether tool calling is enabled.

Returns:

  • (Boolean) —

    Indicates whether tool calling is enabled.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#trigger=(value) ⇒ Hash?

Returns Maps collection names to search/rerank overrides, e.g. { 'memory-miyu' => { 'prompt_name' => 'memory_rerank', 'text_count' => 5 } }. Recognized keys: prompt_name, text_count, text_size, min_similarity. nil when the trigger mechanism is disabled for this session.

Returns:

  • (Hash, nil) —

    Maps collection names to search/rerank overrides, e.g. { 'memory-miyu' => { 'prompt_name' => 'memory_rerank', 'text_count' => 5 } }. Recognized keys: prompt_name, text_count, text_size, min_similarity. nil when the trigger mechanism is disabled for this session.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#trigger_enabled? ⇒ Boolean?

Returns the enabled flag of the first configured trigger entry.

Returns:

  • (Boolean, nil) —

    true when the trigger is active, false when explicitly disabled, nil when no trigger is configured



73
74
75
# File 'lib/ollama_chat/database/models/session.rb', line 73

def trigger_enabled?
  trigger&.values&.first&.[]('enabled')
end

#updated_at=(value) ⇒ Time?

Returns The timestamp of the last update to the session.

Returns:

  • (Time, nil) —

    The timestamp of the last update to the session.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#validate ⇒ Object

Validates the session instance.

Ensures that the name and essential timestamps (updated_at, created_at) are present to ensure session age calculations can be performed without Nil errors.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ollama_chat/database/models/session.rb', line 23

def validate
  super
  validates_presence :name
  validates_unique :name
  validates_presence :updated_at
  validates_presence :created_at

  validates_presence :context_format
  validates_presence :context_manifest_enabled
  validates_presence :current_voice
  validates_presence :document_policy
  validates_presence :embedding_enabled
  validates_presence :location_enabled
  validates_presence :markdown_enabled
  validates_presence :runtime_info_enabled
  validates_presence :stream_enabled
  validates_presence :think_loud_enabled
  validates_presence :think_mode
  validates_presence :think_strip_enabled
  validates_presence :tools_enabled
  validates_presence :voice_enabled
end

#voice_enabled=(value) ⇒ Boolean

Returns Whether voice output is enabled.

Returns:

  • (Boolean) —

    Whether voice output is enabled.



# File 'lib/ollama_chat/database/models/session.rb', line 90

#working_directory=(value) ⇒ String

Returns The directory used as the working context for this session.

Returns:

  • (String) —

    The directory used as the working context for this session.



# File 'lib/ollama_chat/database/models/session.rb', line 90