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(tasks_input) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/clacky/tools/todo_manager.rb', line 137

def add_todos(tasks_input)
  # tasks_input is already a normalized array (possibly empty)
  tasks_to_add = Array(tasks_input)
                 .map { |t| t.is_a?(String) ? t.strip : t.to_s.strip }
                 .reject(&:empty?)

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

  existing_todos = load_todos

  # Auto-clear old completed todos from previous task cycles before adding new ones
  completed_before = existing_todos.count { |t| t[:status] == "completed" }
  if completed_before > 0
    existing_todos.reject! { |t| t[:status] == "completed" }
  end

  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



258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/clacky/tools/todo_manager.rb', line 258

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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/clacky/tools/todo_manager.rb', line 197

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
    # All tasks completed — auto-clear so the agent doesn't need to call clear manually
    save_todos([])
    result[:all_completed] = true
    result[:completion_message] = "All tasks completed and cleared! (#{completed_count}/#{total_count})"
  end

  result
end

#complete_todos(ids) ⇒ Object

Mark several tasks completed in one call. Behavior mirrors ‘complete_todo` but aggregates over `ids`. Tolerates already-completed and not-found ids (returned in result).



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/clacky/tools/todo_manager.rb', line 306

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

  todos = load_todos
  now = Time.now.iso8601
  completed_now = []
  already_completed = []
  not_found = []

  ids.each do |id|
    todo = todos.find { |t| t[:id] == id }
    if todo.nil?
      not_found << id
    elsif todo[:status] == "completed"
      already_completed << todo
    else
      todo[:status] = "completed"
      todo[:completed_at] = now
      completed_now << todo
    end
  end

  save_todos(todos)

  completed_count = todos.count { |t| t[:status] == "completed" }
  total_count    = todos.size
  next_pending   = todos.find { |t| t[:status] == "pending" }

  result = {
    message: "#{completed_now.size} task(s) marked as completed",
    completed: completed_now,
    progress: "#{completed_count}/#{total_count}"
  }

  result[:already_completed] = already_completed unless already_completed.empty?
  result[:not_found]         = not_found          unless not_found.empty?

  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
    # All tasks completed — auto-clear to match single-complete behavior
    save_todos([])
    result[:all_completed] = true
    result[:completion_message] =
      "All tasks completed and cleared! (#{completed_count}/#{total_count})"
  end

  result
end

#execute(action:, task: nil, id: nil, todos_storage: nil, working_dir: nil, **_extra) ⇒ Object



45
46
47
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
76
# File 'lib/clacky/tools/todo_manager.rb', line 45

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

  # Normalize polymorphic inputs: callers may pass scalar or array for
  # `task` and `id`. We coerce both into arrays internally.
  tasks_input = normalize_to_array(task)
  ids_input   = normalize_to_array(id)

  case action
  when "add"
    add_todos(tasks_input)
  when "list"
    list_todos
  when "complete"
    if ids_input.size > 1
      complete_todos(ids_input)
    else
      complete_todo(ids_input.first)
    end
  when "remove"
    if ids_input.size > 1
      remove_todos(ids_input)
    else
      remove_todo(ids_input.first)
    end
  when "clear"
    clear_todos
  else
    { error: "Unknown action: #{action}" }
  end
end

#format_call(args) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/clacky/tools/todo_manager.rb', line 84

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

#format_result(result) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/clacky/tools/todo_manager.rb', line 116

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

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

#list_todosObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/clacky/tools/todo_manager.rb', line 177

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



127
128
129
# File 'lib/clacky/tools/todo_manager.rb', line 127

def load_todos
  @todos
end

#remove_todo(id) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/clacky/tools/todo_manager.rb', line 240

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



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/clacky/tools/todo_manager.rb', line 271

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



131
132
133
134
135
# File 'lib/clacky/tools/todo_manager.rb', line 131

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