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

Inherits:
Object
  • Object
show all
Includes:
Duplicatable, SessionLocking
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 SessionLocking

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

Class Method Details

.with_defaults(chat) ⇒ 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:

Returns:



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
176
177
178
179
180
181
182
# File 'lib/ollama_chat/database/models/session.rb', line 151

def self.with_defaults(chat)
  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:                  "New Session #{Tins::Token.new}",
    current_model:         ,
    current_collection:    chat.initial_collection,
    default_persona_name:  chat.initial_persona_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,
    working_directory:     Dir.pwd,
    messages:              '',
  }
  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



49
50
51
# File 'lib/ollama_chat/database/models/session.rb', line 49

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 68

#count_messagesInteger

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

Returns:

  • (Integer)

    the number of messages stored in the session



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

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 68

#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 68

#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 68

#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 68

#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 68

#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 68

#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 68

#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 68

#estimate_tokensOllamaChat::TokenEstimator::Estimate

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

Returns:



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

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

#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 68

#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 68

#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 68

#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 68

#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 68

#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 68

#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 68

#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 68

#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 68

#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 68

#think_mode=(value) ⇒ String

Returns The current thinking mode (must be one of THINK_MODE_STATES).

Returns:

  • (String)

    The current thinking mode (must be one of THINK_MODE_STATES).



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

#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 68

#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 68

#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 68

#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 68

#validateObject

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.



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

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

  validates_presence :context_format
  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 68

#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 68