Module: OllamaChat::PersonaeManagement
- Included in:
- Chat
- Defined in:
- lib/ollama_chat/personae_management.rb
Overview
Module for managing personae in Ollama chat application
This module provides functionality to manage persona files, including creating, reading, updating, and deleting persona definitions stored as Markdown files in the personae directory.
Instance Attribute Summary collapse
-
#default_persona_name ⇒ String?
readonly
The default_persona_name method returns the name of the default persona.
Instance Method Summary collapse
-
#add_persona ⇒ Object
private
Creates a new persona file interactively.
-
#ask_to_set_default_persona_name(persona_name) ⇒ Boolean
private
Interactively asks the user if they want to set the specified persona as the current default for the session.
-
#assistant ⇒ String, ...
Returns the name of the current default persona.
-
#available_personae ⇒ Array<String>
private
Returns a sorted list of available persona file names.
-
#available_personae_names ⇒ Array<SearchUI::Wrapper>
private
Retrieves a list of available personae, decorated with their favourite status.
-
#backup_persona ⇒ Object
private
Backs up the content of a selected persona file.
-
#choose_persona(chosen: nil, none: false, prompt: 'Select a persona: %s') ⇒ String, ...
private
Interactive method to select a persona from a list.
-
#convert_json_character_to_markdown(character, persona_name) ⇒ String
private
Transforms raw character data (JSON or YAML) into a high-fidelity, structured Markdown persona profile using the persona architect prompt and the current persona template.
-
#default_persona ⇒ Pathname?
private
The default_persona method returns the path to the default persona file.
-
#default_persona_profile ⇒ String?
Retrieves the formatted roleplay prompt for the current default persona.
-
#delete_persona ⇒ String
private
Interactive method to delete an existing persona with backup functionality.
-
#determine_valid_new_name_for_persona(action) ⇒ String?
private
Interactively determines a valid, non-conflicting name for a new persona.
-
#duplicate_persona ⇒ self?
private
Interactively duplicates an existing persona profile to a new name.
-
#edit_persona ⇒ String?
private
Interactive method to edit an existing persona file.
-
#export_persona ⇒ self?
private
Interactively exports a persona profile to a specified file.
-
#import_persona(pathname) ⇒ String?
private
Imports a persona from a Markdown file, prompting for a new name.
-
#import_persona_from_json(json_data) ⇒ String?
private
Imports a character persona from JSON data, prompts for a name, and saves the resulting Markdown profile to disk.
-
#info_persona ⇒ Object
private
Displays detailed information about a selected persona.
-
#initial_persona_name ⇒ String?
The initial_persona_name method retrieves the initial persona for the chat session.
-
#list_personae(output: STDOUT) ⇒ Object
private
Lists all available persona names in a formatted table.
-
#load_persona_file(persona) ⇒ Array<Pathname, String>
private
Loads a persona file from disk.
-
#load_personae ⇒ Object
private
Interactive method to load multiple personae for use.
-
#pathname_to_persona_name(pathname) ⇒ String
private
Converts a persona pathname to its prompt name.
-
#persona_backup_pathname(persona) ⇒ Pathname
private
Generates the backup pathname for a persona file with timestamp.
-
#persona_description(persona, substitute_variables: false) ⇒ String?
private
Generates a formatted description of a persona, including its path and profile.
-
#persona_exist?(persona_name) ⇒ Boolean
Checks whether a persona file exists on disk.
-
#persona_name_to_pathname(persona_name) ⇒ Pathname
private
Converts a persona prompt name to its full filesystem pathname.
-
#persona_name_with_favourite(name, favourited) ⇒ SearchUI::Wrapper
private
Helper to wrap a persona name with its favourite status for the UI.
-
#personae_backup_directory ⇒ Pathname
private
Returns the directory path for persona backup files.
-
#personae_directory ⇒ Pathname
private
Returns the directory path where persona files are stored.
-
#personae_result(personae) ⇒ String?
private
Compiles the descriptions for one or more personae into a single string.
-
#play_persona(persona) ⇒ String
private
Generates the roleplay prompt string for a persona.
-
#select_persona_path(no_prefill: false) ⇒ String?
private
Prompts the user to select a persona, copies its filesystem path to the clipboard, and sets it as the prefill prompt for the next interaction.
-
#set_default_persona ⇒ String?
private
Interactively selects a persona and sets it as the default for the session.
-
#set_default_persona_name(persona_name) ⇒ String?
private
Sets the default persona name and updates the session.
-
#setup_persona_from_session ⇒ Pathname, ...
private
Initializes the default persona for the current chat session.
-
#setup_personae_directory ⇒ Object
private
Creates the personae directory structure if it doesn't already exist.
-
#substitute_variables(profile) ⇒ String
private
The substitute_variables method handles the substitution of variables in profiles.
-
#sync_trigger ⇒ Object
private
Synchronizes the session's trigger collection to match the current default persona.
Instance Attribute Details
#default_persona_name ⇒ String? (readonly)
The default_persona_name method returns the name of the default persona.
40 41 42 |
# File 'lib/ollama_chat/personae_management.rb', line 40 def default_persona_name @default_persona_name end |
Instance Method Details
#add_persona ⇒ Object (private)
Creates a new persona file interactively.
The method prompts the user to enter a name for the persona, creates an
empty Markdown file with that name in the personae directory (if it does
not already exist), opens the file in the configured editor, and finally
returns the result of calling #personae_result on the created file.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/ollama_chat/personae_management.rb', line 213 def add_persona name = determine_valid_new_name_for_persona('to add') or return sources = %w[ [CLIPBOARD] [FILES] [EMPTY/MANUAL] ] chosen_source = choose_entry(sources, prompt: 'Where shall we source the persona from? %s') chosen_source or return content = case chosen_source when '[CLIPBOARD]' perform_paste_from_clipboard(edit: false) when '[FILES]' patterns = switch_history(:patterns) do ask?( prompt: "❓ Enter file patterns to load file, C-u ⇒ new, C-c ⇒ cancel: ", prefill: '**/*.{txt,md}' ) end patterns.nil? ? (return) : (patterns.present? ? load_prompt_from_file(patterns) : nil) else nil end pathname = personae_directory + "#{name}.md" final_content = content.full? || prompt(:persona).to_s pathname.write final_content edit_file(pathname) nil end |
#ask_to_set_default_persona_name(persona_name) ⇒ Boolean (private)
Interactively asks the user if they want to set the specified persona as the current default for the session.
If the user confirms, the default persona is updated via
set_default_persona_name.
684 685 686 687 688 689 690 691 692 693 694 695 |
# File 'lib/ollama_chat/personae_management.rb', line 684 def ask_to_set_default_persona_name(persona_name) yes = confirm?( prompt: "🔔 Set the new persona promt as current default persona? (y/n) ", yes: /\Ay/i ) if yes set_default_persona_name(persona_name) true else false end end |
#assistant ⇒ String, ...
Returns the name of the current default persona.
If the default persona is set to :none or is not configured, it returns nil.
31 32 33 34 35 |
# File 'lib/ollama_chat/personae_management.rb', line 31 def assistant if default_persona_name && default_persona_name != :none default_persona_name end end |
#available_personae ⇒ Array<String> (private)
Returns a sorted list of available persona file names.
This method scans the personae directory for Markdown files and returns their basenames sorted alphabetically.
179 180 181 |
# File 'lib/ollama_chat/personae_management.rb', line 179 def available_personae personae_directory.glob('*.md').map { pathname_to_persona_name(_1) } end |
#available_personae_names ⇒ Array<SearchUI::Wrapper> (private)
Retrieves a list of available personae, decorated with their favourite status.
199 200 201 202 203 204 205 |
# File 'lib/ollama_chat/personae_management.rb', line 199 def available_personae_names favs = all_favourited('persona') personae_directory.glob('*.md').map(&:basename).sort.map { |bn| persona_name = bn.sub_ext('').to_s persona_name_with_favourite(persona_name, favs[persona_name]) } end |
#backup_persona ⇒ Object (private)
Backs up the content of a selected persona file.
Prompts the user to select a persona from the available list. If a persona
is selected, its current content is read and saved to a designated backup
location using File.write. This ensures a safe copy is preserved before
any modifications are made to the original file.
332 333 334 335 336 337 338 339 340 |
# File 'lib/ollama_chat/personae_management.rb', line 332 def backup_persona if persona = choose_persona(prompt: 'Which persona should be safely archived? %s') pathname = persona_name_to_pathname(persona) old_content = pathname.read backup_pathname = persona_backup_pathname(persona) backup_pathname.write(old_content) feedback("Wrote backup of #{persona.to_s} to #{backup_pathname.to_s.inspect}.", type: :info) end end |
#choose_persona(chosen: nil, none: false, prompt: 'Select a persona: %s') ⇒ String, ... (private)
Interactive method to select a persona from a list.
Allows the user to choose a persona from available options or exit. Selected persona is returned if successful, nil if user exits.
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/ollama_chat/personae_management.rb', line 429 def choose_persona(chosen: nil, none: false, prompt: 'Select a persona: %s') personae_list = available_personae_names. reject { chosen&.member?(_1) } if personae_list.empty? feedback("No personae defined.", type: :warn) return end personae_list.unshift('[NONE]') if none personae_list.unshift('[EXIT]') case persona = choose_entry(personae_list, prompt:) when '[EXIT]', nil feedback("Exiting chooser.") return when '[NONE]' :none else persona.value end end |
#convert_json_character_to_markdown(character, persona_name) ⇒ String (private)
Transforms raw character data (JSON or YAML) into a high-fidelity, structured Markdown persona profile using the persona architect prompt and the current persona template.
This method leverages the LLM to interpret raw attributes and expand them into evocative prose, ensuring the final output conforms to the system's standard persona structure. It also normalizes placeholders: {user} is converted to %user for runtime personalization, and {char} is replaced with the actual character name provided.
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 |
# File 'lib/ollama_chat/personae_management.rb', line 634 def convert_json_character_to_markdown(character, persona_name) Infobar.busy( label: 'Converting character…', frames: :braille7, output: STDOUT, ) do generate( prompt: prompt(:persona_architect).to_s. named_placeholders_interpolate( { character:, persona_template: prompt(:persona).to_s } ) ).gsub(/{{user}}/i, '%{user}').gsub(/{{char}}/i, persona_name) end end |
#default_persona ⇒ Pathname? (private)
The default_persona method returns the path to the default persona file.
143 144 145 146 147 |
# File 'lib/ollama_chat/personae_management.rb', line 143 def default_persona if default_persona_name && default_persona_name != :none personae_directory.join(default_persona_name).sub_ext('.md') end end |
#default_persona_profile ⇒ String?
Retrieves the formatted roleplay prompt for the current default persona.
18 19 20 21 22 |
# File 'lib/ollama_chat/personae_management.rb', line 18 def default_persona_profile if persona = default_persona and persona.exist? play_persona(persona) end end |
#delete_persona ⇒ String (private)
Interactive method to delete an existing persona with backup functionality.
Prompts the user to select a persona, asks for confirmation, and creates a timestamped backup of the persona file before deletion.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/ollama_chat/personae_management.rb', line 266 def delete_persona if persona = choose_persona(prompt: 'Which persona is no longer needed? %s') pathname = persona_name_to_pathname(persona) backup_pathname = persona_backup_pathname(persona) if pathname.exist? feedback("Deleting '#{bold{persona}}'...") feedback("Backup will be saved to: #{backup_pathname}") if confirm?(prompt: "🔔 Are you sure? (y/n) ", yes: /\Ay/i) FileUtils.mv pathname, backup_pathname default_persona_name == persona and set_default_persona_name(:none) feedback("Persona #{bold{persona}} deleted successfully", type: :info) self else feedback("Deletion denied.", type: :denied) return end else feedback("Persona not found.", type: :warn) return end end end |
#determine_valid_new_name_for_persona(action) ⇒ String? (private)
Interactively determines a valid, non-conflicting name for a new persona.
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 |
# File 'lib/ollama_chat/personae_management.rb', line 555 def determine_valid_new_name_for_persona(action) switch_history(:persona_name) do persona_name = nil loop do persona_name = ask?( prompt: "❓ Enter new persona prompt name #{action}, C-c ⇒ cancel: " ) if persona_name.nil? feedback("Cancelled.", type: :cancel) return nil end if persona_name_to_pathname(persona_name).exist? feedback("Persona prompt named #{bold{persona_name}} already exists.", type: :warn) else break end end persona_name end end |
#duplicate_persona ⇒ self? (private)
Interactively duplicates an existing persona profile to a new name.
The process follows these steps:
- Prompts the user to select a source persona via
choose_persona. - Prompts the user to enter a unique name for the duplicate via
determine_valid_new_name_for_persona. - Copies the content from the source persona file to the new persona file.
586 587 588 589 590 591 592 593 |
# File 'lib/ollama_chat/personae_management.rb', line 586 def duplicate_persona persona = choose_persona(prompt: 'Which persona shall serve as the blueprint? %s') or return pathname = persona_name_to_pathname(persona) new_persona_name = determine_valid_new_name_for_persona('to ducplicate as') or return new_pathname = persona_name_to_pathname(new_persona_name) new_pathname.write(pathname.read) self end |
#edit_persona ⇒ String? (private)
Interactive method to edit an existing persona file.
Prompts the user to select a persona, opens it for editing, backups the old content, and returns the result after editing.
297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/ollama_chat/personae_management.rb', line 297 def edit_persona if persona = choose_persona(prompt: 'Which persona needs some polishing? %s') pathname = persona_name_to_pathname(persona) old_content = pathname.read if edit_file(pathname) changed = pathname.read != old_content if changed persona_backup_pathname(persona).write(old_content) ask_to_set_default_persona_name(persona) end persona end end end |
#export_persona ⇒ self? (private)
Interactively exports a persona profile to a specified file.
The process follows these steps:
- Prompts the user to select a persona via
choose_persona. - Displays the persona's current content to the terminal.
- Prompts for a destination filename via
determine_valid_output_filename. - Writes the persona content to the chosen file.
661 662 663 664 665 666 667 668 669 670 671 672 |
# File 'lib/ollama_chat/personae_management.rb', line 661 def export_persona persona = choose_persona(prompt: 'Which persona are you taking with you? %s') or return pathname = persona_name_to_pathname(persona) content = pathname.read use_pager do |output| feedback(kramdown_ansi_parse(content + "\n---"), output:) filename = determine_valid_output_filename('to write to') or return filename.write(content) feedback("Persona #{persona.inspect} was exported as #{filename.to_path.inspect}?", output:) end self end |
#import_persona(pathname) ⇒ String? (private)
Imports a persona from a Markdown file, prompting for a new name.
599 600 601 602 603 604 605 |
# File 'lib/ollama_chat/personae_management.rb', line 599 def import_persona(pathname) content = pathname.read persona_name = determine_valid_new_name_for_persona('to import') or return persona_pathname = persona_name_to_pathname(persona_name) persona_pathname.write(content) persona_name end |
#import_persona_from_json(json_data) ⇒ String? (private)
Imports a character persona from JSON data, prompts for a name, and saves the resulting Markdown profile to disk.
613 614 615 616 617 618 619 |
# File 'lib/ollama_chat/personae_management.rb', line 613 def import_persona_from_json(json_data) persona_name = determine_valid_new_name_for_persona('to import from JSON/PNG') or return markdown = convert_json_character_to_markdown(json_data, persona_name) persona_pathname = persona_name_to_pathname(persona_name) persona_pathname.write(markdown) persona_name end |
#info_persona ⇒ Object (private)
Displays detailed information about a selected persona.
Shows the persona's profile using kramdown formatting with ansi parsing.
370 371 372 373 374 375 376 377 |
# File 'lib/ollama_chat/personae_management.rb', line 370 def info_persona if persona = choose_persona(prompt: 'Who would you like to learn more about? %s') description = persona_description(persona) or return use_pager do |output| output.puts kramdown_ansi_parse(description) end end end |
#initial_persona_name ⇒ String?
The initial_persona_name method retrieves the initial persona for the chat session.
11 12 13 |
# File 'lib/ollama_chat/personae_management.rb', line 11 def initial_persona_name session&.default_persona_name || config.persona&.default_name end |
#list_personae(output: STDOUT) ⇒ Object (private)
Lists all available persona names in a formatted table.
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/ollama_chat/personae_management.rb', line 382 def list_personae(output: STDOUT) use_pager do |output| personae = available_personae if personae.empty? feedback("No personae defined.", type: :warn) return end favs = all_favourited('persona') table = Terminal::Table.new table.style = { all_separators: true, border: :unicode_round, } table.headings = %w[ NAME SIZE #TOK ].map { |header| bold { header } } personae.map do |persona_name| pathname = persona_name_to_pathname(persona_name) pathname.exist? or next [ pathname, pathname.size ] end.compact.sort_by(&:last).reverse_each do |pathname, size_bytes| persona_name = pathname.basename.sub_ext('').to_s es = OllamaChat::TokenEstimator.estimate(size_bytes) is_default = default_persona_name == persona_name display_name = prefix_favourite(is_default ? bold { persona_name } : persona_name, favs[persona_name]) table << [ display_name, es.bytes_formatted, es.tokens_formatted, ] end table.align_column 1, :right table.align_column 2, :right output.puts table end end |
#load_persona_file(persona) ⇒ Array<Pathname, String> (private)
Loads a persona file from disk.
496 497 498 499 500 501 |
# File 'lib/ollama_chat/personae_management.rb', line 496 def load_persona_file(persona) pathname = persona_name_to_pathname(persona) if pathname.exist? return pathname, pathname.read end end |
#load_personae ⇒ Object (private)
Interactive method to load multiple personae for use.
Allows sequential selection of multiple personae. Returns JSON results for each loaded persona.
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/ollama_chat/personae_management.rb', line 453 def load_personae chosen = Set[] choose_with_state do while persona = choose_persona(chosen:, prompt: 'Who else should join the conversation? %s') persona == :none and next chosen << persona end end if chosen.empty? feedback("No persona loaded.", type: :info) return end personae_result(chosen) end |
#pathname_to_persona_name(pathname) ⇒ String (private)
Converts a persona pathname to its prompt name.
547 548 549 |
# File 'lib/ollama_chat/personae_management.rb', line 547 def pathname_to_persona_name(pathname) pathname.basename.sub_ext('').to_s end |
#persona_backup_pathname(persona) ⇒ Pathname (private)
The timestamp ensures each backup has a unique filename when multiple backups of the same persona exist
Generates the backup pathname for a persona file with timestamp.
Creates a unique backup filename with the persona name, timestamp, and .md.bak extension. The timestamp format is YYYYMMDDHHMMSS for precise identification of backup versions.
254 255 256 257 |
# File 'lib/ollama_chat/personae_management.rb', line 254 def persona_backup_pathname(persona) = Time.now.strftime('%Y%m%d%H%M%S') personae_backup_directory + (persona + ?- + + '.md.bak') end |
#persona_description(persona, substitute_variables: false) ⇒ String? (private)
Generates a formatted description of a persona, including its path and profile.
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/ollama_chat/personae_management.rb', line 348 def persona_description(persona, substitute_variables: false) persona_path, persona_profile = load_persona_file(persona) if substitute_variables persona_profile = self.substitute_variables(persona_profile) end persona_profile or return <<~EOT # Persona #{persona} File #{persona_path.to_path} --- #{persona_profile} --- EOT end |
#persona_exist?(persona_name) ⇒ Boolean
Checks whether a persona file exists on disk.
Resolves the given persona name to its file path via
persona_name_to_pathname and tests for existence.
51 52 53 54 55 56 57 |
# File 'lib/ollama_chat/personae_management.rb', line 51 def persona_exist?(persona_name) if pathname = persona_name_to_pathname(persona_name) and pathname.exist? true else false end end |
#persona_name_to_pathname(persona_name) ⇒ Pathname (private)
Converts a persona prompt name to its full filesystem pathname.
539 540 541 |
# File 'lib/ollama_chat/personae_management.rb', line 539 def persona_name_to_pathname(persona_name) personae_directory.join(persona_name).sub_ext('.md') end |
#persona_name_with_favourite(name, favourited) ⇒ SearchUI::Wrapper (private)
Helper to wrap a persona name with its favourite status for the UI.
189 190 191 192 |
# File 'lib/ollama_chat/personae_management.rb', line 189 def persona_name_with_favourite(name, favourited) display = prefix_favourite(name, favourited) SearchUI::Wrapper.new(name, display:) end |
#personae_backup_directory ⇒ Pathname (private)
Returns the directory path for persona backup files.
77 78 79 |
# File 'lib/ollama_chat/personae_management.rb', line 77 def personae_backup_directory personae_directory + 'backups' end |
#personae_directory ⇒ Pathname (private)
The directory is created automatically if it doesn't exist
Returns the directory path where persona files are stored.
The path is constructed using the XDG_CONFIG_HOME environment variable for platform-agnostic configuration storage.
69 70 71 |
# File 'lib/ollama_chat/personae_management.rb', line 69 def personae_directory OC::XDG_CONFIG_HOME + 'personae' end |
#personae_result(personae) ⇒ String? (private)
Compiles the descriptions for one or more personae into a single string.
Loads the profile for each persona and concatenates their descriptions.
477 478 479 480 481 482 483 484 485 486 487 488 |
# File 'lib/ollama_chat/personae_management.rb', line 477 def personae_result(personae) personae = Array(personae) result = +'' personae.each do |persona| description = persona_description(persona, substitute_variables: true) or next result << description << "\n" end result.full? end |
#play_persona(persona) ⇒ String (private)
Generates the roleplay prompt string for a persona.
Creates a formatted prompt string that includes the persona name and profile.
522 523 524 525 526 527 528 529 530 531 532 533 |
# File 'lib/ollama_chat/personae_management.rb', line 522 def play_persona(persona) pathname, profile = load_persona_file(persona) profile = substitute_variables(profile) profile_intro = <<~EOT Roleplay as persona %{persona} (no nead to read the file) loaded from %{pathname} %{profile} EOT profile_intro % { persona:, pathname:, profile: } end |
#select_persona_path(no_prefill: false) ⇒ String? (private)
Prompts the user to select a persona, copies its filesystem path to the clipboard, and sets it as the prefill prompt for the next interaction.
318 319 320 321 322 323 324 |
# File 'lib/ollama_chat/personae_management.rb', line 318 def select_persona_path(no_prefill: false) persona = choose_persona(prompt: "Which persona's path do you need? %s") or return path = persona_name_to_pathname(persona).to_s perform_copy_to_clipboard(text: path, edit: false) no_prefill or self.prefill_prompt = path path end |
#set_default_persona ⇒ String? (private)
Interactively selects a persona and sets it as the default for the session.
This method prompts the user to choose a persona from the available list
using choose_persona. If a valid persona is selected, it updates the
session and the internal state via set_default_persona_name.
133 134 135 136 137 |
# File 'lib/ollama_chat/personae_management.rb', line 133 def set_default_persona if persona = choose_persona(none: true, prompt: 'Who would you like to talk to today? %s') set_default_persona_name(persona) end end |
#set_default_persona_name(persona_name) ⇒ String? (private)
Sets the default persona name and updates the session.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ollama_chat/personae_management.rb', line 95 def set_default_persona_name(persona_name) if persona_name.present? && persona_name != :none @default_persona_name = Pathname.new(persona_name).basename.sub_ext('').to_path else @default_persona_name = nil end @session.update(default_persona_name:) sync_trigger .set_system_prompt(session&.current_system_prompt.full?) default_persona_name end |
#setup_persona_from_session ⇒ Pathname, ... (private)
Initializes the default persona for the current chat session.
This method ensures that a default persona is configured at startup:
- If a default persona is already set, it returns immediately.
- Otherwise, it attempts to retrieve the initial persona prompt name from the session and verifies if the corresponding Markdown file exists.
- If a valid file is found, it sets it as the default persona.
- The
ensureblock guarantees that the session always has a default persona assigned, falling back to:noneif no valid persona is found.
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/ollama_chat/personae_management.rb', line 161 def setup_persona_from_session default_persona and return if persona = initial_persona_name and persona_pathname = personae_directory + (persona + '.md') and persona_pathname.exist? then set_default_persona_name(persona_pathname) end ensure default_persona or set_default_persona_name(:none) end |
#setup_personae_directory ⇒ Object (private)
Creates the personae directory structure if it doesn't already exist.
This method ensures both the main personae directory and the backups subdirectory exist for proper file organization.
85 86 87 |
# File 'lib/ollama_chat/personae_management.rb', line 85 def setup_personae_directory FileUtils.mkdir_p personae_backup_directory end |
#substitute_variables(profile) ⇒ String (private)
The substitute_variables method handles the substitution of variables in profiles. It replaces placeholders with actual values.
509 510 511 512 513 |
# File 'lib/ollama_chat/personae_management.rb', line 509 def substitute_variables(profile) profile = profile.gsub(/%(?=[^{])/, '%%') profile = profile % { user: } profile end |
#sync_trigger ⇒ Object (private)
Synchronizes the session's trigger collection to match the current default persona.
When a persona is set, the trigger is re-keyed (or seeded) to
memory-<persona>, preserving any existing trigger configuration.
When no persona is active, the trigger is set to nil.
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/ollama_chat/personae_management.rb', line 113 def sync_trigger unless @default_persona_name session.update(trigger: nil) unless session.trigger.nil? return end collection = create_memory_collection(@default_persona_name) return if session.trigger&.keys&.first == collection cfg = session.trigger&.values&.first or cfg = chat.config.trigger.to_h.stringify_keys_recursive session.update(trigger: { collection => cfg }) end |