|
| 1 | +""" |
| 2 | +Todo Module |
| 3 | +=========== |
| 4 | +Provides tools for the agent to maintain a persistent, human-readable todo |
| 5 | +file (.agent/todo.md) in the user's working directory. |
| 6 | +
|
| 7 | +The todo list lets the agent plan complex tasks and track progress, while |
| 8 | +the scratchpad is used for free-form notes and discoveries. |
| 9 | +""" |
| 10 | + |
| 11 | +import os |
| 12 | + |
| 13 | +AGENT_DIR = ".agent" |
| 14 | +TODO_FILE = os.path.join(AGENT_DIR, "todo.md") |
| 15 | + |
| 16 | +_HEADER_TEMPLATE = """\ |
| 17 | +<!-- This file is managed by the Lambda coding agent. --> |
| 18 | +<!-- Feel free to read it, but edits may be overwritten by the agent. --> |
| 19 | +
|
| 20 | +# Lambda Task List |
| 21 | +
|
| 22 | +## To Do |
| 23 | +""" |
| 24 | + |
| 25 | + |
| 26 | +def _ensure_todo() -> str: |
| 27 | + """Return the absolute path to the todo list, creating it if it doesn't exist.""" |
| 28 | + agent_dir = os.path.abspath(AGENT_DIR) |
| 29 | + os.makedirs(agent_dir, exist_ok=True) |
| 30 | + path = os.path.abspath(TODO_FILE) |
| 31 | + if not os.path.exists(path): |
| 32 | + with open(path, "w", encoding="utf-8") as f: |
| 33 | + f.write(_HEADER_TEMPLATE) |
| 34 | + return path |
| 35 | + |
| 36 | + |
| 37 | +def read_todo() -> str: |
| 38 | + """Reads the full contents of the Lambda todo file (.agent/todo.md). |
| 39 | +
|
| 40 | + Use this to recall your current task list and implementation plan. |
| 41 | + """ |
| 42 | + path = _ensure_todo() |
| 43 | + try: |
| 44 | + with open(path, "r", encoding="utf-8") as f: |
| 45 | + return f.read() |
| 46 | + except Exception as e: |
| 47 | + return f"Error reading todo list: {e}" |
| 48 | + |
| 49 | + |
| 50 | +def write_todo(content: str) -> str: |
| 51 | + """Overwrites the entire Lambda todo file with the provided content. |
| 52 | +
|
| 53 | + Use this when you need to replace the todo list with a fresh task list. |
| 54 | + For incremental updates, prefer update_todo. |
| 55 | +
|
| 56 | + Args: |
| 57 | + content: The full markdown content to write to the todo list. |
| 58 | + """ |
| 59 | + path = _ensure_todo() |
| 60 | + try: |
| 61 | + with open(path, "w", encoding="utf-8") as f: |
| 62 | + f.write(_HEADER_TEMPLATE + content) |
| 63 | + return f"Todo list written successfully → {path}" |
| 64 | + except Exception as e: |
| 65 | + return f"Error writing todo list: {e}" |
| 66 | + |
| 67 | + |
| 68 | +def update_todo(note: str, section: str = "To Do") -> str: |
| 69 | + """Appends an item to a specific section in the todo list. |
| 70 | +
|
| 71 | + This is ideal for checking off steps or adding new sub-tasks. |
| 72 | +
|
| 73 | + Args: |
| 74 | + note: The text to append (supports markdown, e.g. '- [ ] Task'). |
| 75 | + section: The section heading to append under (e.g. 'To Do', 'In Progress', 'Done'). |
| 76 | + """ |
| 77 | + path = _ensure_todo() |
| 78 | + try: |
| 79 | + with open(path, "r", encoding="utf-8") as f: |
| 80 | + existing = f.read() |
| 81 | + |
| 82 | + entry = f"\n{note}" |
| 83 | + |
| 84 | + section_heading = f"## {section}" |
| 85 | + if section_heading in existing: |
| 86 | + # Append under the existing section |
| 87 | + parts = existing.split(section_heading, 1) |
| 88 | + # Find the next section heading (##) or end of file |
| 89 | + rest = parts[1] |
| 90 | + next_section = rest.find("\n## ") |
| 91 | + if next_section == -1: |
| 92 | + # No next section — just append at the end |
| 93 | + updated = existing + entry |
| 94 | + else: |
| 95 | + # Insert before the next section |
| 96 | + insert_pos = len(parts[0]) + len(section_heading) + next_section |
| 97 | + updated = existing[:insert_pos] + entry + "\n" + existing[insert_pos:] |
| 98 | + else: |
| 99 | + # Create the section at the end |
| 100 | + updated = existing.rstrip() + f"\n\n{section_heading}\n{entry}\n" |
| 101 | + |
| 102 | + with open(path, "w", encoding="utf-8") as f: |
| 103 | + f.write(updated) |
| 104 | + |
| 105 | + return f"Todo list updated (section: {section}) → {path}" |
| 106 | + except Exception as e: |
| 107 | + return f"Error updating todo list: {e}" |
| 108 | + |
| 109 | + |
| 110 | +def clear_todo() -> str: |
| 111 | + """Clears the todo list, resetting it to a blank state. |
| 112 | +
|
| 113 | + Use this when a major task is fully complete and the task list is no longer needed. |
| 114 | + """ |
| 115 | + path = _ensure_todo() |
| 116 | + try: |
| 117 | + with open(path, "w", encoding="utf-8") as f: |
| 118 | + f.write(_HEADER_TEMPLATE) |
| 119 | + return f"Todo list cleared → {path}" |
| 120 | + except Exception as e: |
| 121 | + return f"Error clearing todo list: {e}" |
| 122 | + |
| 123 | + |
| 124 | +# Tool registrations for the agent |
| 125 | +TODO_EXECUTORS = { |
| 126 | + "read_todo": read_todo, |
| 127 | + "write_todo": write_todo, |
| 128 | + "update_todo": update_todo, |
| 129 | + "clear_todo": clear_todo, |
| 130 | +} |
| 131 | + |
| 132 | +TODO_FUNCTIONS = [ |
| 133 | + read_todo, |
| 134 | + write_todo, |
| 135 | + update_todo, |
| 136 | + clear_todo, |
| 137 | +] |
0 commit comments