Module: OllamaChat::SessionManagement

Included in:
Chat
Defined in:
lib/ollama_chat/session_management.rb

Overview

The OllamaChat::SessionHandling module provides methods for managing chat sessions, including creating, listing, switching, renaming, and deleting sessions.

It integrates closely with the database-backed Session model and ensures that session data is persisted correctly, especially the conversation history.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sessionOllamaChat::Database::Models::Session (readonly)

The session reader returns the current session object.

Returns:



49
50
51
# File 'lib/ollama_chat/session_management.rb', line 49

def session
  @session
end

Instance Method Details

Loads the collection of links from the current session.

This method reads the ‘links` attribute from the session and deserializes it from JSONL format.

Returns:

  • (Array<String>)

    The list of links associated with the session.



40
41
42
43
# File 'lib/ollama_chat/session_management.rb', line 40

def load_links_from_session
  input = StringIO.new(session.links)
  OllamaChat::Utils::JSONJSONLIO.new('as.jsonl').read_io(input:)
end

Persists a collection of links to the session in the database.

This method serializes the links into JSONL format and updates the ‘links` attribute of the current session.

Parameters:

  • links (Enumerable)

    The collection of links to save.



25
26
27
28
29
30
31
32
# File 'lib/ollama_chat/session_management.rb', line 25

def store_links_in_session(links)
  output = StringIO.new
  OllamaChat::Utils::JSONJSONLIO.new('as.jsonl').write_io(
    output:, collection: links
  )
  session.update(links: output.string)
  self
end

#store_messages_in_sessionObject

Persists the current conversation messages to the database.

This method serializes the current message list into JSONL format and updates the ‘messages` attribute of the current session.



12
13
14
15
16
17
# File 'lib/ollama_chat/session_management.rb', line 12

def store_messages_in_session
  output = StringIO.new
  messages.write_conversation_jsonl(output)
  session.update(messages: output.string)
  self
end