|
| 1 | +# Copyright 2026 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 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from typing import Optional |
| 18 | + |
| 19 | +from pydantic import BaseModel |
| 20 | +from pydantic import ConfigDict |
| 21 | +from pydantic import Field |
| 22 | +from pydantic import model_validator |
| 23 | + |
| 24 | +from ..utils.feature_decorator import experimental |
| 25 | +from .base_events_summarizer import BaseEventsSummarizer |
| 26 | + |
| 27 | + |
| 28 | +@experimental |
| 29 | +class ResumabilityConfig(BaseModel): |
| 30 | + """The config of the resumability for an application. |
| 31 | +
|
| 32 | + The "resumability" in ADK refers to the ability to: |
| 33 | + 1. pause an invocation upon a long-running function call. |
| 34 | + 2. resume an invocation from the last event, if it's paused or failed midway |
| 35 | + through. |
| 36 | +
|
| 37 | + Note: ADK resumes the invocation in a best-effort manner: |
| 38 | + 1. Tool call to resume needs to be idempotent because we only guarantee |
| 39 | + an at-least-once behavior once resumed. |
| 40 | + 2. Any temporary / in-memory state will be lost upon resumption. |
| 41 | + """ |
| 42 | + |
| 43 | + is_resumable: bool = False |
| 44 | + """Whether the app supports agent resumption. |
| 45 | + If enabled, the feature will be enabled for all agents in the app. |
| 46 | + """ |
| 47 | + |
| 48 | + |
| 49 | +@experimental |
| 50 | +class EventsCompactionConfig(BaseModel): |
| 51 | + """The config of event compaction for an application.""" |
| 52 | + |
| 53 | + model_config = ConfigDict( |
| 54 | + arbitrary_types_allowed=True, |
| 55 | + extra="forbid", |
| 56 | + ) |
| 57 | + |
| 58 | + summarizer: Optional[BaseEventsSummarizer] = None |
| 59 | + """The event summarizer to use for compaction.""" |
| 60 | + |
| 61 | + compaction_interval: int |
| 62 | + """The number of *new* user-initiated invocations that, once |
| 63 | + fully represented in the session's events, will trigger a compaction.""" |
| 64 | + |
| 65 | + overlap_size: int |
| 66 | + """The number of preceding invocations to include from the |
| 67 | + end of the last compacted range. This creates an overlap between consecutive |
| 68 | + compacted summaries, maintaining context.""" |
| 69 | + |
| 70 | + token_threshold: Optional[int] = Field( |
| 71 | + default=None, |
| 72 | + gt=0, |
| 73 | + ) |
| 74 | + """Post-invocation token threshold trigger. |
| 75 | +
|
| 76 | + If set, ADK will attempt a post-invocation compaction when the most recently |
| 77 | + observed prompt token count meets or exceeds this threshold. |
| 78 | + """ |
| 79 | + |
| 80 | + event_retention_size: Optional[int] = Field(default=None, ge=0) |
| 81 | + """Post-invocation raw event retention size. |
| 82 | +
|
| 83 | + If token-based post-invocation compaction is triggered, this keeps the last N |
| 84 | + raw events un-compacted. |
| 85 | + """ |
| 86 | + |
| 87 | + @model_validator(mode="after") |
| 88 | + def _validate_token_params(self) -> EventsCompactionConfig: |
| 89 | + token_threshold_set = self.token_threshold is not None |
| 90 | + retention_size_set = self.event_retention_size is not None |
| 91 | + if token_threshold_set != retention_size_set: |
| 92 | + raise ValueError( |
| 93 | + "token_threshold and event_retention_size must be set together." |
| 94 | + ) |
| 95 | + return self |
0 commit comments