lm-deluge 0.0.75__tar.gz → 0.0.76__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. {lm_deluge-0.0.75/src/lm_deluge.egg-info → lm_deluge-0.0.76}/PKG-INFO +1 -1
  2. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/pyproject.toml +1 -1
  3. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/client.py +101 -20
  4. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/llm_tools/__init__.py +8 -1
  5. lm_deluge-0.0.76/src/lm_deluge/llm_tools/filesystem.py +0 -0
  6. lm_deluge-0.0.76/src/lm_deluge/llm_tools/subagents.py +233 -0
  7. lm_deluge-0.0.76/src/lm_deluge/llm_tools/todos.py +342 -0
  8. {lm_deluge-0.0.75 → lm_deluge-0.0.76/src/lm_deluge.egg-info}/PKG-INFO +1 -1
  9. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge.egg-info/SOURCES.txt +3 -0
  10. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/LICENSE +0 -0
  11. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/README.md +0 -0
  12. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/setup.cfg +0 -0
  13. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/__init__.py +0 -0
  14. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/api_requests/__init__.py +0 -0
  15. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/api_requests/anthropic.py +0 -0
  16. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/api_requests/base.py +0 -0
  17. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/api_requests/bedrock.py +0 -0
  18. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/api_requests/chat_reasoning.py +0 -0
  19. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/api_requests/common.py +0 -0
  20. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/api_requests/deprecated/bedrock.py +0 -0
  21. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/api_requests/deprecated/cohere.py +0 -0
  22. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/api_requests/deprecated/deepseek.py +0 -0
  23. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/api_requests/deprecated/mistral.py +0 -0
  24. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/api_requests/deprecated/vertex.py +0 -0
  25. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/api_requests/gemini.py +0 -0
  26. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/api_requests/mistral.py +0 -0
  27. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/api_requests/openai.py +0 -0
  28. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/api_requests/response.py +0 -0
  29. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/batches.py +0 -0
  30. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/built_in_tools/anthropic/__init__.py +0 -0
  31. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/built_in_tools/anthropic/bash.py +0 -0
  32. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/built_in_tools/anthropic/computer_use.py +0 -0
  33. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/built_in_tools/anthropic/editor.py +0 -0
  34. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/built_in_tools/base.py +0 -0
  35. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/built_in_tools/openai.py +0 -0
  36. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/cache.py +0 -0
  37. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/cli.py +0 -0
  38. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/config.py +0 -0
  39. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/embed.py +0 -0
  40. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/errors.py +0 -0
  41. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/file.py +0 -0
  42. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/image.py +0 -0
  43. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/llm_tools/classify.py +0 -0
  44. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/llm_tools/extract.py +0 -0
  45. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/llm_tools/locate.py +0 -0
  46. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/llm_tools/ocr.py +0 -0
  47. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/llm_tools/score.py +0 -0
  48. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/llm_tools/translate.py +0 -0
  49. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/mock_openai.py +0 -0
  50. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/__init__.py +0 -0
  51. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/anthropic.py +0 -0
  52. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/bedrock.py +0 -0
  53. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/cerebras.py +0 -0
  54. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/cohere.py +0 -0
  55. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/deepseek.py +0 -0
  56. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/fireworks.py +0 -0
  57. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/google.py +0 -0
  58. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/grok.py +0 -0
  59. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/groq.py +0 -0
  60. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/kimi.py +0 -0
  61. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/meta.py +0 -0
  62. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/minimax.py +0 -0
  63. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/mistral.py +0 -0
  64. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/openai.py +0 -0
  65. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/openrouter.py +0 -0
  66. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/models/together.py +0 -0
  67. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/presets/cerebras.py +0 -0
  68. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/presets/meta.py +0 -0
  69. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/prompt.py +0 -0
  70. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/request_context.py +0 -0
  71. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/rerank.py +0 -0
  72. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/tool.py +0 -0
  73. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/tracker.py +0 -0
  74. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/usage.py +0 -0
  75. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/util/harmony.py +0 -0
  76. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/util/json.py +0 -0
  77. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/util/logprobs.py +0 -0
  78. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/util/schema.py +0 -0
  79. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/util/spatial.py +0 -0
  80. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/util/validation.py +0 -0
  81. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/util/xml.py +0 -0
  82. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge/warnings.py +0 -0
  83. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge.egg-info/dependency_links.txt +0 -0
  84. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge.egg-info/requires.txt +0 -0
  85. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/src/lm_deluge.egg-info/top_level.txt +0 -0
  86. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/tests/test_builtin_tools.py +0 -0
  87. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/tests/test_file_upload.py +0 -0
  88. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/tests/test_mock_openai.py +0 -0
  89. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/tests/test_native_mcp_server.py +0 -0
  90. {lm_deluge-0.0.75 → lm_deluge-0.0.76}/tests/test_openrouter_generic.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lm_deluge
