Class: Clacky::Tools::TodoManager

Inherits:
Base
  • Object
show all
Defined in:
lib/clacky/tools/todo_manager.rb

Instance Method Summary collapse

Methods inherited from Base

#category, #description, #name, #parameters, #to_function_definition

Instance Method Details

#add_todos(task: nil, tasks: nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/clacky/tools/todo_manager.rb', line 102

def add_todos(task: nil, tasks: nil)
  # Determine which tasks to add
  tasks_to_add = []

  if tasks && tasks.is_a?(Array) && !tasks.empty?
    tasks_to_add = tasks.map(&:strip).reject(&:empty?)
  elsif task && !task.strip.empty?
    tasks_to_add = [task.strip]
  end

  return { error: "At least one task description is required" } if tasks_to_add.empty?

  existing_todos = load_todos
  next_id = existing_todos.empty? ? 1 : existing_todos.map { |t| t[:id] }.max + 1

  added_todos = []
  tasks_to_add.each_with_index do |task_desc, index|
    new_todo = {
      id: next_id + index,
      task: task_desc,
      status: "pending",
      created_at: Time.now.iso8601
    }
    existing_todos << new_todo
    added_todos << new_todo
  end

  save_todos(existing_todos)

  {
    message: added_todos.size == 1 ? "TODO added successfully" : "#{added_todos.size} TODOs added successfully",
    todos: added_todos,
    total: existing_todos.size,
    reminder: "⚠️ IMPORTANT: You have added TODO(s) but have NOT started working yet! You MUST now use other tools (write, edit, shell, etc.) to actually complete these tasks. DO NOT stop here!"
  }
end

#clear_todosObject



218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/clacky/tools/todo_manager.rb', line 218

def clear_todos
  todos = load_todos
  count = todos.size

  # Clear the in-memory storage
  save_todos([])

  {
    message: "All TODOs cleared",
    cleared_count: count
  }
end

#complete_todo(id) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
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
# File 'lib/clacky/tools/todo_manager.rb', line 159

def complete_todo(id)
  return { error: "Task ID is required" } if id.nil?

  todos = load_todos
  todo = todos.find { |t| t[:id] == id }

  return { error: "Task not found: #{id}" } unless todo

  if todo[:status] == "completed"
    return { message: "Task already completed", todo: todo }
  end

  todo[:status] = "completed"
  todo[:completed_at] = Time.now.iso8601
  save_todos(todos)

  # Find the next pending task
  next_pending = todos.find { |t| t[:status] == "pending" }
  
  # Count statistics
  completed_count = todos.count { |t| t[:status] == "completed" }
  total_count = todos.size

  result = {
    message: "Task marked as completed",
    todo: todo,
    progress: "#{completed_count}/#{total_count}",
    reminder: "⚠️ REMINDER: Check the PROJECT-SPECIFIC RULES section in your system prompt before continuing to the next task"
  }

  if next_pending
    result[:next_task] = next_pending
    result[:next_task_info] = "✅ Progress: #{completed_count}/#{total_count}. Next task: ##{next_pending[:id]} - #{next_pending[:task]}"
  else
    result[:all_completed] = true
    result[:completion_message] = "🎉 All tasks completed! (#{completed_count}/#{total_count})"
  end

  result
end

#execute(action:, task: nil, tasks: nil, id: nil, ids: nil, todos_storage: nil, working_dir: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/clacky/tools/todo_manager.rb', line 32

def execute(action:, task: nil, tasks: nil, id: nil, ids: nil, todos_storage: nil, working_dir: nil)
  # todos_storage is injected by Agent, stores todos in memory
  @todos = todos_storage || []

  case action
  when "add"
    add_todos(task: task, tasks: tasks)
  when "list"
    list_todos
  when "complete"
    complete_todo(id)
  when "remove"
    # Support both single ID and batch IDs
    if ids && ids.is_a?(Array)
      remove_todos(ids)
    else
      remove_todo(id)
    end
  when "clear"
    clear_todos
  else
    { error: "Unknown action: #{action}" }
  end
end

#format_call(args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/clacky/tools/todo_manager.rb', line 57

def format_call(args)
  action = args[:action] || args['action']
  case action
  when 'add'
    count = (args[:tasks] || args['tasks'])&.size || 1
    "TodoManager(add #{count} task#{count > 1 ? 's' : ''})"
  when 'complete'
    "TodoManager(complete ##{args[:id] || args['id']})"
  when 'list'
    "TodoManager(list)"
  when 'remove'
    ids = args[:ids] || args['ids']
    if ids && ids.is_a?(Array) && !ids.empty?
      "TodoManager(remove #{ids.size} tasks: #{ids.join(', ')})"
    else
      "TodoManager(remove ##{args[:id] || args['id']})"
    end
  when 'clear'
    "TodoManager(clear all)"
  else
    "TodoManager(#{action})"
  end
end

#format_result(result) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/clacky/tools/todo_manager.rb', line 81

def format_result(result)
  return result[:error] if result[:error]

  if result[:message]
    result[:message]
  else
    "Done"
  end
end

#list_todosObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/clacky/tools/todo_manager.rb', line 139

def list_todos
  todos = load_todos

  if todos.empty?
    return {
      message: "No TODO items",
      todos: [],
      total: 0
    }
  end

  {
    message: "TODO list",
    todos: todos,
    total: todos.size,
    pending: todos.count { |t| t[:status] == "pending" },
    completed: todos.count { |t| t[:status] == "completed" }
  }
end

#load_todosObject



92
93
94
# File 'lib/clacky/tools/todo_manager.rb', line 92

def load_todos
  @todos
end

#remove_todo(id) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/clacky/tools/todo_manager.rb', line 200

def remove_todo(id)
  return { error: "Task ID is required" } if id.nil?

  todos = load_todos
  todo = todos.find { |t| t[:id] == id }

  return { error: "Task not found: #{id}" } unless todo

  todos.reject! { |t| t[:id] == id }
  save_todos(todos)

  {
    message: "Task removed",
    todo: todo,
    remaining: todos.size
  }
end

#remove_todos(ids) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/clacky/tools/todo_manager.rb', line 231

def remove_todos(ids)
  return { error: "Task IDs array is required" } if ids.nil? || ids.empty?

  todos = load_todos
  removed_todos = []
  not_found_ids = []

  ids.each do |id|
    todo = todos.find { |t| t[:id] == id }
    if todo
      removed_todos << todo
    else
      not_found_ids << id
    end
  end

  # Remove all found todos
  todos.reject! { |t| ids.include?(t[:id]) }
  save_todos(todos)

  result = {
    message: "#{removed_todos.size} task(s) removed",
    removed: removed_todos,
    remaining: todos.size
  }

  # Add warning about not found IDs
  result[:not_found] = not_found_ids unless not_found_ids.empty?

  result
end

#save_todos(todos) ⇒ Object



96
97
98
99
100
# File 'lib/clacky/tools/todo_manager.rb', line 96

def save_todos(todos)
  # Modify the array in-place so Agent's @todos is updated
  # Important: Don't use @todos.clear first because todos might be @todos itself!
  @todos.replace(todos)
end