Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions agents/s01_agent_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from anthropic import Anthropic
from dotenv import load_dotenv
from utils import content_to_dicts

load_dotenv(override=True)

Expand Down Expand Up @@ -70,8 +71,7 @@ def agent_loop(messages: list):
model=MODEL, system=SYSTEM, messages=messages,
tools=TOOLS, max_tokens=8000,
)
# Append assistant turn
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "assistant", "content": content_to_dicts(response.content)})
# If the model didn't call a tool, we're done
if response.stop_reason != "tool_use":
return
Expand Down
3 changes: 2 additions & 1 deletion agents/s02_tool_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from anthropic import Anthropic
from dotenv import load_dotenv
from utils import content_to_dicts

load_dotenv(override=True)

Expand Down Expand Up @@ -116,7 +117,7 @@ def agent_loop(messages: list):
model=MODEL, system=SYSTEM, messages=messages,
tools=TOOLS, max_tokens=8000,
)
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "assistant", "content": content_to_dicts(response.content)})
if response.stop_reason != "tool_use":
return
results = []
Expand Down
3 changes: 2 additions & 1 deletion agents/s03_todo_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

from anthropic import Anthropic
from dotenv import load_dotenv
from utils import content_to_dicts

load_dotenv(override=True)

Expand Down Expand Up @@ -168,7 +169,7 @@ def agent_loop(messages: list):
model=MODEL, system=SYSTEM, messages=messages,
tools=TOOLS, max_tokens=8000,
)
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "assistant", "content": content_to_dicts(response.content)})
if response.stop_reason != "tool_use":
return
results = []
Expand Down
5 changes: 3 additions & 2 deletions agents/s04_subagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from anthropic import Anthropic
from dotenv import load_dotenv
from utils import content_to_dicts

load_dotenv(override=True)

Expand Down Expand Up @@ -119,7 +120,7 @@ def run_subagent(prompt: str) -> str:
model=MODEL, system=SUBAGENT_SYSTEM, messages=sub_messages,
tools=CHILD_TOOLS, max_tokens=8000,
)
sub_messages.append({"role": "assistant", "content": response.content})
sub_messages.append({"role": "assistant", "content": content_to_dicts(response.content)})
if response.stop_reason != "tool_use":
break
results = []
Expand All @@ -146,7 +147,7 @@ def agent_loop(messages: list):
model=MODEL, system=SYSTEM, messages=messages,
tools=PARENT_TOOLS, max_tokens=8000,
)
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "assistant", "content": content_to_dicts(response.content)})
if response.stop_reason != "tool_use":
return
results = []
Expand Down
3 changes: 2 additions & 1 deletion agents/s05_skill_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

from anthropic import Anthropic
from dotenv import load_dotenv
from utils import content_to_dicts

load_dotenv(override=True)

Expand Down Expand Up @@ -190,7 +191,7 @@ def agent_loop(messages: list):
model=MODEL, system=SYSTEM, messages=messages,
tools=TOOLS, max_tokens=8000,
)
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "assistant", "content": content_to_dicts(response.content)})
if response.stop_reason != "tool_use":
return
results = []
Expand Down
3 changes: 2 additions & 1 deletion agents/s06_context_compact.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

from anthropic import Anthropic
from dotenv import load_dotenv
from utils import content_to_dicts

load_dotenv(override=True)

Expand Down Expand Up @@ -203,7 +204,7 @@ def agent_loop(messages: list):
model=MODEL, system=SYSTEM, messages=messages,
tools=TOOLS, max_tokens=8000,
)
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "assistant", "content": content_to_dicts(response.content)})
if response.stop_reason != "tool_use":
return
results = []
Expand Down
3 changes: 2 additions & 1 deletion agents/s07_task_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from anthropic import Anthropic
from dotenv import load_dotenv
from utils import content_to_dicts

load_dotenv(override=True)

Expand Down Expand Up @@ -212,7 +213,7 @@ def agent_loop(messages: list):
model=MODEL, system=SYSTEM, messages=messages,
tools=TOOLS, max_tokens=8000,
)
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "assistant", "content": content_to_dicts(response.content)})
if response.stop_reason != "tool_use":
return
results = []
Expand Down
3 changes: 2 additions & 1 deletion agents/s08_background_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