3
- Version: 0.0.75
3
+ Version: 0.0.76
4
4
  Summary: Python utility for using LLM API models.
5
5
  Author-email: Benjamin Anderson <ben@trytaylor.ai>
6
6
  Requires-Python: >=3.10
@@ -3,7 +3,7 @@ requires = ["setuptools", "wheel"]
3
3
 
4
4
  [project]
5
5
  name = "lm_deluge"
6
- version = "0.0.75"
6
+ version = "0.0.76"
7
7
  authors = [{ name = "Benjamin Anderson", email = "ben@trytaylor.ai" }]
8
8
  description = "Python utility for using LLM API models."
9
9
  readme = "README.md"
@@ -1,4 +1,5 @@
1
1
  import asyncio
2
+ from dataclasses import dataclass
2
3
  from typing import (
3
4
  Any,
4
5
  AsyncGenerator,
@@ -37,6 +38,14 @@ from .request_context import RequestContext
37
38
  from .tracker import StatusTracker
38
39
 
39
40
 
41
+ @dataclass
42
+ class AgentLoopResponse:
43
+ """Wrapper for agent loop results to distinguish from single request results."""
44
+
45
+ conversation: Conversation
46
+ final_response: APIResponse
47
+
48
+
40
49
  # TODO: add optional max_input_tokens to client so we can reject long prompts to prevent abuse
41
50
  class _LLMClient(BaseModel):
42
51
  """
@@ -88,7 +97,9 @@ class _LLMClient(BaseModel):
88
97
  # Internal state for async task handling
89
98
  _next_task_id: int = PrivateAttr(default=0)
90
99
  _tasks: dict[int, asyncio.Task] = PrivateAttr(default_factory=dict)
91
- _results: dict[int, APIResponse] = PrivateAttr(default_factory=dict)
100
+ _results: dict[int, APIResponse | AgentLoopResponse] = PrivateAttr(
101
+ default_factory=dict
102
+ )
92
103
  _tracker: StatusTracker | None = PrivateAttr(default=None)
93
104
  _capacity_lock: asyncio.Lock = PrivateAttr(default_factory=asyncio.Lock)
94
105
 
@@ -747,11 +758,11 @@ class _LLMClient(BaseModel):
747
758
  async def wait_for(self, task_id: int) -> APIResponse:
748
759
  task = self._tasks.get(task_id)
749
760
  if task:
750
- return await task
751
- res = self._results.get(task_id)
752
- if res:
753
- return res
761
+ result = await task
754
762
  else:
763
+ result = self._results.get(task_id)
764
+
765
+ if result is None:
755
766
  return APIResponse(
756
767
  id=-1,
757
768
  model_internal="",
@@ -762,6 +773,11 @@ class _LLMClient(BaseModel):
762
773
  error_message="Task not found",
763
774
  )
764
775
 
776
+ assert isinstance(
777
+ result, APIResponse
778
+ ), f"Expected APIResponse, got {type(result)}. Use wait_for_agent_loop for agent loop tasks."
779
+ return result
780
+
765
781
  async def wait_for_all(
766
782
  self, task_ids: Sequence[int] | None = None
767
783
  ) -> list[APIResponse]:
@@ -797,6 +813,9 @@ class _LLMClient(BaseModel):
797
813
  tid = tasks_map.pop(task)
798
814
  task_result = self._results.get(tid, await task)
799
815
  assert task_result
816
+ assert isinstance(
817
+ task_result, APIResponse
818
+ ), f"Expected APIResponse, got {type(task_result)}. as_completed() only works with single requests, not agent loops."
800
819
  yield tid, task_result
801
820
 
802
821
  while tasks_map:
@@ -807,6 +826,9 @@ class _LLMClient(BaseModel):
807
826
  tid = tasks_map.pop(task)
808
827
  task_result = self._results.get(tid, await task)
809
828
  assert task_result
829
+ assert isinstance(
830
+ task_result, APIResponse
831
+ ), f"Expected APIResponse, got {type(task_result)}. as_completed() only works with single requests, not agent loops."
810
832
  yield tid, task_result
811
833
 
812
834
  async def stream(
@@ -828,24 +850,15 @@ class _LLMClient(BaseModel):
828
850
  return self.postprocess(item)
829
851
  return item
830
852
 
831
- async def run_agent_loop(
853
+ async def _run_agent_loop_internal(
832
854
  self,
833
- conversation: Prompt,
855
+ task_id: int,
856
+ conversation: Conversation,
834
857
  *,
835
858
  tools: list[Tool | dict | MCPServer] | None = None,
836
859
  max_rounds: int = 5,
837
- show_progress: bool = False,
838
- ) -> tuple[Conversation, APIResponse]:
839
- """Run a simple agent loop until no more tool calls are returned.
840
-
841
- The provided ``conversation`` will be mutated and returned alongside the
842
- final ``APIResponse`` from the model. ``tools`` may include ``Tool``
843
- instances or built‑in tool dictionaries.
844
- """
845
-
846
- if not isinstance(conversation, Conversation):
847
- conversation = prompts_to_conversations([conversation])[0]
848
- assert isinstance(conversation, Conversation)
860
+ ) -> AgentLoopResponse:
861
+ """Internal method to run agent loop and return wrapped result."""
849
862
 
850
863
  # Expand MCPServer objects to their constituent tools for tool execution
851
864
  expanded_tools: list[Tool] = []
@@ -898,7 +911,75 @@ class _LLMClient(BaseModel):
898
911
  if response is None:
899
912
  raise RuntimeError("model did not return a response")
900
913
 
901
- return conversation, response
914
+ result = AgentLoopResponse(conversation=conversation, final_response=response)
915
+ self._results[task_id] = result
916
+ return result
917
+
918
+ def start_agent_loop_nowait(
919
+ self,
920
+ conversation: Prompt,
921
+ *,
922
+ tools: list[Tool | dict | MCPServer] | None = None,
923
+ max_rounds: int = 5,
924
+ ) -> int:
925
+ """Start an agent loop without waiting for it to complete.
926
+
927
+ Returns a task_id that can be used with wait_for_agent_loop().
928
+ """
929
+ if not isinstance(conversation, Conversation):
930
+ conversation = prompts_to_conversations([conversation])[0]
931
+ assert isinstance(conversation, Conversation)
932
+
933
+ task_id = self._next_task_id
934
+ self._next_task_id += 1
935
+
936
+ task = asyncio.create_task(
937
+ self._run_agent_loop_internal(
938
+ task_id, conversation, tools=tools, max_rounds=max_rounds
939
+ )
940
+ )
941
+ self._tasks[task_id] = task
942
+ return task_id
943
+
944
+ async def wait_for_agent_loop(
945
+ self, task_id: int
946
+ ) -> tuple[Conversation, APIResponse]:
947
+ """Wait for an agent loop task to complete.
948
+
949
+ Returns the conversation and final response from the agent loop.
950
+ """
951
+ task = self._tasks.get(task_id)
952
+ if task:
953
+ result = await task
954
+ else:
955
+ result = self._results.get(task_id)
956
+
957
+ if result is None:
958
+ raise RuntimeError(f"Agent loop task {task_id} not found")
959
+
960
+ assert isinstance(
961
+ result, AgentLoopResponse
962
+ ), f"Expected AgentLoopResponse, got {type(result)}"
963
+ return result.conversation, result.final_response
964
+
965
+ async def run_agent_loop(
966
+ self,
967
+ conversation: Prompt,
968
+ *,
969
+ tools: list[Tool | dict | MCPServer] | None = None,
970
+ max_rounds: int = 5,
971
+ show_progress: bool = False,
972
+ ) -> tuple[Conversation, APIResponse]:
973
+ """Run a simple agent loop until no more tool calls are returned.
974
+
975
+ The provided ``conversation`` will be mutated and returned alongside the
976
+ final ``APIResponse`` from the model. ``tools`` may include ``Tool``
977
+ instances or built‑in tool dictionaries.
978
+ """
979
+ task_id = self.start_agent_loop_nowait(
980
+ conversation, tools=tools, max_rounds=max_rounds
981
+ )
982
+ return await self.wait_for_agent_loop(task_id)
902
983
 
903
984
  def run_agent_loop_sync(
904
985
  self,
@@ -1,11 +1,18 @@
1
1
  from .extract import extract, extract_async
2
- from .translate import translate, translate_async
3
2
  from .score import score_llm
3
+ from .subagents import SubAgentManager
4
+ from .todos import TodoItem, TodoManager, TodoPriority, TodoStatus
5
+ from .translate import translate, translate_async
4
6
 
5
7
  __all__ = [
6
8
  "extract",
7
9
  "extract_async",
10
+ "TodoItem",
11
+ "TodoManager",
12
+ "TodoPriority",
13
+ "TodoStatus",
8
14
  "translate",
9
15
  "translate_async",
10
16
  "score_llm",
17
+ "SubAgentManager",
11
18
  ]
File without changes
@@ -0,0 +1,233 @@
1
+ from lm_deluge.api_requests.base import APIResponse
2
+ from lm_deluge.client import AgentLoopResponse, _LLMClient
3
+ from lm_deluge.prompt import Conversation, prompts_to_conversations
4
+ from lm_deluge.tool import Tool
5
+
6
+
7
+ class SubAgentManager:
8
+ """Manages subagent tasks that can be spawned by a main LLM via tool calls.
9
+
10
+ The SubAgentManager exposes tools that allow a main LLM to delegate subtasks
11
+ to specialized or cheaper subagent models, saving context and improving efficiency.
12
+
13
+ Example:
14
+ >>> manager = SubAgentManager(
15
+ ... client=LLMClient("gpt-4o-mini"), # Subagent model
16
+ ... tools=[search_tool, calculator_tool] # Tools available to subagents
17
+ ... )
18
+ >>> main_client = LLMClient("gpt-4o") # More expensive main model
19
+ >>> conv = Conversation.user("Research AI and calculate market size")
20
+ >>> # Main model can now call manager tools to spawn subagents
21
+ >>> conv, resp = await main_client.run_agent_loop(
22
+ ... conv,
23
+ ... tools=manager.get_tools()
24
+ ... )
25
+ """
26
+
27
+ def __init__(
28
+ self,
29
+ client: _LLMClient,
30
+ tools: list[Tool] | None = None,
31
+ max_rounds: int = 5,
32
+ ):
33
+ """Initialize the SubAgentManager.
34
+
35
+ Args:
36
+ client: LLMClient to use for subagent tasks
37
+ tools: Tools available to subagents (optional)
38
+ max_rounds: Maximum rounds for each subagent's agent loop
39
+ """
40
+ self.client = client
41
+ self.tools = tools or []
42
+ self.max_rounds = max_rounds
43
+ self.subagents: dict[int, dict] = {}
44
+
45
+ async def _start_subagent(self, task: str) -> int:
46
+ """Start a subagent with the given task.
47
+
48
+ Args:
49
+ task: The task description for the subagent
50
+
51
+ Returns:
52
+ Subagent task ID
53
+ """
54
+ conversation = prompts_to_conversations([task])[0]
55
+ assert isinstance(conversation, Conversation)
56
+
57
+ # Use agent loop nowait API to start the subagent
58
+ task_id = self.client.start_agent_loop_nowait(
59
+ conversation,
60
+ tools=self.tools, # type: ignore
61
+ max_rounds=self.max_rounds,
62
+ )
63
+
64
+ # Track the subagent
65
+ self.subagents[task_id] = {
66
+ "status": "running",
67
+ "conversation": None,
68
+ "response": None,
69
+ "error": None,
70
+ }
71
+
72
+ return task_id
73
+
74
+ def _finalize_subagent_result(
75
+ self, agent_id: int, result: AgentLoopResponse
76
+ ) -> str:
77
+ """Update subagent tracking state from a finished agent loop."""
78
+ agent = self.subagents[agent_id]
79
+ agent["conversation"] = result.conversation
80
+ agent["response"] = result.final_response
81
+
82
+ if result.final_response.is_error:
83
+ agent["status"] = "error"
84
+ agent["error"] = result.final_response.error_message
85
+ return f"Error: {agent['error']}"
86
+
87
+ agent["status"] = "finished"
88
+ return result.final_response.completion or "Subagent finished with no output"
89
+
90
+ async def _check_subagent(self, agent_id: int) -> str:
91
+ """Check the status of a subagent.
92
+
93
+ Args:
94
+ agent_id: The subagent task ID
95
+
96
+ Returns:
97
+ Status string describing the subagent's state
98
+ """
99
+ if agent_id not in self.subagents:
100
+ return f"Error: Subagent {agent_id} not found"
101
+
102
+ agent = self.subagents[agent_id]
103
+ status = agent["status"]
104
+
105
+ if status == "finished":
106
+ response: APIResponse = agent["response"]
107
+ return response.completion or "Subagent finished with no output"
108
+ elif status == "error":
109
+ return f"Error: {agent['error']}"
110
+ else:
111
+ # Try to check if it's done
112
+ try:
113
+ # Check if the task exists in client's results
114
+ stored_result = self.client._results.get(agent_id)
115
+ if isinstance(stored_result, AgentLoopResponse):
116
+ return self._finalize_subagent_result(agent_id, stored_result)
117
+
118
+ task = self.client._tasks.get(agent_id)
119
+ if task and task.done():
120
+ try:
121
+ task_result = task.result()
122
+ except Exception as e:
123
+ agent["status"] = "error"
124
+ agent["error"] = str(e)
125
+ return f"Error: {agent['error']}"
126
+
127
+ if isinstance(task_result, AgentLoopResponse):
128
+ return self._finalize_subagent_result(agent_id, task_result)
129
+
130
+ agent["status"] = "error"
131
+ agent["error"] = (
132
+ f"Unexpected task result type: {type(task_result).__name__}"
133
+ )
134
+ return f"Error: {agent['error']}"
135
+
136
+ # Still running
137
+ return f"Subagent {agent_id} is still running. Call this tool again to check status."
138
+ except Exception as e:
139
+ agent["status"] = "error"
140
+ agent["error"] = str(e)
141
+ return f"Error checking subagent: {e}"
142
+
143
+ async def _wait_for_subagent(self, agent_id: int) -> str:
144
+ """Wait for a subagent to complete and return its output.
145
+
146
+ Args:
147
+ agent_id: The subagent task ID
148
+
149
+ Returns:
150
+ The subagent's final output
151
+ """
152
+ if agent_id not in self.subagents:
153
+ return f"Error: Subagent {agent_id} not found"
154
+
155
+ try:
156
+ # Use the wait_for_agent_loop API
157
+ conversation, response = await self.client.wait_for_agent_loop(agent_id)
158
+
159
+ agent = self.subagents[agent_id]
160
+ agent["conversation"] = conversation
161
+ agent["response"] = response
162
+
163
+ if response.is_error:
164
+ agent["status"] = "error"
165
+ agent["error"] = response.error_message
166
+ return f"Error: {response.error_message}"
167
+ else:
168
+ agent["status"] = "finished"
169
+ return response.completion or "Subagent finished with no output"
170
+ except Exception as e:
171
+ agent = self.subagents[agent_id]
172
+ agent["status"] = "error"
173
+ agent["error"] = str(e)
174
+ return f"Error waiting for subagent: {e}"
175
+
176
+ def get_tools(self) -> list[Tool]:
177
+ """Get the tools that allow a main LLM to control subagents.
178
+
179
+ Returns:
180
+ List of Tool objects for starting, checking, and waiting for subagents
181
+ """
182
+ start_tool = Tool(
183
+ name="start_subagent",
184
+ description=(
185
+ "Start a subagent to work on a subtask independently. "
186
+ "Use this to delegate complex subtasks or when you need to save context. "
187
+ "Returns the subagent's task ID which can be used to check its status."
188
+ ),
189
+ run=self._start_subagent,
190
+ parameters={
191
+ "task": {
192
+ "type": "string",
193
+ "description": "The task description for the subagent to work on",
194
+ }
195
+ },
196
+ required=["task"],
197
+ )
198
+
199
+ check_tool = Tool(
200
+ name="check_subagent",
201
+ description=(
202
+ "Check the status and output of a running subagent. "
203
+ "If the subagent is still running, you'll be told to check again later. "
204
+ "If finished, returns the subagent's final output."
205
+ ),
206
+ run=self._check_subagent,
207
+ parameters={
208
+ "agent_id": {
209
+ "type": "integer",
210
+ "description": "The task ID of the subagent to check",
211
+ }
212
+ },
213
+ required=["agent_id"],
214
+ )
215
+
216
+ wait_tool = Tool(
217
+ name="wait_for_subagent",
218
+ description=(
219
+ "Wait for a subagent to complete and return its output. "
220
+ "This will block until the subagent finishes. "
221
+ "Use check_subagent if you want to do other work while waiting."
222
+ ),
223
+ run=self._wait_for_subagent,
224
+ parameters={
225
+ "agent_id": {
226
+ "type": "integer",
227
+ "description": "The task ID of the subagent to wait for",
228
+ }
229
+ },
230
+ required=["agent_id"],
231
+ )
232
+
233
+ return [start_tool, check_tool, wait_tool]
@@ -0,0 +1,342 @@
1
+ # Adapted from https://github.com/sst/opencode - MIT License
2
+ # MIT License
3
+ # Copyright (c) 2025 opencode
4
+
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+ from __future__ import annotations
23
+
24
+ import json
25
+ import uuid
26
+ from typing import Any, Literal, Sequence
27
+
28
+ from pydantic import BaseModel, Field, field_validator
29
+
30
+ from ..tool import Tool
31
+
32
+ TODO_WRITE_DESCRIPTION = """Use this tool to create and manage a structured task list for your current coding session. This helps you track progress, organize complex tasks, and demonstrate thoroughness to the user.
33
+ It also helps the user understand the progress of the task and overall progress of their requests.
34
+
35
+ ## When to Use This Tool
36
+ Use this tool proactively in these scenarios:
37
+
38
+ 1. Complex multi-step tasks - When a task requires 3 or more distinct steps or actions
39
+ 2. Non-trivial and complex tasks - Tasks that require careful planning or multiple operations
40
+ 3. User explicitly requests todo list - When the user directly asks you to use the todo list
41
+ 4. User provides multiple tasks - When users provide a list of things to be done (numbered or comma-separated)
42
+ 5. After receiving new instructions - Immediately capture user requirements as todos. Feel free to edit the todo list based on new information.
43
+ 6. After completing a task - Mark it complete and add any new follow-up tasks
44
+ 7. When you start working on a new task, mark the todo as in_progress. Ideally you should only have one todo as in_progress at a time. Complete existing tasks before starting new ones.
45
+
46
+ ## When NOT to Use This Tool
47
+
48
+ Skip using this tool when:
49
+ 1. There is only a single, straightforward task
50
+ 2. The task is trivial and tracking it provides no organizational benefit
51
+ 3. The task can be completed in less than 3 trivial steps
52
+ 4. The task is purely conversational or informational
53
+
54
+ NOTE that you should not use this tool if there is only one trivial task to do. In this case you are better off just doing the task directly.
55
+
56
+ ## Examples of When to Use the Todo List
57
+
58
+ <example>
59
+ User: I want to add a dark mode toggle to the application settings. Make sure you run the tests and build when you're done!
60
+ Assistant: I'll help add a dark mode toggle to your application settings. Let me create a todo list to track this implementation.
61
+ *Creates todo list with the following items:*
62
+ 1. Create dark mode toggle component in Settings page
63
+ 2. Add dark mode state management (context/store)
64
+ 3. Implement CSS-in-JS styles for dark theme
65
+ 4. Update existing components to support theme switching
66
+ 5. Run tests and build process, addressing any failures or errors that occur
67
+ *Begins working on the first task*
68
+
69
+ <reasoning>
70
+ The assistant used the todo list because:
71
+ 1. Adding dark mode is a multi-step feature requiring UI, state management, and styling changes
72
+ 2. The user explicitly requested tests and build be run afterward
73
+ 3. The assistant inferred that tests and build need to pass by adding "Ensure tests and build succeed" as the final task
74
+ </reasoning>
75
+ </example>
76
+
77
+ <example>
78
+ User: Help me rename the function getCwd to getCurrentWorkingDirectory across my project
79
+ Assistant: Let me first search through your codebase to find all occurrences of 'getCwd'.
80
+ *Uses grep or search tools to locate all instances of getCwd in the codebase*
81
+ Assistant: I've found 15 instances of 'getCwd' across 8 different files. Let me create a todo list to track these changes.
82
+ *Creates todo list with specific items for each file that needs updating*
83
+
84
+ <reasoning>
85
+ The assistant used the todo list because:
86
+ 1. First, the assistant searched to understand the scope of the task
87
+ 2. Upon finding multiple occurrences across different files, it determined this was a complex task with multiple steps
88
+ 3. The todo list helps ensure every instance is tracked and updated systematically
89
+ 4. This approach prevents missing any occurrences and maintains code consistency
90
+ </reasoning>
91
+ </example>
92
+
93
+
94
+ <example>
95
+ User: I need to implement these features for my e-commerce site: user registration, product catalog, shopping cart, and checkout flow.
96
+ Assistant: I'll help implement these features. First, let's add all the features to the todo list.
97
+ *Creates a todo list breaking down each feature into specific tasks based on the project architecture*
98
+ Assistant: Let's start with user registration. This will involve creating database models, API endpoints, and frontend forms that integrate with your existing authentication system.
99
+
100
+ <reasoning>
101
+ The assistant used the todo list because:
102
+ 1. The user provided multiple complex features to implement in a comma separated list
103
+ 2. The todo list helps organize these large features into manageable tasks
104
+ 3. This approach allows for tracking progress across the entire implementation
105
+ </reasoning>
106
+ </example>
107
+
108
+ <example>
109
+ User: Can you help optimize my React application? It's rendering slowly and has performance issues.</user>
110
+ Assistant: I'll help optimize your React application. First, let me examine your codebase to identify potential performance bottlenecks.
111
+ *Reviews component structure, render patterns, state management, and data fetching*
112
+ Assistant: After analyzing your codebase, I've identified several performance issues. Let me create a todo list to track our optimization efforts.
113
+ *Creates todo list with items like: 1) Implement memoization for expensive calculations in ProductList, 2) Add virtualization for long lists in Dashboard, 3) Optimize image loading in Gallery component, 4) Fix state update loops in ShoppingCart, 5) Review bundle size and implement code splitting*
114
+ Let's start by implementing memoization for the expensive calculations in your ProductList component.</assistant>
115
+
116
+ <reasoning>
117
+ The assistant used the todo list because:
118
+ 1. First, the assistant examined the codebase to identify specific performance issues
119
+ 2. Based on this analysis, it identified multiple optimization opportunities
120
+ 3. Performance optimization is a non-trivial task requiring multiple steps
121
+ 4. The todo list helps methodically track improvements across different components
122
+ 5. This systematic approach ensures all performance bottlenecks are addressed
123
+ </reasoning>
124
+ </example>
125
+
126
+ ## Examples of When NOT to Use the Todo List
127
+
128
+ <example>
129
+ User: How do I print 'Hello World' in Python?
130
+ Assistant: In Python, you can print "Hello World" with this simple code:
131
+
132
+ python
133
+ print("Hello World")
134
+
135
+ This will output the text "Hello World" to the console when executed.</assistant>
136
+
137
+ <reasoning>
138
+ The assistant did not use the todo list because this is a single, trivial task that can be completed in one step. There's no need to track multiple tasks or steps for such a straightforward request.
139
+ </reasoning>
140
+ </example>
141
+
142
+ <example>
143
+ User: What does the git status command do?
144
+ Assistant: The git status command shows the current state of your working directory and staging area. It displays which changes have been staged, which haven't, and which files aren't being tracked by Git. This is a fundamental command for understanding the current state of your repository before making commits.
145
+
146
+ <reasoning>
147
+ The assistant did not use the todo list because this is an informational request with no actual coding task to complete. The user is simply asking for an explanation, not for the assistant to perform multiple steps or tasks.
148
+ </reasoning>
149
+ </example>
150
+
151
+ <example>
152
+ User: Can you add a comment to the calculateTotal function to explain what it does?
153
+ Assistant: Sure, let me add a comment to the calculateTotal function to explain what it does.
154
+ * Uses the Edit tool to add a comment to the calculateTotal function *
155
+
156
+ <reasoning>
157
+ The assistant did not use the todo list because this is a single, straightforward task confined to one location in the code. Adding a comment doesn't require tracking multiple steps or systematic organization.
158
+ </reasoning>
159
+ </example>
160
+
161
+ <example>
162
+ User: Run npm install for me and tell me what happens.
163
+ Assistant: I'll run the npm install command for you.
164
+
165
+ *Executes: npm install*
166
+
167
+ The command completed successfully. Here's the output:
168
+ [Output of npm install command]
169
+
170
+ All dependencies have been installed according to your package.json file.
171
+
172
+ <reasoning>
173
+ The assistant did not use the todo list because this is a single command execution with immediate results. There are no multiple steps to track or organize, making the todo list unnecessary for this straightforward task.
174
+ </reasoning>
175
+ </example>
176
+
177
+ ## Task States and Management
178
+
179
+ 1. **Task States**: Use these states to track progress:
180
+ - pending: Task not yet started
181
+ - in_progress: Currently working on (limit to ONE task at a time)
182
+ - completed: Task finished successfully
183
+ - cancelled: Task no longer needed
184
+
185
+ 2. **Task Management**:
186
+ - Update task status in real-time as you work
187
+ - Mark tasks complete IMMEDIATELY after finishing (don't batch completions)
188
+ - Only have ONE task in_progress at any time
189
+ - Complete current tasks before starting new ones
190
+ - Cancel tasks that become irrelevant
191
+
192
+ 3. **Task Breakdown**:
193
+ - Create specific, actionable items
194
+ - Break complex tasks into smaller, manageable steps
195
+ - Use clear, descriptive task names
196
+
197
+ When in doubt, use this tool. Being proactive with task management demonstrates attentiveness and ensures you complete all requirements successfully.
198
+ """
199
+
200
+ TODO_READ_DESCRIPTION = "Use this tool to read your todo list"
201
+
202
+ TodoStatus = Literal["pending", "in_progress", "completed", "cancelled"]
203
+ TodoPriority = Literal["high", "medium", "low"]
204
+
205
+
206
+ class TodoItem(BaseModel):
207
+ """Structured representation of a single todo entry."""
208
+
209
+ content: str = Field(description="Brief description of the task")
210
+ status: TodoStatus = Field(
211
+ default="pending",
212
+ description="Current status of the task: pending, in_progress, completed, cancelled",
213
+ )
214
+ priority: TodoPriority = Field(
215
+ default="medium",
216
+ description="Priority level of the task: high, medium, low",
217
+ )
218
+ id: str = Field(
219
+ default_factory=lambda: uuid.uuid4().hex,
220
+ description="Unique identifier for the todo item",
221
+ )
222
+
223
+ @field_validator("status", "priority", mode="before")
224
+ @classmethod
225
+ def _normalize_lower(cls, value: Any) -> Any:
226
+ if isinstance(value, str):
227
+ return value.strip().lower()
228
+ return value
229
+
230
+ def is_active(self) -> bool:
231
+ return self.status not in {"completed", "cancelled"}
232
+
233
+
234
+ TodoLike = TodoItem | dict[str, Any]
235
+
236
+
237
+ class TodoManager:
238
+ """Stateful todo scratchpad that exposes read/write tools."""
239
+
240
+ def __init__(
241
+ self,
242
+ todos: Sequence[TodoLike] | None = None,
243
+ *,
244
+ write_tool_name: str = "todowrite",
245
+ read_tool_name: str = "todoread",
246
+ ):
247
+ self.write_tool_name = write_tool_name
248
+ self.read_tool_name = read_tool_name
249
+ self._todos: list[TodoItem] = []
250
+ self._tools: list[Tool] | None = None
251
+
252
+ if todos:
253
+ self._todos = [self._coerce(todo) for todo in todos]
254
+
255
+ def _coerce(self, todo: TodoLike) -> TodoItem:
256
+ if isinstance(todo, TodoItem):
257
+ return todo
258
+ if isinstance(todo, dict):
259
+ return TodoItem(**todo)
260
+ raise TypeError("Todos must be TodoItem instances or dictionaries")
261
+
262
+ def _serialize(self) -> list[dict[str, Any]]:
263
+ return [todo.model_dump() for todo in self._todos]
264
+
265
+ def _pending_count(self) -> int:
266
+ return sum(1 for todo in self._todos if todo.is_active())
267
+
268
+ def _format_output(self) -> str:
269
+ payload = {
270
+ "title": f"{self._pending_count()} todos",
271
+ "todos": self._serialize(),
272
+ }
273
+ return json.dumps(payload, indent=2)
274
+
275
+ def _write_tool(self, todos: list[dict[str, Any]]) -> str:
276
+ self._todos = [self._coerce(todo) for todo in todos]
277
+ return self._format_output()
278
+
279
+ def _read_tool(self) -> str:
280
+ return self._format_output()
281
+
282
+ def get_todos(self) -> list[TodoItem]:
283
+ """Return a copy of the current todo list."""
284
+ return list(self._todos)
285
+
286
+ def get_tools(self) -> list[Tool]:
287
+ """Return Tool instances bound to this manager's state."""
288
+ if self._tools is not None:
289
+ return self._tools
290
+
291
+ todo_definition = {
292
+ "type": "object",
293
+ "properties": {
294
+ "content": {
295
+ "type": "string",
296
+ "description": "Brief description of the task",
297
+ },
298
+ "status": {
299
+ "type": "string",
300
+ "description": "Current status of the task: pending, in_progress, completed, cancelled",
301
+ "enum": ["pending", "in_progress", "completed", "cancelled"],
302
+ },
303
+ "priority": {
304
+ "type": "string",
305
+ "description": "Priority level of the task: high, medium, low",
306
+ "enum": ["high", "medium", "low"],
307
+ },
308
+ "id": {
309
+ "type": "string",
310
+ "description": "Unique identifier for the todo item",
311
+ },
312
+ },
313
+ "required": ["content", "status", "priority", "id"],
314
+ }
315
+
316
+ write_tool = Tool(
317
+ name=self.write_tool_name,
318
+ description=TODO_WRITE_DESCRIPTION,
319
+ parameters={
320
+ "todos": {
321
+ "type": "array",
322
+ "description": "The updated todo list",
323
+ "items": {"$ref": "#/$defs/Todo"},
324
+ }
325
+ },
326
+ required=["todos"],
327
+ definitions={"Todo": todo_definition},
328
+ run=self._write_tool,
329
+ )
330
+
331
+ read_tool = Tool(
332
+ name=self.read_tool_name,
333
+ description=TODO_READ_DESCRIPTION,
334
+ parameters={},
335
+ run=self._read_tool,
336
+ )
337
+
338
+ self._tools = [write_tool, read_tool]
339
+ return self._tools
340
+
341
+
342
+ __all__ = ["TodoManager", "TodoItem", "TodoStatus", "TodoPriority"]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lm_deluge
3
- Version: 0.0.75
3
+ Version: 0.0.76
4
4
  Summary: Python utility for using LLM API models.
5
5
  Author-email: Benjamin Anderson <ben@trytaylor.ai>
6
6
  Requires-Python: >=3.10
@@ -48,9 +48,12 @@ src/lm_deluge/built_in_tools/anthropic/editor.py
48
48
  src/lm_deluge/llm_tools/__init__.py
49
49
  src/lm_deluge/llm_tools/classify.py
50
50
  src/lm_deluge/llm_tools/extract.py
51
+ src/lm_deluge/llm_tools/filesystem.py
51
52
  src/lm_deluge/llm_tools/locate.py
52
53
  src/lm_deluge/llm_tools/ocr.py
53
54
  src/lm_deluge/llm_tools/score.py
55
+ src/lm_deluge/llm_tools/subagents.py
56
+ src/lm_deluge/llm_tools/todos.py
54
57
  src/lm_deluge/llm_tools/translate.py
55
58
  src/lm_deluge/models/__init__.py
56
59
  src/lm_deluge/models/anthropic.py
File without changes
File without changes
File without changes