Skip to content

Commit efb9c93

Browse files
authored
Merge pull request #249 from dapr/cyb3rward0g/agent-orchestrator-state
Refactor: Auto-inject Agent State Schemas and simplify agent configuration API
2 parents e3a151d + 3f08c09 commit efb9c93

File tree

32 files changed

+614
-413
lines changed

32 files changed

+614
-413
lines changed

dapr_agents/agents/base.py

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
AgentRegistryConfig,
1313
AgentStateConfig,
1414
AgentExecutionConfig,
15+
DEFAULT_AGENT_WORKFLOW_BUNDLE,
1516
)
1617
from dapr_agents.agents.prompting import AgentProfileConfig, PromptingAgentBase
1718
from dapr_agents.agents.utils.text_printer import ColorTextFormatter
@@ -44,7 +45,7 @@ def __init__(
4445
self,
4546
*,
4647
# Profile / prompt
47-
profile_config: Optional[AgentProfileConfig] = None,
48+
profile: Optional[AgentProfileConfig] = None,
4849
name: Optional[str] = None,
4950
role: Optional[str] = None,
5051
goal: Optional[str] = None,
@@ -53,42 +54,42 @@ def __init__(
5354
system_prompt: Optional[str] = None,
5455
prompt_template: Optional[PromptTemplateBase] = None,
5556
# Components (infrastructure)
56-
pubsub_config: Optional[AgentPubSubConfig] = None,
57-
state_config: Optional[AgentStateConfig] = None,
58-
registry_config: Optional[AgentRegistryConfig] = None,
57+
pubsub: Optional[AgentPubSubConfig] = None,
58+
state: Optional[AgentStateConfig] = None,
59+
registry: Optional[AgentRegistryConfig] = None,
5960
base_metadata: Optional[Dict[str, Any]] = None,
6061
max_etag_attempts: int = 10,
6162
# Memory / runtime
62-
memory_config: Optional[AgentMemoryConfig] = None,
63+
memory: Optional[AgentMemoryConfig] = None,
6364
llm: Optional[ChatClientBase] = None,
6465
tools: Optional[Iterable[Any]] = None,
6566
# Metadata
6667
agent_metadata: Optional[Dict[str, Any]] = None,
6768
# Execution
68-
execution_config: Optional[AgentExecutionConfig] = None,
69+
execution: Optional[AgentExecutionConfig] = None,
6970
) -> None:
7071
"""
7172
Initialize an agent with behavior + infrastructure.
7273
7374
Args:
74-
profile_config: Base profile config (name/role/goal/prompts). Optional if
75+
profile: Base profile config (name/role/goal/prompts). Optional if
7576
individual fields are provided below.
76-
name: Agent name (required if `profile_config` is omitted).
77+
name: Agent name (required if `profile` is omitted).
7778
role: Agent role (e.g., "Assistant").
7879
goal: High-level agent objective.
7980
instructions: Additional instruction strings for the prompt.
8081
style_guidelines: Style directives for the prompt.
8182
system_prompt: System prompt override.
8283
prompt_template: Optional explicit prompt template instance.
8384
84-
pubsub_config: Pub/Sub config used by `AgentComponents`.
85-
state_config: Durable state config used by `AgentComponents`.
86-
registry_config: Team registry config used by `AgentComponents`.
87-
execution_config: Execution dials for the agent run.
85+
pubsub: Pub/Sub config used by `AgentComponents`.
86+
state: Durable state config used by `AgentComponents`.
87+
registry: Team registry config used by `AgentComponents`.
88+
execution: Execution dials for the agent run.
8889
base_metadata: Default Dapr state metadata used by `AgentComponents`.
8990
max_etag_attempts: Concurrency retry count for registry mutations.
9091
91-
memory_config: Memory backend configuration. If omitted and a state store
92+
memory: Memory backend configuration. If omitted and a state store
9293
is configured, a Dapr-backed conversation memory is created by default.
9394
llm: Chat client. Defaults to `get_default_llm()`.
9495
tools: Optional tool callables or `AgentTool` instances.
@@ -97,38 +98,39 @@ def __init__(
9798
"""
9899
# Resolve and validate profile (ensures non-empty name).
99100
resolved_profile = self._build_profile(
100-
base_profile=profile_config,
101+
base_profile=profile,
101102
name=name,
102103
role=role,
103104
goal=goal,
104105
instructions=instructions,
105106
style_guidelines=style_guidelines,
106107
system_prompt=system_prompt,
107108
)
108-
self.profile_config = resolved_profile
109+
self.profile = resolved_profile
109110
self.name = resolved_profile.name # type: ignore[assignment]
110111

111112
# Wire infrastructure via AgentComponents.
112113
super().__init__(
113114
name=self.name,
114-
pubsub_config=pubsub_config,
115-
state_config=state_config,
116-
registry_config=registry_config,
115+
pubsub=pubsub,
116+
state=state,
117+
registry=registry,
117118
base_metadata=base_metadata,
118119
max_etag_attempts=max_etag_attempts,
120+
default_bundle=DEFAULT_AGENT_WORKFLOW_BUNDLE,
119121
)
120122

121123
# -----------------------------
122124
# Memory wiring
123125
# -----------------------------
124-
self._memory_config = memory_config or AgentMemoryConfig()
125-
if self._memory_config.store is None and state_config is not None:
126+
self._memory = memory or AgentMemoryConfig()
127+
if self._memory.store is None and state is not None:
126128
# Auto-provision a Dapr-backed memory if we have a state store.
127-
self._memory_config.store = ConversationDaprStateMemory( # type: ignore[union-attr]
128-
store_name=state_config.store.store_name,
129+
self._memory.store = ConversationDaprStateMemory( # type: ignore[union-attr]
130+
store_name=state.store.store_name,
129131
session_id=f"{self.name}-session",
130132
)
131-
self.memory = self._memory_config.store or ConversationListMemory()
133+
self.memory = self._memory.store or ConversationListMemory()
132134

133135
# -----------------------------
134136
# Prompting helper
@@ -143,15 +145,15 @@ def __init__(
143145
template_format=resolved_profile.template_format,
144146
include_chat_history=True,
145147
prompt_template=prompt_template,
146-
profile_config=resolved_profile,
148+
profile=resolved_profile,
147149
)
148150
# Keep profile config synchronized with helper defaults.
149-
if self.profile_config.name is None:
150-
self.profile_config.name = self.prompting_helper.name
151-
if self.profile_config.role is None:
152-
self.profile_config.role = self.prompting_helper.role
153-
if self.profile_config.goal is None:
154-
self.profile_config.goal = self.prompting_helper.goal
151+
if self.profile.name is None:
152+
self.profile.name = self.prompting_helper.name
153+
if self.profile.role is None:
154+
self.profile.role = self.prompting_helper.role
155+
if self.profile.goal is None:
156+
self.profile.goal = self.prompting_helper.goal
155157

156158
self.prompt_template = self.prompting_helper.prompt_template
157159
self._text_formatter = self.prompting_helper.text_formatter
@@ -173,14 +175,21 @@ def __init__(
173175
# -----------------------------
174176
# Execution config
175177
# -----------------------------
176-
self.execution_config = execution_config or AgentExecutionConfig()
178+
self.execution = execution or AgentExecutionConfig()
177179
try:
178-
self.execution_config.max_iterations = max(
179-
1, int(self.execution_config.max_iterations)
180-
)
180+
self.execution.max_iterations = max(1, int(self.execution.max_iterations))
181181
except Exception:
182-
self.execution_config.max_iterations = 10
183-
self.execution_config.tool_choice = self.execution_config.tool_choice or "auto"
182+
self.execution.max_iterations = 10
183+
if not self.tools:
184+
if self.execution.tool_choice is not None:
185+
logger.debug(
186+
"No tools configured for agent '%s'; ignoring tool_choice=%r.",
187+
self.name,
188+
self.execution.tool_choice,
189+
)
190+
self.execution.tool_choice = None
191+
elif self.execution.tool_choice is None:
192+
self.execution.tool_choice = "auto"
184193

185194
# -----------------------------
186195
# Load durable state (from AgentComponents)
@@ -200,7 +209,7 @@ def __init__(
200209
"goal": self.prompting_helper.goal,
201210
"instructions": list(self.prompting_helper.instructions),
202211
}
203-
if self.pubsub_config is not None:
212+
if self.pubsub is not None:
204213
base_meta["topic_name"] = self.agent_topic_name
205214
base_meta["pubsub_name"] = self.message_bus_name
206215

@@ -433,7 +442,7 @@ def _build_profile(
433442
if not profile.name or not profile.name.strip():
434443
raise ValueError(
435444
"Durable agents require a non-empty name "
436-
"(provide name= or profile_config.name)."
445+
"(provide name= or profile.name)."
437446
)
438447

439448
return profile

0 commit comments

Comments
 (0)