|
| 1 | +"""Agentic Generative UI feature.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from textwrap import dedent |
| 6 | +from typing import Any, Literal |
| 7 | + |
| 8 | +from fastapi import FastAPI |
| 9 | +from pydantic import BaseModel, Field |
| 10 | + |
| 11 | +from ag_ui.core import EventType, StateDeltaEvent, StateSnapshotEvent |
| 12 | +from ag_ui_adk import ADKAgent, add_adk_fastapi_endpoint |
| 13 | +from google.adk.agents import LlmAgent |
| 14 | + |
| 15 | +StepStatus = Literal['pending', 'completed'] |
| 16 | + |
| 17 | + |
| 18 | +class Step(BaseModel): |
| 19 | + """Represents a step in a plan.""" |
| 20 | + |
| 21 | + description: str = Field(description='The description of the step') |
| 22 | + status: StepStatus = Field( |
| 23 | + default='pending', |
| 24 | + description='The status of the step (e.g., pending, completed)', |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +class Plan(BaseModel): |
| 29 | + """Represents a plan with multiple steps.""" |
| 30 | + |
| 31 | + steps: list[Step] = Field(default_factory=list, description='The steps in the plan') |
| 32 | + |
| 33 | + |
| 34 | +class JSONPatchOp(BaseModel): |
| 35 | + """A class representing a JSON Patch operation (RFC 6902).""" |
| 36 | + |
| 37 | + op: Literal['add', 'remove', 'replace', 'move', 'copy', 'test'] = Field( |
| 38 | + description='The operation to perform: add, remove, replace, move, copy, or test', |
| 39 | + ) |
| 40 | + path: str = Field(description='JSON Pointer (RFC 6901) to the target location') |
| 41 | + value: Any = Field( |
| 42 | + default=None, |
| 43 | + description='The value to apply (for add, replace operations)', |
| 44 | + ) |
| 45 | + from_: str | None = Field( |
| 46 | + default=None, |
| 47 | + alias='from', |
| 48 | + description='Source path (for move, copy operations)', |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +async def create_plan(steps: list[str]) -> StateSnapshotEvent: |
| 53 | + """Create a plan with multiple steps. |
| 54 | +
|
| 55 | + Args: |
| 56 | + steps (list[str]): List of step descriptions to create the plan. |
| 57 | +
|
| 58 | + Returns: |
| 59 | + StateSnapshotEvent: Event containing the initial state of the steps. |
| 60 | + """ |
| 61 | + plan: Plan = Plan( |
| 62 | + steps=[Step(description=step) for step in steps], |
| 63 | + ) |
| 64 | + return StateSnapshotEvent( |
| 65 | + type=EventType.STATE_SNAPSHOT, |
| 66 | + snapshot=plan.model_dump(), |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +async def update_plan_step( |
| 71 | + index: int, description: str | None = None, status: StepStatus | None = None |
| 72 | +) -> StateDeltaEvent: |
| 73 | + """Update the plan with new steps or changes. |
| 74 | +
|
| 75 | + Args: |
| 76 | + index (int): The index of the step to update. |
| 77 | + description (str | None): The new description for the step. |
| 78 | + status (StepStatus | None): The new status for the step. |
| 79 | +
|
| 80 | + Returns: |
| 81 | + StateDeltaEvent: Event containing the changes made to the plan. |
| 82 | + """ |
| 83 | + changes: list[JSONPatchOp] = [] |
| 84 | + if description is not None: |
| 85 | + changes.append( |
| 86 | + JSONPatchOp( |
| 87 | + op='replace', path=f'/steps/{index}/description', value=description |
| 88 | + ) |
| 89 | + ) |
| 90 | + if status is not None: |
| 91 | + changes.append( |
| 92 | + JSONPatchOp(op='replace', path=f'/steps/{index}/status', value=status) |
| 93 | + ) |
| 94 | + return StateDeltaEvent( |
| 95 | + type=EventType.STATE_DELTA, |
| 96 | + delta=changes, |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +# Create the ADK agent |
| 101 | +agent = LlmAgent( |
| 102 | + name="planner", |
| 103 | + model="gemini-2.0-flash", |
| 104 | + instruction=dedent( |
| 105 | + """ |
| 106 | + When planning use tools only, without any other messages. |
| 107 | + IMPORTANT: |
| 108 | + - Use the `create_plan` tool to set the initial state of the steps |
| 109 | + - Use the `update_plan_step` tool to update the status of each step |
| 110 | + - Do NOT repeat the plan or summarise it in a message |
| 111 | + - Do NOT confirm the creation or updates in a message |
| 112 | + - Do NOT ask the user for additional information or next steps |
| 113 | + - Do NOT leave a plan hanging, always complete the plan via `update_plan_step` if one is ongoing. |
| 114 | +
|
| 115 | + Only one plan can be active at a time, so do not call the `create_plan` tool |
| 116 | + again until all the steps in current plan are completed. |
| 117 | + """ |
| 118 | + ), |
| 119 | + tools=[create_plan, update_plan_step], |
| 120 | +) |
| 121 | + |
| 122 | +# Create ADK middleware agent instance |
| 123 | +adk_agent = ADKAgent( |
| 124 | + adk_agent=agent, |
| 125 | + app_name="demo_app", |
| 126 | + user_id="demo_user", |
| 127 | + session_timeout_seconds=3600, |
| 128 | + use_in_memory_services=True, |
| 129 | +) |
| 130 | + |
| 131 | +# Create FastAPI app |
| 132 | +app = FastAPI(title="ADK Middleware Agentic Generative UI") |
| 133 | + |
| 134 | +# Add the ADK endpoint |
| 135 | +add_adk_fastapi_endpoint(app, adk_agent, path="/") |
0 commit comments