Module: RubyTodo::OpenAIAdvancedPromptBuilder

Included in:
OpenAIPromptBuilder
Defined in:
lib/ruby_todo/ai_assistant/prompt_builder.rb

Overview

Module for building advanced prompts for OpenAI

Instance Method Summary collapse

Instance Method Details

#build_enriched_context(context) ⇒ Object

Build a more detailed context with task information



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruby_todo/ai_assistant/prompt_builder.rb', line 48

def build_enriched_context(context)
  rich_context = "Current context:\n"

  if context && context[:notebooks] && !context[:notebooks].empty?
    rich_context += "Available notebooks:\n"
    context[:notebooks].each do |notebook|
      rich_context += "- #{notebook[:name]}#{notebook[:is_default] ? " (default)" : ""}\n"
    end
  else
    rich_context += "No notebooks available.\n"
  end

  if context && context[:tasks] && !context[:tasks].empty?
    rich_context += "\nRecent tasks:\n"
    context[:tasks].each do |task|
      task[:notebook] || "unknown"
      status = task[:status] || "todo"
      priority = task[:priority] ? " [priority: #{task[:priority]}]" : ""
      tags = task[:tags] ? " [tags: #{task[:tags]}]" : ""

      rich_context += "- Task #{task[:id]}: #{task[:title]} (#{status})#{priority}#{tags}\n"
    end
  else
    rich_context += "\nNo tasks available.\n"
  end

  rich_context
end

#build_task_creation_prompt(context) ⇒ Object

Build system prompt for specialized task types



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ruby_todo/ai_assistant/prompt_builder.rb', line 78

def build_task_creation_prompt(context)
  "    You are an AI assistant for the Ruby Todo CLI application.\n    Your role is to help users manage their tasks and notebooks using natural language.\n\n    Available commands:\n    - task:add [notebook] [title] --description [description] --tags [comma,separated,tags] - Create a new task\n    - task:move [notebook] [task_id] [status] - Move a task to a new status (todo/in_progress/done/archived)\n\n    IMPORTANT: When creating tasks, the exact format must be:\n    task:add \"notebook_name\" \"task_title\" --description \"description\" --priority level --tags \"tags\"\n\n    Current context:\n    \#{context.to_json}\n  PROMPT\nend\n"

#extract_task_info_from_prompt(prompt) ⇒ Object

Extract task-related information from the prompt



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby_todo/ai_assistant/prompt_builder.rb', line 7

def extract_task_info_from_prompt(prompt)
  info = {
    notebook: nil,
    title: nil,
    priority: nil,
    tags: nil
  }

  # Extract notebook name
  if prompt =~ /(?:in|to)\s+(?:(?:the|my)\s+)?(?:notebook\s+)?["']?([^"'\s]+(?:\s+[^"'\s]+)*)["']?/i
    info[:notebook] = Regexp.last_match(1)
  end

  # Extract task title
  task_title_regex = /
    (?:titled|called|named)\s+["']([^"']+)["']|
    (?:add|create)\s+(?:a\s+)?(?:task|to-?do)\s+(?:to|for|about)\s+["']?([^"']+?)["']?(?:\s+to|\s+in|$)
  /xi

  if prompt =~ task_title_regex
    info[:title] = Regexp.last_match(1) || Regexp.last_match(2)
  end

  # Extract priority
  if prompt =~ /priority\s+(high|medium|low)/i
    info[:priority] = Regexp.last_match(1)
  end

  # Extract tags
  if prompt =~ /tags?\s+["']?([^"']+)["']?/i
    info[:tags] = Regexp.last_match(1)
  end

  # Set defaults for missing information
  info[:notebook] ||= "test_notebook" # Default notebook for tests
  info[:title] ||= "Task from prompt" # Default title

  info
end