Skip to content

Commit af2ecb2

Browse files
committed
docs(showcase/ag2): region markers for shared-state-read-write + subagents
Catches up ag2's shared-state-read-write and subagents demos (added in PR CopilotKit#4359, post smalls batch 1) to parity with langgraph-python. - shared-state-read-write: nested use-agent/use-agent-read and set-state/use-agent-write on page.tsx; notes-card-render and preferences-card-render on the card components (6 regions total) - subagents: delegation-log-frontend on the log component; subagent-setup + supervisor-delegation-tools on src/agents/subagents.py wrapping AG2/AutoGen ConversableAgent definitions and the @tool-decorated supervisor delegation functions (3 regions total)
1 parent a703664 commit af2ecb2

5 files changed

Lines changed: 32 additions & 0 deletions

File tree

showcase/integrations/ag2/src/agents/subagents.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class SubagentsSnapshot(BaseModel):
6161
# prompt. They don't share memory or tools with the supervisor — the
6262
# supervisor only sees what each sub-agent's final reply produces.
6363

64+
# @region[subagent-setup]
6465
_SUB_LLM_CONFIG = LLMConfig({"model": "gpt-4o-mini", "stream": False})
6566

6667
_research_agent = ConversableAgent(
@@ -102,6 +103,7 @@ class SubagentsSnapshot(BaseModel):
102103
human_input_mode="NEVER",
103104
max_consecutive_auto_reply=1,
104105
)
106+
# @endregion[subagent-setup]
105107

106108

107109
async def _invoke_sub_agent(sub_agent: ConversableAgent, task: str) -> str:
@@ -215,6 +217,12 @@ async def _run_delegation(
215217
# ---------------------------------------------------------------------------
216218

217219

220+
# @region[supervisor-delegation-tools]
221+
# Each @tool wraps a sub-agent invocation. The supervisor LLM "calls"
222+
# these tools to delegate work; each call asynchronously runs the
223+
# matching sub-agent, records the delegation into shared state via
224+
# ContextVariables, and returns a ReplyResult the supervisor reads as
225+
# its tool output on the next step.
218226
@tool()
219227
async def research_agent(
220228
context_variables: ContextVariables,
@@ -266,6 +274,7 @@ async def critique_agent(
266274
return await _run_delegation(
267275
context_variables, "critique_agent", _critique_agent, task
268276
)
277+
# @endregion[supervisor-delegation-tools]
269278

270279

271280
# ---------------------------------------------------------------------------

showcase/integrations/ag2/src/app/demos/shared-state-read-write/notes-card.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export interface NotesCardProps {
1616
* triggers a re-render. The "Clear" button is a write-back (UI -> agent
1717
* state) to demonstrate both directions on the same field.
1818
*/
19+
// @region[notes-card-render]
20+
// Read-side render: this card reflects the agent-authored `notes` slice
21+
// of shared state. The parent page passes `state.notes` in; we never
22+
// touch agent state ourselves — we just render it. The Clear button is
23+
// a small write-back, exposed as an `onClear` prop.
1924
export function NotesCard({ notes, onClear }: NotesCardProps) {
2025
return (
2126
<div
@@ -67,3 +72,4 @@ export function NotesCard({ notes, onClear }: NotesCardProps) {
6772
</div>
6873
);
6974
}
75+
// @endregion[notes-card-render]

showcase/integrations/ag2/src/app/demos/shared-state-read-write/page.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export default function SharedStateReadWriteDemo() {
3737
}
3838

3939
function DemoContent() {
40+
// @region[use-agent]
41+
// @region[use-agent-read]
4042
// Subscribe the component to agent state changes. Any time the agent
4143
// mutates its state (e.g. via its `set_notes` tool returning a
4244
// ReplyResult with updated ContextVariables) this hook fires, we
@@ -45,6 +47,8 @@ function DemoContent() {
4547
agentId: "shared-state-read-write",
4648
updates: [UseAgentUpdate.OnStateChanged],
4749
});
50+
// @endregion[use-agent-read]
51+
// @endregion[use-agent]
4852

4953
useConfigureSuggestions({
5054
suggestions: [
@@ -78,6 +82,8 @@ function DemoContent() {
7882
// eslint-disable-next-line react-hooks/exhaustive-deps
7983
}, []);
8084

85+
// @region[set-state]
86+
// @region[use-agent-write]
8187
// WRITE: every edit in the sidebar goes straight into agent state. On
8288
// the agent's next turn, AG2 hydrates ContextVariables from this and
8389
// the agent's `get_current_preferences` tool reflects them straight
@@ -88,6 +94,8 @@ function DemoContent() {
8894
notes, // preserve what the agent has written
8995
} as RWAgentState);
9096
};
97+
// @endregion[use-agent-write]
98+
// @endregion[set-state]
9199

92100
// WRITE: let the user clear the agent-authored notes from the UI.
93101
const handleClearNotes = () => {

showcase/integrations/ag2/src/app/demos/shared-state-read-write/preferences-card.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ export interface PreferencesCardProps {
3333
* agent calls its `get_current_preferences` tool to read those values out
3434
* of `ContextVariables` and tailors its reply accordingly.
3535
*/
36+
// @region[preferences-card-render]
37+
// Write-side render: every edit here bubbles up through `onChange`, and
38+
// the parent pipes it straight into `agent.setState({ preferences: ... })`.
39+
// Nothing in this component knows about the agent directly — that's
40+
// intentional: the card is a plain controlled form, and the agent state
41+
// wiring lives one layer up.
3642
export function PreferencesCard({ value, onChange }: PreferencesCardProps) {
3743
const set = <K extends keyof Preferences>(key: K, v: Preferences[K]) =>
3844
onChange({ ...value, [key]: v });
@@ -143,3 +149,4 @@ export function PreferencesCard({ value, onChange }: PreferencesCardProps) {
143149
</div>
144150
);
145151
}
152+
// @endregion[preferences-card-render]

showcase/integrations/ag2/src/app/demos/subagents/delegation-log.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const STATUS_BADGE: Record<Delegation["status"], string> = {
5151
failed: "text-[#D14343]",
5252
};
5353

54+
// @region[delegation-log-frontend]
5455
/**
5556
* Live delegation log — renders the `delegations` slot of agent state.
5657
*
@@ -137,3 +138,4 @@ export function DelegationLog({ delegations, isRunning }: DelegationLogProps) {
137138
</div>
138139
);
139140
}
141+
// @endregion[delegation-log-frontend]

0 commit comments

Comments
 (0)