Module: RubyTodo::AIAssistant::TaskResponseProcessor

Included in:
TaskCreatorCombined
Defined in:
lib/ruby_todo/ai_assistant/task_creator.rb,
lib/ruby_todo/ai_assistant/task_response_processor.rb

Overview

Module for parsing and processing AI responses

Instance Method Summary collapse

Instance Method Details

#create_task_from_details(notebook_name, task_details) ⇒ Object

Create a task from the generated details



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/ruby_todo/ai_assistant/task_creator.rb', line 172

def create_task_from_details(notebook_name, task_details)
  # Ensure we have a valid notebook
  notebook = RubyTodo::Notebook.find_by(name: notebook_name)
  unless notebook
    # Try to create the notebook if it doesn't exist
    RubyTodo::CLI.start(["notebook:create", notebook_name])
    notebook = RubyTodo::Notebook.find_by(name: notebook_name)
  end

  # Use default notebook as fallback if we couldn't create or find the specified one
  unless notebook
    default_notebook = RubyTodo::Notebook.default_notebook
    notebook_name = default_notebook ? default_notebook.name : "default"
    # Create default notebook if it doesn't exist
    unless RubyTodo::Notebook.find_by(name: notebook_name)
      RubyTodo::CLI.start(["notebook:create", notebook_name])
    end
  end

  # Display the improved task title if there's a significant difference
  if task_details["title"] && task_details["description"] &&
     task_details["title"] != task_details["description"] &&
     task_details["title"] != "Task from #{task_details["description"]}"
    say "✨ Enhanced title: \"#{task_details["title"]}\"", :green
  end

  # Ensure priority is properly normalized before passing to CLI
  task_details["priority"] = normalize_priority(task_details["priority"]) if task_details["priority"]

  args = ["task:add", notebook_name, task_details["title"]]
  args << "--description" << task_details["description"] if task_details["description"]
  args << "--priority" << task_details["priority"] if task_details["priority"]
  args << "--tags" << task_details["tags"] if task_details["tags"]

  RubyTodo::CLI.start(args)
rescue StandardError => e
  say "Error creating task: #{e.message}".red
  say "Attempting simplified task creation...".yellow

  # Fallback to simplified command
  begin
    RubyTodo::CLI.start(["task:add", notebook_name, task_details["title"]])
  rescue StandardError => e2
    say "Failed to create task: #{e2.message}".red
  end
end

#extract_task_details_with_regex(content, task_description) ⇒ Object

Extract task details using regex as a fallback



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ruby_todo/ai_assistant/task_creator.rb', line 141

def extract_task_details_with_regex(content, task_description)
  title_match = content.match(/title["\s:]+([^"]+)["]/i)
  desc_match = content.match(/description["\s:]+([^"]+)["]/i)
  priority_match = content.match(/priority["\s:]+([^"]+)["]/i)
  tags_match = content.match(/tags["\s:]+([^"]+)["]/i)

  {
    "title" => title_match ? title_match[1] : "Task from #{task_description}",
    "description" => desc_match ? desc_match[1] : task_description,
    "priority" => priority_match ? normalize_priority(priority_match[1]) : "medium",
    "tags" => tags_match ? tags_match[1] : ""
  }
end

#normalize_priority(details) ⇒ Object

Normalize priority to ensure only valid values are used



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ruby_todo/ai_assistant/task_creator.rb', line 156

def normalize_priority(priority)
  priority = priority.downcase.strip
  return priority if %w[high medium low].include?(priority)

  # Map similar terms to valid priorities
  case priority
  when /^h/i, "important", "urgent", "critical"
    "high"
  when /^l/i, "minor", "trivial"
    "low"
  else
    "medium" # Default fallback
  end
end

#parse_task_details_response(content, task_description) ⇒ Object

Parse the OpenAI response for task details



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ruby_todo/ai_assistant/task_creator.rb', line 127

def parse_task_details_response(content, task_description)
  # Try to extract JSON from the response
  json_match = content.match(/```json\n(.+?)\n```/m) || content.match(/\{.+\}/m)
  if json_match
    json_str = json_match[0].gsub(/```json\n|```/, "")
    JSON.parse(json_str)
  else
    extract_task_details_with_regex(content, task_description)
  end
rescue JSON::ParserError
  extract_task_details_with_regex(content, task_description)
end