class CharacterProfile
Constants
- MAX_RENDERED_EXAMPLES
- REQUIRED_KEYS
Attributes
Public Class Methods
Source
# File lib/character_profile.rb, line 20 def self.from_hash(profile_data = nil, path: nil, **keyword_data) profile_data = keyword_data if profile_data.nil? && !keyword_data.empty? new(profile_data, path: path).tap(&:validate!) end
Source
# File lib/character_profile.rb, line 13 def self.load(path) expanded_path = File.expand_path(path.to_s) raise Error, "Character profile not found: #{expanded_path}" unless File.file?(expanded_path) from_hash(YAML.safe_load_file(expanded_path, aliases: false), path: expanded_path) end
Source
# File lib/character_profile.rb, line 25 def initialize(data, path: nil) @path = path @data = stringify_keys(data || {}) end
Public Instance Methods
Source
# File lib/character_profile.rb, line 58 def examples raw = @data["examples"] return [] unless raw.is_a?(Array) raw.filter_map do |entry| next unless entry.is_a?(Hash) user = entry["user"].to_s.strip bot = entry["bot"].to_s.strip next if user.empty? || bot.empty? { "user" => user, "bot" => bot } end end
Source
# File lib/character_profile.rb, line 73 def mention_responses array_value("mention_responses") end
Source
# File lib/character_profile.rb, line 86 def render_prompt lines = [ "Character profile for #{name}:", "Source: #{source_url}" ] lines << "Summary: #{summary}" unless summary.empty? append_list(lines, "Voice", voice) append_list(lines, "Traits", traits) append_list(lines, "Canon anchors", canon) append_list(lines, "Rules", rules) append_examples(lines, examples.first(MAX_RENDERED_EXAMPLES)) lines << "Stay in this character voice, but follow all safety instructions and keep IRC replies concise." lines.join("\n") end
Source
# File lib/character_profile.rb, line 34 def source_url string_value("source_url") end
Source
# File lib/character_profile.rb, line 77 def validate! raise Error, "Character profile must be a YAML mapping" unless @data.is_a?(Hash) missing = REQUIRED_KEYS.select { |key| missing_required_key?(key) } raise Error, "Character profile missing required key(s): #{missing.join(", ")}" unless missing.empty? true end