Skip to content

Commit c82a970

Browse files
hangfeicopybara-github
authored andcommitted
feat: Introduce app abstraction, contents_strategy and compaction_strategy
* `app`: the top level abstraction for an ADK application. It contains an root agent, and plugins. * `content_strategy`: the abstraction for selecting the contents for LLM request. * `compaction_strategy`: the abstraction for compacting the events. * Added `sequence_id` and `summary_range` in event class. PiperOrigin-RevId: 791357608
1 parent 71fbc92 commit c82a970

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

src/google/adk/agents/app.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import annotations
15+
16+
from typing import Union
17+
from pydantic import BaseModel, Field
18+
from ..plugins.base_plugin import BasePlugin
19+
from .base_agent import BaseAgent
20+
from .base_compaction_strategy import BaseCompactionStrategy
21+
from .base_contents_strategy import BaseContentsStrategy
22+
from ..events.event import Event
23+
24+
class App(BaseModel):
25+
"""Agentic application."""
26+
27+
name: str
28+
"""The name of the application."""
29+
30+
root_agent: BaseAgent = None
31+
"""The root agent in the application."""
32+
33+
plugins: list[BasePlugin] = Field(default_factory=list)
34+
"""The plugins in the application."""
35+
36+
include_contents: Union[str, BaseContentsStrategy] = None
37+
"""The strategy to include contents in the application."""
38+
39+
compaction_strategy: Union[str, BaseCompactionStrategy] = None
40+
"""The strategy to compact the contents in the application."""
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import annotations
15+
16+
from typing import Optional
17+
18+
from pydantic import BaseModel
19+
20+
from ..events.event import Event
21+
22+
23+
class BaseCompactionStrategy(BaseModel):
24+
"""Base interface for compacting events."""
25+
26+
def compact_events(
27+
self,
28+
current_branch: Optional[str],
29+
events: list[Event],
30+
agent_name: str = '',
31+
) -> Event:
32+
"""Compacts the events.
33+
34+
This method will summarize the events and return a new summray event
35+
indicating the range of events it summarized.
36+
37+
When sending events to the LLM, if a summary event is present, the events it
38+
replaces (those identified in itssummary_range) should not be included.
39+
40+
Args:
41+
current_branch: The current branch of the agent.
42+
events: Events to compact.
43+
agent_name: The name of the agent.
44+
45+
Returns:
46+
The new compaction event.
47+
"""
48+
raise NotImplementedError()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import annotations
15+
16+
from typing import List, Optional
17+
from pydantic import BaseModel
18+
from ..events.event import Event
19+
20+
21+
class BaseContentsStrategy(BaseModel):
22+
"""Base interface for handling event filtering or select logics."""
23+
24+
def get_events(
25+
self,
26+
current_branch: Optional[str],
27+
events: list[Event],
28+
agent_name: str = '',
29+
) -> List[Event]:
30+
"""Get the contents for the LLM request.
31+
32+
Applies filtering, rearrangement, and content processing to events.
33+
34+
Args:
35+
current_branch: The current branch of the agent.
36+
events: Events to process.
37+
agent_name: The name of the agent.
38+
39+
Returns:
40+
A list of returned contents.
41+
"""
42+
raise NotImplementedError()

src/google/adk/events/event.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ class Event(LlmResponse):
7777
conversation history.
7878
"""
7979

80+
sequence_id: Optional[int] = None
81+
"""The sequence id of the event."""
82+
83+
# TODO: we can support continuous range if we are certain the range is always continous
84+
# or discrete items if we want to support non-continuous range.
85+
# The sequence ID range of the events that are summarized, in the form(start_sequence_id, end_sequence_id)`
86+
summary_range: Optional[tuple[int, int]] = None
87+
"""The range of events to summarize."""
88+
8089
# The following are computed fields.
8190
# Do not assign the ID. It will be assigned by the session.
8291
id: str = ''

0 commit comments

Comments
 (0)