module AiPlanSession
Constants
- ADMIN_DENIAL
- PENDING_KEY
- PLUGIN
Public Instance Methods
Source
# File lib/ai/plan_session.rb, line 75 def admin_allowed?(network:, channel:, nick:, is_op:, store: RabbotDb) RabbotDb.user_allowed_at_level?( network: network, channel: channel, nick: nick, is_op: is_op, minimum: RabbotDb::LEVEL_ADMIN ) end
Source
# File lib/ai/plan_session.rb, line 85 def admin_denial_message ADMIN_DENIAL end
Source
# File lib/ai/plan_session.rb, line 61 def clear_pending!(network:, channel:, store: RabbotDb) network = store.normalize_network(network) channel = store.normalize_channel(channel) return if channel.empty? store.set_plugin_json( plugin: PLUGIN, key: PENDING_KEY, value: nil, network: network, channel: channel ) end
Source
# File lib/ai/plan_session.rb, line 125 def deserialize_questions(raw) Array(raw).map do |question| { id: question["id"].to_s, prompt: question["prompt"].to_s, options: Array(question["options"]).map do |option| { id: option["id"].to_s, label: option["label"].to_s } end, allow_multiple: question["allow_multiple"] == true } end end
Source
# File lib/ai/plan_session.rb, line 141 def option_label(options, index) option = options[index] return nil unless option label = option[:label].to_s.strip return label unless label.empty? option[:id].to_s.strip end
Source
# File lib/ai/plan_session.rb, line 37 def pending(network:, channel:, store: RabbotDb) network = store.normalize_network(network) channel = store.normalize_channel(channel) return nil if channel.empty? raw = store.get_plugin_json( plugin: PLUGIN, key: PENDING_KEY, network: network, channel: channel, default: nil ) session_id = raw.is_a?(Hash) ? raw["session_id"].to_s.strip : "" return nil if session_id.empty? { session_id: raw["session_id"].to_s, asker: raw["asker"].to_s, question: raw["question"].to_s, questions: deserialize_questions(raw["questions"]), model: raw["model"].to_s } end
Source
# File lib/ai/plan_session.rb, line 89 def resolve_answer(text, questions:) response = text.to_s.strip return response if response.empty? question = Array(questions).first return response unless question && question[:options].is_a?(Array) && question[:options].any? if response.match?(/\A[a-z]\z/i) index = response.downcase[0].ord - "a".ord return option_label(question[:options], index) || response end if response.match?(/\A\d+\z/) index = response.to_i - 1 return option_label(question[:options], index) || response end response end
Source
# File lib/ai/plan_session.rb, line 17 def save_pending!(network:, channel:, session_id:, asker:, question:, questions:, model:, store: RabbotDb) network = store.normalize_network(network) channel = store.normalize_channel(channel) return if network.empty? || channel.empty? || session_id.to_s.strip.empty? store.set_plugin_json( plugin: PLUGIN, key: PENDING_KEY, value: { "session_id" => session_id.to_s, "asker" => asker.to_s.strip, "question" => question.to_s.strip, "questions" => serialize_questions(questions), "model" => model.to_s.strip }, network: network, channel: channel ) end
Source
# File lib/ai/plan_session.rb, line 109 def serialize_questions(questions) Array(questions).map do |question| { "id" => question[:id].to_s, "prompt" => question[:prompt].to_s, "options" => Array(question[:options]).map do |option| { "id" => option[:id].to_s, "label" => option[:label].to_s } end, "allow_multiple" => question[:allow_multiple] == true } end end