from anthropic import Anthropic
from dotenv import load_dotenv
from utils import content_to_dicts

load_dotenv(override=True)

Expand Down Expand Up @@ -198,7 +199,7 @@ def agent_loop(messages: list):
model=MODEL, system=SYSTEM, messages=messages,
tools=TOOLS, max_tokens=8000,
)
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "assistant", "content": content_to_dicts(response.content)})
if response.stop_reason != "tool_use":
return
results = []
Expand Down
3 changes: 2 additions & 1 deletion agents/s09_agent_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

from anthropic import Anthropic
from dotenv import load_dotenv
from utils import content_to_dicts

load_dotenv(override=True)
if os.getenv("ANTHROPIC_BASE_URL"):
Expand Down Expand Up @@ -360,7 +361,7 @@ def agent_loop(messages: list):
tools=TOOLS,
max_tokens=8000,
)
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "assistant", "content": content_to_dicts(response.content)})
if response.stop_reason != "tool_use":
return
results = []
Expand Down
3 changes: 2 additions & 1 deletion agents/s10_team_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

from anthropic import Anthropic
from dotenv import load_dotenv
from utils import content_to_dicts

load_dotenv(override=True)
if os.getenv("ANTHROPIC_BASE_URL"):
Expand Down Expand Up @@ -441,7 +442,7 @@ def agent_loop(messages: list):
tools=TOOLS,
max_tokens=8000,
)
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "assistant", "content": content_to_dicts(response.content)})
if response.stop_reason != "tool_use":
return
results = []
Expand Down
3 changes: 2 additions & 1 deletion agents/s11_autonomous_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

from anthropic import Anthropic
from dotenv import load_dotenv
from utils import content_to_dicts

load_dotenv(override=True)
if os.getenv("ANTHROPIC_BASE_URL"):
Expand Down Expand Up @@ -525,7 +526,7 @@ def agent_loop(messages: list):
tools=TOOLS,
max_tokens=8000,
)
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "assistant", "content": content_to_dicts(response.content)})
if response.stop_reason != "tool_use":
return
results = []
Expand Down
3 changes: 2 additions & 1 deletion agents/s12_worktree_task_isolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

from anthropic import Anthropic
from dotenv import load_dotenv
from utils import content_to_dicts

load_dotenv(override=True)

Expand Down Expand Up @@ -734,7 +735,7 @@ def agent_loop(messages: list):
tools=TOOLS,
max_tokens=8000,
)
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "assistant", "content": content_to_dicts(response.content)})
if response.stop_reason != "tool_use":
return

Expand Down
7 changes: 4 additions & 3 deletions agents/s_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

from anthropic import Anthropic
from dotenv import load_dotenv
from utils import content_to_dicts

load_dotenv(override=True)
if os.getenv("ANTHROPIC_BASE_URL"):
Expand Down Expand Up @@ -180,7 +181,7 @@ def run_subagent(prompt: str, agent_type: str = "Explore") -> str:
resp = None
for _ in range(30):
resp = client.messages.create(model=MODEL, messages=sub_msgs, tools=sub_tools, max_tokens=8000)
sub_msgs.append({"role": "assistant", "content": resp.content})
sub_msgs.append({"role": "assistant", "content": content_to_dicts(resp.content)})
if resp.stop_reason != "tool_use":
break
results = []
Expand Down Expand Up @@ -468,7 +469,7 @@ def _loop(self, name: str, role: str, prompt: str):
except Exception:
self._set_status(name, "shutdown")
return
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "assistant", "content": content_to_dicts(response.content)})
if response.stop_reason != "tool_use":
break
results = []
Expand Down Expand Up @@ -675,7 +676,7 @@ def agent_loop(messages: list):
model=MODEL, system=SYSTEM, messages=messages,
tools=TOOLS, max_tokens=8000,
)
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "assistant", "content": content_to_dicts(response.content)})
if response.stop_reason != "tool_use":
return
# Tool execution
Expand Down
6 changes: 6 additions & 0 deletions agents/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Shared utilities for all agent files."""


def content_to_dicts(content) -> list:
"""Convert Anthropic SDK response content blocks to serializable dicts."""
return [{"type": b.type, **b.model_dump(exclude={"type"})} for b in content]