From efcb1ecda7073635ec827d7d68c9970c5d04a7ce Mon Sep 17 00:00:00 2001 From: Br1an67 <932039080@qq.com> Date: Mon, 2 Mar 2026 01:17:53 +0800 Subject: [PATCH] feat: add memory field to AgentDefinition Add an optional `memory` field to `AgentDefinition` that accepts 'user', 'project', or 'local' to enable persistent cross-session learning for subagents, matching the CLI's --agents frontmatter support. --- src/claude_agent_sdk/types.py | 1 + tests/test_types.py | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/claude_agent_sdk/types.py b/src/claude_agent_sdk/types.py index 3ea89d5a..37fbc5ea 100644 --- a/src/claude_agent_sdk/types.py +++ b/src/claude_agent_sdk/types.py @@ -47,6 +47,7 @@ class AgentDefinition: prompt: str tools: list[str] | None = None model: Literal["sonnet", "opus", "haiku", "inherit"] | None = None + memory: Literal["user", "project", "local"] | None = None # Permission Update types (matching TypeScript SDK) diff --git a/tests/test_types.py b/tests/test_types.py index 95a88bfa..a0ce0bbf 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -159,6 +159,42 @@ def test_claude_code_options_with_model_specification(self): assert options.permission_prompt_tool_name == "CustomTool" +class TestAgentDefinition: + """Test AgentDefinition configuration.""" + + def test_agent_definition_defaults(self): + """Test AgentDefinition with required fields only.""" + from dataclasses import asdict + + from claude_agent_sdk.types import AgentDefinition + + agent = AgentDefinition(description="Test agent", prompt="You are a test agent") + assert agent.tools is None + assert agent.model is None + assert agent.memory is None + + # Verify None fields are excluded from serialized dict + d = {k: v for k, v in asdict(agent).items() if v is not None} + assert "memory" not in d + assert "tools" not in d + + def test_agent_definition_with_memory(self): + """Test AgentDefinition with memory field.""" + from dataclasses import asdict + + from claude_agent_sdk.types import AgentDefinition + + agent = AgentDefinition( + description="Memory agent", + prompt="You are an agent with memory", + memory="project", + ) + assert agent.memory == "project" + + d = {k: v for k, v in asdict(agent).items() if v is not None} + assert d["memory"] == "project" + + class TestHookInputTypes: """Test hook input type definitions."""