todo.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. """Planning and task management middleware for agents."""
  2. # ruff: noqa: E501
  3. from __future__ import annotations
  4. from typing import TYPE_CHECKING, Annotated, Literal, cast
  5. if TYPE_CHECKING:
  6. from collections.abc import Awaitable, Callable
  7. from langchain_core.messages import SystemMessage, ToolMessage
  8. from langchain_core.tools import tool
  9. from langgraph.types import Command
  10. from typing_extensions import NotRequired, TypedDict
  11. from langchain.agents.middleware.types import (
  12. AgentMiddleware,
  13. AgentState,
  14. ModelCallResult,
  15. ModelRequest,
  16. ModelResponse,
  17. OmitFromInput,
  18. )
  19. from langchain.tools import InjectedToolCallId
  20. class Todo(TypedDict):
  21. """A single todo item with content and status."""
  22. content: str
  23. """The content/description of the todo item."""
  24. status: Literal["pending", "in_progress", "completed"]
  25. """The current status of the todo item."""
  26. class PlanningState(AgentState):
  27. """State schema for the todo middleware."""
  28. todos: Annotated[NotRequired[list[Todo]], OmitFromInput]
  29. """List of todo items for tracking task progress."""
  30. WRITE_TODOS_TOOL_DESCRIPTION = """Use this tool to create and manage a structured task list for your current work session. This helps you track progress, organize complex tasks, and demonstrate thoroughness to the user.
  31. Only use this tool if you think it will be helpful in staying organized. If the user's request is trivial and takes less than 3 steps, it is better to NOT use this tool and just do the task directly.
  32. ## When to Use This Tool
  33. Use this tool in these scenarios:
  34. 1. Complex multi-step tasks - When a task requires 3 or more distinct steps or actions
  35. 2. Non-trivial and complex tasks - Tasks that require careful planning or multiple operations
  36. 3. User explicitly requests todo list - When the user directly asks you to use the todo list
  37. 4. User provides multiple tasks - When users provide a list of things to be done (numbered or comma-separated)
  38. 5. The plan may need future revisions or updates based on results from the first few steps
  39. ## How to Use This Tool
  40. 1. When you start working on a task - Mark it as in_progress BEFORE beginning work.
  41. 2. After completing a task - Mark it as completed and add any new follow-up tasks discovered during implementation.
  42. 3. You can also update future tasks, such as deleting them if they are no longer necessary, or adding new tasks that are necessary. Don't change previously completed tasks.
  43. 4. You can make several updates to the todo list at once. For example, when you complete a task, you can mark the next task you need to start as in_progress.
  44. ## When NOT to Use This Tool
  45. It is important to skip using this tool when:
  46. 1. There is only a single, straightforward task
  47. 2. The task is trivial and tracking it provides no benefit
  48. 3. The task can be completed in less than 3 trivial steps
  49. 4. The task is purely conversational or informational
  50. ## Task States and Management
  51. 1. **Task States**: Use these states to track progress:
  52. - pending: Task not yet started
  53. - in_progress: Currently working on (you can have multiple tasks in_progress at a time if they are not related to each other and can be run in parallel)
  54. - completed: Task finished successfully
  55. 2. **Task Management**:
  56. - Update task status in real-time as you work
  57. - Mark tasks complete IMMEDIATELY after finishing (don't batch completions)
  58. - Complete current tasks before starting new ones
  59. - Remove tasks that are no longer relevant from the list entirely
  60. - IMPORTANT: When you write this todo list, you should mark your first task (or tasks) as in_progress immediately!.
  61. - IMPORTANT: Unless all tasks are completed, you should always have at least one task in_progress to show the user that you are working on something.
  62. 3. **Task Completion Requirements**:
  63. - ONLY mark a task as completed when you have FULLY accomplished it
  64. - If you encounter errors, blockers, or cannot finish, keep the task as in_progress
  65. - When blocked, create a new task describing what needs to be resolved
  66. - Never mark a task as completed if:
  67. - There are unresolved issues or errors
  68. - Work is partial or incomplete
  69. - You encountered blockers that prevent completion
  70. - You couldn't find necessary resources or dependencies
  71. - Quality standards haven't been met
  72. 4. **Task Breakdown**:
  73. - Create specific, actionable items
  74. - Break complex tasks into smaller, manageable steps
  75. - Use clear, descriptive task names
  76. Being proactive with task management demonstrates attentiveness and ensures you complete all requirements successfully
  77. Remember: If you only need to make a few tool calls to complete a task, and it is clear what you need to do, it is better to just do the task directly and NOT call this tool at all."""
  78. WRITE_TODOS_SYSTEM_PROMPT = """## `write_todos`
  79. You have access to the `write_todos` tool to help you manage and plan complex objectives.
  80. Use this tool for complex objectives to ensure that you are tracking each necessary step and giving the user visibility into your progress.
  81. This tool is very helpful for planning complex objectives, and for breaking down these larger complex objectives into smaller steps.
  82. It is critical that you mark todos as completed as soon as you are done with a step. Do not batch up multiple steps before marking them as completed.
  83. For simple objectives that only require a few steps, it is better to just complete the objective directly and NOT use this tool.
  84. Writing todos takes time and tokens, use it when it is helpful for managing complex many-step problems! But not for simple few-step requests.
  85. ## Important To-Do List Usage Notes to Remember
  86. - The `write_todos` tool should never be called multiple times in parallel.
  87. - Don't be afraid to revise the To-Do list as you go. New information may reveal new tasks that need to be done, or old tasks that are irrelevant."""
  88. @tool(description=WRITE_TODOS_TOOL_DESCRIPTION)
  89. def write_todos(todos: list[Todo], tool_call_id: Annotated[str, InjectedToolCallId]) -> Command:
  90. """Create and manage a structured task list for your current work session."""
  91. return Command(
  92. update={
  93. "todos": todos,
  94. "messages": [ToolMessage(f"Updated todo list to {todos}", tool_call_id=tool_call_id)],
  95. }
  96. )
  97. class TodoListMiddleware(AgentMiddleware):
  98. """Middleware that provides todo list management capabilities to agents.
  99. This middleware adds a `write_todos` tool that allows agents to create and manage
  100. structured task lists for complex multi-step operations. It's designed to help
  101. agents track progress, organize complex tasks, and provide users with visibility
  102. into task completion status.
  103. The middleware automatically injects system prompts that guide the agent on when
  104. and how to use the todo functionality effectively.
  105. Example:
  106. ```python
  107. from langchain.agents.middleware.todo import TodoListMiddleware
  108. from langchain.agents import create_agent
  109. agent = create_agent("openai:gpt-4o", middleware=[TodoListMiddleware()])
  110. # Agent now has access to write_todos tool and todo state tracking
  111. result = await agent.invoke({"messages": [HumanMessage("Help me refactor my codebase")]})
  112. print(result["todos"]) # Array of todo items with status tracking
  113. ```
  114. """
  115. state_schema = PlanningState
  116. def __init__(
  117. self,
  118. *,
  119. system_prompt: str = WRITE_TODOS_SYSTEM_PROMPT,
  120. tool_description: str = WRITE_TODOS_TOOL_DESCRIPTION,
  121. ) -> None:
  122. """Initialize the `TodoListMiddleware` with optional custom prompts.
  123. Args:
  124. system_prompt: Custom system prompt to guide the agent on using the todo
  125. tool.
  126. tool_description: Custom description for the `write_todos` tool.
  127. """
  128. super().__init__()
  129. self.system_prompt = system_prompt
  130. self.tool_description = tool_description
  131. # Dynamically create the write_todos tool with the custom description
  132. @tool(description=self.tool_description)
  133. def write_todos(
  134. todos: list[Todo], tool_call_id: Annotated[str, InjectedToolCallId]
  135. ) -> Command:
  136. """Create and manage a structured task list for your current work session."""
  137. return Command(
  138. update={
  139. "todos": todos,
  140. "messages": [
  141. ToolMessage(f"Updated todo list to {todos}", tool_call_id=tool_call_id)
  142. ],
  143. }
  144. )
  145. self.tools = [write_todos]
  146. def wrap_model_call(
  147. self,
  148. request: ModelRequest,
  149. handler: Callable[[ModelRequest], ModelResponse],
  150. ) -> ModelCallResult:
  151. """Update the system message to include the todo system prompt."""
  152. if request.system_message is not None:
  153. new_system_content = [
  154. *request.system_message.content_blocks,
  155. {"type": "text", "text": f"\n\n{self.system_prompt}"},
  156. ]
  157. else:
  158. new_system_content = [{"type": "text", "text": self.system_prompt}]
  159. new_system_message = SystemMessage(
  160. content=cast("list[str | dict[str, str]]", new_system_content)
  161. )
  162. return handler(request.override(system_message=new_system_message))
  163. async def awrap_model_call(
  164. self,
  165. request: ModelRequest,
  166. handler: Callable[[ModelRequest], Awaitable[ModelResponse]],
  167. ) -> ModelCallResult:
  168. """Update the system message to include the todo system prompt (async version)."""
  169. if request.system_message is not None:
  170. new_system_content = [
  171. *request.system_message.content_blocks,
  172. {"type": "text", "text": f"\n\n{self.system_prompt}"},
  173. ]
  174. else:
  175. new_system_content = [{"type": "text", "text": self.system_prompt}]
  176. new_system_message = SystemMessage(
  177. content=cast("list[str | dict[str, str]]", new_system_content)
  178. )
  179. return await handler(request.override(system_message=new_system_message))