module AiChannelLog
Constants
- AGENT_FOOTER_PATTERN
- CHANNEL_LOG_KEY
- MAX_CHANNEL_LOG
- MENU_HEADING_PATTERN
- TAG_PATTERN
Public Instance Methods
Source
# File lib/ai/channel_log.rb, line 27 def agent_answer_card?(text) strip_irc_codes(text).match?(AGENT_FOOTER_PATTERN) end
Source
# File lib/ai/channel_log.rb, line 46 def append_entry(network:, channel:, nick:, text:, at: Time.now, store: RabbotDb, from_bot: false, is_command: false) network = store.normalize_network(network) channel = store.normalize_channel(channel) nick = store.normalize_nick(nick) text = sanitize_log_text(text) return if channel.empty? || nick.empty? || text.empty? entries = Array( AiPluginStorage.get_json( key: CHANNEL_LOG_KEY, network: network, channel: channel, default: [], store: store ) ) entry = { "nick" => nick, "text" => text, "at" => store.iso_time(at) } entry["from_bot"] = true if from_bot entry["is_command"] = true if is_command entries << entry entries = entries.last(MAX_CHANNEL_LOG) AiPluginStorage.set_json( key: CHANNEL_LOG_KEY, value: entries, network: network, channel: channel, store: store ) end
Source
# File lib/ai/channel_log.rb, line 94 def entries(network:, channel:, store: RabbotDb) network = store.normalize_network(network) channel = store.normalize_channel(channel) return [] if channel.empty? Array( AiPluginStorage.get_json( key: CHANNEL_LOG_KEY, network: network, channel: channel, default: [], store: store ) ).map { |entry| normalize_entry(entry) } end
Source
# File lib/ai/channel_log.rb, line 110 def normalize_entry(entry) { nick: entry["nick"].to_s, text: entry["text"].to_s, at: entry["at"].to_s, from_bot: entry["from_bot"] == true, is_command: entry["is_command"] == true } end
Source
# File lib/ai/channel_log.rb, line 31 def plain_log_text(text) plain_lines = strip_irc_codes(text).split("\n").map(&:strip).reject(&:empty?) return "" if plain_lines.empty? return plain_lines.join("\n") unless plain_lines.first.match?(TAG_PATTERN) body = plain_lines.drop(1) return plain_lines.join("\n") if body.empty? if body.last&.match?(/\([^)]*\)/) body[-1] = body.last.sub(/\s+\([^)]+\)\z/, "").strip end body[-1] = body.last.sub(/\s+\S+\s+ยท\s+[\d.]+\s*tokens.*\z/i, "").strip if body.last body.reject { |line| line.match?(MENU_HEADING_PATTERN) }.join("\n").strip end
Source
# File lib/ai/channel_log.rb, line 82 def record_bot_answer!(network:, channel:, nick:, text:, at: Time.now, store: RabbotDb) append_entry( network: network, channel: channel, nick: nick, text: text, at: at, store: store, from_bot: true ) end
Source
# File lib/ai/channel_log.rb, line 120 def sanitize_log_text(text) text.to_s.gsub(/[\x00-\x1f]/, " ").squeeze(" ").strip end
Source
# File lib/ai/channel_log.rb, line 21 def strip_irc_codes(text) text.to_s .gsub(/\x03\d{1,2}(?:,\d{1,2})?/, "") .delete("\x03\x02\x1f") end